Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Update table contents periodically

Posted on 2011-10-13
5
Medium Priority
?
386 Views
Last Modified: 2012-05-12
I want to refresh my table periodically since its contents change from time to time.

Please let me know what's the best way to do this. Thanks!
0
Comment
Question by:dshrenik
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36966265
You change table model contents and repaint your table
0
 

Author Comment

by:dshrenik
ID: 36966270
I am not changing the contents explicitly.

I want my program to poll data (from a server, for example). This must happen periodically, say eveery 20s. Should I have a timer, or is there a better way?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966276
while(true){
Thread.currentThread().sleep(20000);
//get new data and update the model
jtable.repaint();
}

0
 

Author Comment

by:dshrenik
ID: 36966284
This gets into the constructor?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36966310
Execute this (find some gif to add as before)
and look at the numbers - they will cahnge every
3 seconds.
If you see compiler warning, don't pay attenetion
it is not important.

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout());

    MyTableModel model = new MyTableModel();

    JTable table = new JTable(model);
    table.setRowHeight(80);
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
   // frame.setSize(500, 400);
      frame.pack();
    frame.setVisible(true);
      System.out.println("showed");

      try{
          while(true){
          Thread.currentThread().sleep(3000);
             model.toggleMult();
              frame.repaint();
          }

      }
          catch(Exception ex){

          }
      }

  }


class MyTableModel extends AbstractTableModel {
    boolean mult;

public void toggleMult(){
    mult = !mult;
}


  public Object getValueAt(int row, int column) {
      if(mult)
    return "" + (row * column);
      else return "" + (row + column);
  }

  public int getColumnCount() {
    return 4;
  }

  public int getRowCount() {
    return 5;
  }
}

class ImageRenderer extends DefaultTableCellRenderer {
  JLabel lbl = new JLabel();

  ImageIcon icon = new ImageIcon("HearthFlag.gif");

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    lbl.setText((String) value);
    lbl.setIcon(icon);
    return lbl;
  }
}

Open in new window

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question