简体   繁体   English

JTable不会通过PropertyChangeListener在repaint()上更新

[英]JTable doesn`t update on repaint() via PropertyChangeListener

I m programming a user client for a mysql database which reads data from the database and displays it in a JTable. I 我正在m programming a user client for a mysql database which reads data from the database and displays it in a JTable. I m programming a user client for a mysql database which reads data from the database and displays it in a JTable. I ve come that far, that the PropertyChangeEvent is sent and received with the right data. m programming a user client for a mysql database which reads data from the database and displays it in a JTable. I特地为你们那么远,那PropertyChangeEvent发送和正确的数据接收。 rowData and columnName have the new and updated data. rowData和columnName具有新的和更新的数据。 If created a empty table to show at start of the programm. 如果创建了一个空表以在程序启动时显示。 Although i have the right data and I call repaint() it does t update the JTable. I 尽管我拥有正确的数据,并且我调用repaint(),但它不会t update the JTable. I t update the JTable. I ve searched the web and stackoverflow for quite some hours now and didn`t find a fitting answer to my problem. t update the JTable. I已经在网上和stackoverflow上搜索了好几个小时,却找不到适合我问题的答案。

Here is the code: 这是代码:

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();

    }

}

and the firePropertyChangeEvent: 和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);
}

if needed you can view the whole code at full Code 如果需要,您可以查看完整的完整代码

Your code is updating the rowData and columnName variables. 您的代码正在更新rowData和columnName变量。 These variable are just sitting in memory and have nothing to do with the JTable. 这些变量仅位于内存中,与JTable无关。 When you create the JTable the data from those two variables are copied to the TableModel used by the JTable. 创建JTable时,来自这两个变量的数据将复制到JTable使用的TableModel中。

One approach is to update the TableModel directly. 一种方法是直接更新TableModel Then the TableModel with invoke the appropriate fireXXX method to make sure the table gets updated. 然后,通过调用适当的fireXXX方法来确保TableModel更新。 You can use the setDataVector(...) method of the DefaultTableModel to reset the data in the existing TableModel. 您可以使用DefaultTableModelsetDataVector(...)方法来重置现有TableModel中的数据。 Or you can use the setRowCount(0) method of the DefaultTableModel to clear the model. 或者,您可以使用DefaultTableModelsetRowCount(0)方法清除模型。 Then you use the addRow(...) method to add the new rows. 然后,使用addRow(...)方法添加新行。

Or your other option is to create a new TableModel and add the model to the table. 或者您的另一个选择是创建一个新的TableModel并将模型添加到表中。

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

No need for a repaint() or anything. 不需要repaint()或其他任何东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM