我正在尝试构建一个GUI,显示一些用于矢量路由的数据,但是我没有尝试获取表中的信息以在物理表上更新。只有在按下按钮时才会从程序中更改数据(无用户交互)。
这是我的驱动程序类:
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();
}
}
这是我初始化并添加一个监听器的地方:
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);
}
});
}
这是MyTableModel类:
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();
}
}
每当我调用setValueAt()时,我都会在main方法中执行,一切正常(单元格被更新并调用tableChanged)。
我知道我不应该在tableChanged方法中只有一个print语句,但我不确定该放在那里,我认为这就是我的问题所在。
有人能指出我正确的方向,以便在我更改表格中显示的数据时实际更新吗?
谢谢!
答案 0 :(得分:4)
看看发生了什么。 turnOnGUI()
创建一个框架(并设置可见),没有参考。
new DistanceVectorRoutingUI().setVisible(true);
然后在另一个类中创建另一个帧(gui
)。但是不要把它看得见。
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
你有两个框架实例。永远不会显示gui
。