Note that there are some explanatory texts on larger screens.

plurals
  1. POJtable/JScrollPanel won't refresh (update data)
    primarykey
    data
    text
    <p>I have setup a <code>JTable</code> 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!</p> <pre><code>String[][] data = new String[100][4]; String[] columnNames = new String[]{ "IP", "PC_NAME", "ttl", "db"}; </code></pre> <p>Constructor:</p> <pre><code>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);} </code></pre> <p>ActionListener:</p> <pre><code>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); }} </code></pre> <p>Here is the function that store data into an array:</p> <pre><code>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(); } </code></pre> <p><strong>Edit 1:</strong></p> <p>I tried with DefaultTableModel and still no luck. Here is code that I used for updating table. Constructor code didn't changed. Declaration:</p> <pre><code>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); </code></pre> <p>Here is function that updated table:</p> <pre><code> 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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload