Java JTable将不会刷新
我试图构建一个GUI,显示一些用于向量路由的数据,但我一直在尝试获取表中的信息,以便在物理表上进行更新。只有在按下按钮(无用户交互)时,数据才会从程序中更改 这是我的驾驶课:Java JTable将不会刷新,java,swing,jtable,Java,Swing,Jtable,我试图构建一个GUI,显示一些用于向量路由的数据,但我一直在尝试获取表中的信息,以便在物理表上进行更新。只有在按下按钮(无用户交互)时,数据才会从程序中更改 这是我的驾驶课: public class DistanceVectorRouting { public static void main(String[] args) { DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI(); gu
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);
}
});
}
public class distancevectorroutinggui扩展JFrame{
/**
*创建新的表单距离VectorRoutinGUI
*/
公共距离VectorOutingGUI(){
初始化组件();
}
私有组件(){
routerTable1=newjavax.swing.JTable();
routerTable1.setModel(新的MyTableModel());
routerTable1.getModel().addTableModelListener(新的TableModelListener()){
公共作废表已更改(TableModelEvent e){
系统输出打印ln(e);
}
});
}
公共静态无效turnOnGUI(){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
java.util.logging.Logger.getLogger(DistanceVectorRoutingUI.class.getName()).log(java.util.logging.Level.SEVERE,null,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);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新的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();
}
}
公共类MyTableModel扩展AbstractTableModel实现TableModelListener{
私有字符串[]columnNames={“目的地”,“距离”};
私有对象[][]数据={{“1”,“1”},
{"2", "1"},
{"3", "1"},
{"4", "1"},
{"5", "1"},
{"6", "1"}};
@凌驾
public int getColumnCount(){
返回columnNames.length;
}
@凌驾
public int getRowCount(){
返回数据长度;
}
@凌驾
公共字符串getColumnName(int-col){
返回列名[col];
}
@凌驾
公共对象getValueAt(整数行,整数列){
返回数据[行][col];
}
@凌驾
公共类getColumnClass(int c){
返回getValueAt(0,c).getClass();
}
@凌驾
公共布尔值可编辑(int行,int列){
if(col<2){
返回false;
}否则{
返回true;
}
}
@凌驾
public void setValueAt(对象值、整行、整列){
此.data[row][col]=值;
System.out.println(此.data[row][col]);
fireTableDataChanged();
}
}
每当我调用setValueAt()时,我都会在main方法中调用它,一切都会正常工作(单元格被更新,tableChanged被调用)
我知道我不应该在tableChanged方法中只有一个print语句,但我不确定应该放在那里,我想这就是我的问题所在
有没有人能给我指出正确的方向,让我知道当我更改表时,表中显示的数据将实际更新到哪里
谢谢!看看发生了什么。
turnOnGUI()
创建了一个没有参考的框架(并设置为可见)
new DistanceVectorRoutingUI().setVisible(true);
然后在另一个类中创建另一个框架(gui
),但不要将其设置为可见
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
您有两个框架实例。
gui
从未显示。看看发生了什么。turnOnGUI()
创建一个没有引用的框架(并设置为可见)
new DistanceVectorRoutingUI().setVisible(true);
然后在另一个类中创建另一个框架(gui
),但不要将其设置为可见
DistanceVectorRoutingUI gui = new DistanceVectorRoutingUI();
gui.turnOnGUI();
您有两个框架实例。
gui
从未显示。“只有在按下按钮时才从程序中更改数据(没有用户交互)。-这是一个矛盾,不是吗?无论如何,这个“按钮”的代码在哪里?我也要小心执行返回getValueAt(0,c).getClass();
…如果列值为null
,会发生什么情况?@MadProgrammer我知道,我的意思是没有用户与表交互。除了让它显示外,我没有对按钮做任何操作。我的印象是,当表中的数据发生更改时,表可以更新。我只是用按钮来表示,“好的,去执行这个函数,它将更新表中的值。”如果这不是工作原理,请纠正我,turnOnGUI()
创建新的框架外观suspect@peeskillet它只被调用一次。然后所有内容都被添加到它中。这是因为它位于一个不会更改/更新的框架中吗?其中一些代码是使用NetBeans GUI Builder创建的。“只有在按下按钮时(无用户交互),程序中的数据才会更改。”。-这是一个矛盾,不是吗?不管怎样,这个“屁股”的代码在哪里