3

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();

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
ZhiZha
  • 143
  • 2
  • 4
  • 13
  • Please clean up your code indentation. All code on the same block should be uniformly indented (I like 3 spaces myself). If you don't do this, you are making it more difficult than it needs to be for a volunteer to review and understand your code and this really isn't fair to us. – Hovercraft Full Of Eels Sep 27 '12 at 22:23
  • Also please spend a little time and describe the details of your problem. We certainly can't run this code fragment and so if you don't post compilable code, you need to give us enough information for us to be able to make reasonable guesses as to the source of the problem. This will require information supplied by **you**. – Hovercraft Full Of Eels Sep 27 '12 at 22:24
  • I have other class that call function WriteMonitorData and pass 4 strings. When function WriteMonitorData is called I stored passed string to variable data that contains data for table. Problem is that table data doesn't refresh. I successfully store passed string to variable data but for some reason table rows are empty – ZhiZha Sep 27 '12 at 22:30
  • See edit to answer. Use a DefaultTableModel to hold the JTable's data, and edit its data. – Hovercraft Full Of Eels Sep 27 '12 at 22:32
  • I tried with DefaultTableModel and still no luck. Can you tell me what did I do wrong ? – ZhiZha Sep 27 '12 at 23:03
  • Not really since I can't run or compile your code. Consider putting in the effort to create and post an [sscce](http://sscce.org) (check out the link) as this would allow us to run your code without being drowned in unrelated code. – Hovercraft Full Of Eels Sep 28 '12 at 01:05

1 Answers1

3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373