【问题标题】:JTable doesn`t update on repaint() via PropertyChangeListenerJTable 不会通过 PropertyChangeListener 更新 repaint()
【发布时间】:2015-01-11 21:14:16
【问题描述】:

m programming a user client for a mysql database which reads data from the database and displays it in a JTable. Ive 已经走了这么远,PropertyChangeEvent 与正确的数据一起发送和接收。 rowData 和 columnName 具有新的和更新的数据。如果创建了一个空表以在程序开始时显示。虽然我有正确的数据并且我调用了 repaint() 它确实t update the JTable. Ive 搜索了网络和 stackoverflow 好几个小时,但没有找到适合我的问题的答案。

代码如下:

public class SuchenPanel extends JPanel implements PropertyChangeListener{

protected SuchenComboBoxControl comboControl = new SuchenComboBoxControl();
protected String[][] rowData = StringTools.makeRowData(comboControl.getCurrentRs()); 
protected String[] columnName = StringTools.makeColumnName(comboControl.getCurrentRs());
protected JTable tableView = new JTable(rowData,columnName);
protected JScrollPane scroll = new JScrollPane(tableView);


public SuchenPanel(){

    comboControl.addPropertyChangeListener(this);

    setLayout(new BorderLayout());
    JPanel north = new JPanel();
    north.setLayout(new GridLayout(0,3,6,3));

    JComboBox<Object> tableBox= new JComboBox<Object>(TablesColoumns.TABLES);
    tableBox.setActionCommand(SuchenCommand.TABLE);
    tableBox.addActionListener(comboControl);
    north.add(new JLabel("In welcher Tabelle wollen Sie suchen?"));
    north.add(tableBox);
    add(north,BorderLayout.NORTH);
    add(scroll,BorderLayout.CENTER);
}


@Override
public void propertyChange(PropertyChangeEvent e) {
    String propertyName = e.getPropertyName();
    if(propertyName.equals(PropertyChangeCommands.tableUPDATE)){
        this.rowData = StringTools.makeRowData((ResultSet)e.getNewValue());
        this.columnName = StringTools.makeColumnName((ResultSet) e.getNewValue());
        repaint();

    }

}

还有firePropertyChangeEvent:

public class SuchenComboBoxControl implements ActionListener{

protected ResultSet currentRs=null;
private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);


public SuchenComboBoxControl(){

}


@Override
public void actionPerformed(ActionEvent e) {
    JComboBox<?> cb = (JComboBox<?>)e.getSource();
    String command = e.getActionCommand();

    switch(command){
    case SuchenCommand.TABLE:
        String tablename = (String)cb.getSelectedItem();
        ResultSet execRs=MysqlConnection.getResultFromStatement("select * from "+StringTools.concatForExecute(tablename));
        this.pcs.firePropertyChange(PropertyChangeCommands.tableUPDATE, currentRs, execRs);
        this.currentRs=execRs;          
    }
}

public void addPropertyChangeListener (PropertyChangeListener listener){
    this.pcs.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener){
    this.pcs.removePropertyChangeListener(listener);
}

如果需要,您可以在full Code查看整个代码

【问题讨论】:

    标签: java mysql swing jtable


    【解决方案1】:

    您的代码正在更新 rowData 和 columnName 变量。这些变量只存在于内存中,与 JTable 无关。当您创建 JTable 时,来自这两个变量的数据将被复制到 JTable 使用的 TableModel。

    一种方法是直接更新TableModel。然后 TableModel 调用适当的 fireXXX 方法以确保表得到更新。您可以使用DefaultTableModelsetDataVector(...) 方法来重置现有TableModel 中的数据。或者您可以使用DefaultTableModelsetRowCount(0) 方法清除模型。然后使用addRow(...) 方法添加新行。

    或者您的其他选择是创建一个新的 TableModel 并将模型添加到表中。

    this.rowData = StringTools.makeRowData((ResultSet)e.getNewValue());
    this.columnName = StringTools.makeColumnName((ResultSet) e.getNewValue());
    table.setModel( new DefaultTableModel(rowDate, columnName);
    

    不需要 repaint() 或任何东西。

    【讨论】:

    • 好的,首先谢谢你,这是解决我问题的有效方法。更新 tableModel 还是设置新模型效率更高?
    • 创建表时从TableModel中获取的信息用于创建表的TableColumnManager和TableColumns。您还可以将自定义渲染器和编辑器添加到表中。如果您创建一个新的 TableModel(或刷新数据向量),那么所有这些都必须由表再次完成。如果您更改结构(列和标题),则需要这样做,但如果您只是刷新数据,那么您应该删除数据并重新添加新数据。
    • 好吧,看来我对 JTable 的概念有误。你帮了我很多。
    【解决方案2】:

    您在((DefaultTableModel)[JTable].getModel()) 上尝试过fireTableDataChanged() 吗?

    【讨论】:

    • 不,据我所知,fireTableDataChanged() 与更新我的值并重新绘制它完全相同。如果我错了,请纠正我。你会在哪里使用它?
    • 而不是repaint()。在[DefaultTableModel].setValueOf((Object)value, (int)row, (int)col))之后。
    • 触发事件是模型的责任,绝不应该在外部触发
    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2016-09-01
    相关资源
    最近更新 更多