ZhiZha
ZhiZha

Reputation: 143

Jtable/JScrollPanel won't refresh (update data)

I have setup a JTable with paging - which works very well, but I have a problem with updating data to the table. table.repaint() is not working. Here is the code that I am using. Thanks in advance!

String[][] data = new String[100][4];

 String[] columnNames = new String[]{
         "IP", "PC_NAME", "ttl", "db"};

Constructor:

gui(){
    JTable table =  new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    JButton next = new JButton("next");
    JButton prev = new JButton("prev");
    next.addActionListener(this);
    prev.addActionListener(this);
    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(prev);
    buttonPanel.add(next);
    panel.add(buttonPanel, BorderLayout.SOUTH);
    panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
    panel.add(scrollPane, BorderLayout.CENTER);
    getContentPane().add(panel);}

ActionListener:

public void actionPerformed(ActionEvent e) {
 if (e.getActionCommand() == "next") {
     Rectangle rect = scrollPane.getVisibleRect();
     JScrollBar  bar = scrollPane.getVerticalScrollBar();
     int blockIncr = scrollPane.getViewport().getViewRect().height;
     bar.setValue(bar.getValue() + blockIncr);
     scrollPane.scrollRectToVisible(rect);
 }
 if (e.getActionCommand() == "prev") {
     Rectangle rect = scrollPane.getVisibleRect();
     JScrollBar  bar = scrollPane.getVerticalScrollBar();
     int blockIncr = scrollPane.getViewport().getViewRect().height;
     bar.setValue(bar.getValue() - blockIncr);
     scrollPane.scrollRectToVisible(rect);
 }}

Here is the function that store data into an array:

int i=0;
public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
{
    data[i][0]=IP;
    data[i][1]=PC_NAME;
    data[i][2]=ttl;
    data[i][3]=gw;
    i++;
    table.repaint();
    scrollPane.repaint();

}

Edit 1:

I tried with DefaultTableModel and still no luck. Here is code that I used for updating table. Constructor code didn't changed. Declaration:

private static final long serialVersionUID = 1L;
 String[][] data = new String[100][4];

 String[] columnNames = new String[]{
         "IP", "PC_NAME", "ttl", "db"};



            DefaultTableModel model = new DefaultTableModel(data,columnNames);
    JTable table =  new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);

Here is function that updated table:

 public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
{

    System.out.println(IP);
    model.setValueAt(IP, i, 0);
    model.setValueAt(PC_NAME, i, 1);
    model.setValueAt(ttl, i, 2);
    model.setValueAt(gw, i, 3);
    i++;
    model.fireTableDataChanged();
    table.repaint();
    scrollPane.repaint();

}

Upvotes: 3

Views: 1197

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your problem is that your comparing Strings with ==. Use the equals(...) method instead.

  • == compares if two variables refer to the same object -- something you don't care about. Thus two String variables can hold the same word but not be deemed equal by this operation.
  • the equals(...) or equalsIgnoreCase(...) method checks if two String variables hold the same chars in the same order, and that you do care about.

So not:

if (e.getActionCommand() == "next") {
  //  ...
}

but rather:

if ("next".equals(e.getActionCommand())) {
  //  ...
}

or if you don't care about case:

if ("next".equalsIgnoreCase(e.getActionCommand())) {
  //  ...
}

Edit 1
Next, once your JTable has hold of the data, changing the data will likely have no effect on the JTable. So here you change the data array:

int i=0;
public void WriteMonitorData (String IP, String PC_NAME, String ttl, String gw)
{
    data[i][0]=IP;
    data[i][1]=PC_NAME;
    data[i][2]=ttl;
    data[i][3]=gw;
    i++;
    table.repaint();
    scrollPane.repaint();

}

But this array has already been passed into the JTable, is now part of its model and has been changed into a Vector in all likelihood.

To see a change in your JTable's representation of the data, you must change the data held by the table's model. I suggest that you use a DefaultTableModel to hold your data in the JTable and then in your method above (which should begin with a lower case letter), you change the data held by the model.

Also, regardless if your if blocks work or not, don't use == to compare Strings as this will bite you, if not now, soon.

Upvotes: 3

Related Questions