[英]JTable Will Not Refresh
我正在尝试构建一个显示一些用于矢量路由的数据的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方法中仅包含一条打印语句,但是我不确定该放在哪里,这就是我的问题所在。
有人可以指出我的正确方向,以便我更改时表格中显示的数据实际更新时该位置吗?
谢谢!
看看发生了什么。 turnOnGUI()
创建一个没有引用的框架(并设置为可见)。
new DistanceVectorRoutingUI().setVisible(true);
然后在另一个类中,创建另一个框架( gui
)。 但是不要将其设置为可见。
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
您有两个框架实例。 gui
从未显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.