Swing I';我可以使用setValueAt()更新TableModel,但更改在表中不可见

Swing I';我可以使用setValueAt()更新TableModel,但更改在表中不可见,swing,jtable,tablemodel,Swing,Jtable,Tablemodel,我可以使用setValueAt()更新TableModel,但更改在表中不可见 代码如下: import javax.swing.JFrame; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableM

我可以使用setValueAt()更新TableModel,但更改在表中不可见 代码如下:

    import javax.swing.JFrame;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import java.awt.Dimension;
    import java.awt.GridLayout;

public class TableFTFEditDemo extends JPanel {
private boolean DEBUG = true;
JTable table;
MyTableModel tableModel=new MyTableModel();

public TableFTFEditDemo() {
    super(new GridLayout(1,0));

    table = new JTable(tableModel);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Set up stricter input validation for the integer column.
    table.setDefaultEditor(Integer.class,
                           new IntegerEditor(0, 100));

//If we didn't want this editor to be used for other
//Integer columns, we'd do this:
//table.getColumnModel().getColumn(3).setCellEditor(
//  new IntegerEditor(0, 100));

    //Add the scroll pane to this panel.
    add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
    private String[] columnNames ;
    private Object[][] data; 
    MyTableModel(){
    columnNames=new String[] {"First Name","Last Name","Sport","# of Seaters","Vegetarian","blank"};
    data=new Object[][] {
            {"Kathy", "Smith",
            "Snowboarding", new Integer(5), new Boolean(false),"sdf"},
            {"John", "Doe",
            "Rowing", new Integer(3), new Boolean(true),"reytry"},
            {"Sue", "Black",
            "Knitting", new Integer(2), new Boolean(false),"wwttu"},
            {"Jane", "White",
            "Speed reading", new Integer(20), new Boolean(true),"yuiyio"},
            {"Joe", "Brown",
            "Pool", new Integer(10), new Boolean(false),"ertey"}
    };
}
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

        data[row][col] = value;
        this.fireTableCellUpdated(row,col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableFTFEditDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    TableFTFEditDemo newContentPane = new TableFTFEditDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
void runtable(){

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
            tableModel.setValueAt(new Integer(5),2,5); 
     ((AbstractTableModel)table.getModel()).fireTableCellUpdated(2,5);
        }
    });
}
 /** using this to call setValueAt from other classes*/
public void setValueAt1(Object value, int row, int col) {
     table.setValueAt(new Integer(5),row,col); 
     ((AbstractTableModel)table.getModel()).fireTableCellUpdated(row,col);   
    }
 }
import javax.swing.JFrame;
导入javax.swing.JDialog;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.table.AbstractTableModel;
导入java.awt.Dimension;
导入java.awt.GridLayout;
公共类TableFTFEditDemo扩展了JPanel{
私有布尔调试=true;
JTable表;
MyTableModel tableModel=新的MyTableModel();
公共表ftfeditdemo(){
超级(新网格布局(1,0));
table=新的JTable(tableModel);
表.setPreferredScrollableViewportSize(新维度(500,70));
表.setFillsViewPerthweight(真);
//创建滚动窗格并将表添加到其中。
JScrollPane scrollPane=新的JScrollPane(表);
//为整数列设置更严格的输入验证。
table.setDefaultEditor(Integer.class、,
新的整数编辑器(01100);
//如果我们不想让这个编辑器用于其他
//整数列,我们可以这样做:
//表.getColumnModel().getColumn(3).setCellEditor(
//新的整数编辑器(01100);
//将滚动窗格添加到此面板。
添加(滚动窗格);
}
类MyTableModel扩展了AbstractTableModel{
私有字符串[]列名称;
私有对象[][]数据;
MyTableModel(){
columnNames=新字符串[]{“名字”、“姓氏”、“运动”、“座位”、“素食者”、“空白”};
数据=新对象[][]{
{“凯西”,“史密斯”,
“滑雪板”、新整数(5)、新布尔值(false)、“sdf”}、,
{“约翰”,“多伊”,
“Rowing”、新整数(3)、新布尔值(true)、“reytry”}、,
{“苏”,“黑”,
“编织”,新整数(2),新布尔值(false),“wwttu”},
{“简”、“白”,
“快速读取”、新整数(20)、新布尔值(true)、“yuiyio”}、,
{“乔”,“布朗”,
“Pool”、新整数(10)、新布尔值(false)、“ertey”}
};
}
public int getColumnCount(){
返回columnNames.length;
}
public int getRowCount(){
返回数据长度;
}
公共字符串getColumnName(int-col){
返回列名[col];
}
公共对象getValueAt(整数行,整数列){
返回数据[行][col];
}
/*
*JTable使用此方法确定默认渲染器/
*每个单元格的编辑器。如果我们没有实现此方法,
*最后一列将包含文本(“真”/“假”),
*而不是复选框。
*/
公共类getColumnClass(int c){
返回getValueAt(0,c).getClass();
}
公共布尔值可编辑(int行,int列){
//请注意,数据/单元地址是恒定的,
//无论单元格出现在屏幕上的何处。
if(col<2){
返回false;
}否则{
返回true;
}
}
public void setValueAt(对象值、整行、整列){
如果(调试){
System.out.println(“设置值在“+行+”,“+列
+“至”+价值
+“(一个例子)
+value.getClass()+”);
}
数据[行][列]=值;
此.firetablecell已更新(行、列);
如果(调试){
System.out.println(“数据的新值:”);
printDebugData();
}
}
私有void printDebugData(){
int numRows=getRowCount();
int numCols=getColumnCount();
对于(int i=0;i

PS:代码以frm oracle为例。我只想知道如何在表中反映tablemodel中所做的更改。

您的代码按预期工作,只是您实际上没有在您的模型上调用
setValueAt()
。您执行的runtable()方法永远不会被调用

要进行验证,请更改
createAndShowGUI()
以返回
newContentPane
,并使用以下主菜单:

    static TableFTFEditDemo instance;

    public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            instance = createAndShowGUI();
        }
    });
    Thread.sleep(5000);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            instance.tableModel.setValueAt(5, 2, 5);
        }
    });
}


编程的5秒延迟后,第2列第5行中的值将从“wwtu”变为“5”。

Thanx用于应答。实际上,我会使用其他类别的setValueAt1()