Reputation: 195
I'm trying to build a GUI that displays some data for vector routing and what not, but I'm stuck trying to get the information in the table to update on the physical table. Data is only changed from within the program when a button is pushed (no user interaction).
Here is my driver class:
public class DistanceVectorRouting {
public static void main(String[] args) {
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
System.out.println(gui.routerTable1.getValueAt(0, 1));
gui.routerTable1.setValueAt("foo", 0, 1);
System.out.println(gui.routerTable1.getValueAt(0, 1));
//fireTableDataChanged();
}
}
Here is where I initialize and add a listener:
public class DistanceVectorRoutingUI extends JFrame {
/**
* Creates new form DistanceVectorRoutingUI
*/
public DistanceVectorRoutingUI() {
initComponents();
}
private void initComponents() {
routerTable1 = new javax.swing.JTable();
routerTable1.setModel(new MyTableModel());
routerTable1.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
System.out.println(e);
}
});
}
public static void turnOnGUI() {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DistanceVectorRoutingUI().setVisible(true);
}
});
}
Here is MyTableModel class:
public class MyTableModel extends AbstractTableModel implements TableModelListener {
private String[] columnNames = {"Destination", "Distance"};
private Object[][] data = {{"1", "1"},
{"2", "1"},
{"3", "1"},
{"4", "1"},
{"5", "1"},
{"6", "1"}};
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
@Override
public void setValueAt(Object value, int row, int col) {
this.data[row][col] = value;
System.out.println(this.data[row][col]);
fireTableDataChanged();
}
}
Whenever I invoke the setValueAt(), I do it in the main method and everything works (cells are updated and tableChanged is invoked).
I know I'm not supposed to have just a print statement in the tableChanged method, but I'm not sure what to put there and I think that's where my issue is.
Could someone point me in the right direction to get this to where the data displayed in the table will actually update when I change it?
Thank you!
Upvotes: 1
Views: 53
Reputation: 208994
Look at what's happening. turnOnGUI()
creates a frame (and set visible), that has no reference.
new DistanceVectorRoutingUI().setVisible(true);
Then in the other class you create another frame (gui
). But don't set it visible.
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
You have two frame instances. gui
is never shown.
Upvotes: 4