简体   繁体   中英

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 ve come that far, that the PropertyChangeEvent is sent and received with the right data. rowData and columnName have the new and updated data. 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 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.

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:

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. These variable are just sitting in memory and have nothing to do with the JTable. When you create the JTable the data from those two variables are copied to the TableModel used by the JTable.

One approach is to update the TableModel directly. Then the TableModel with invoke the appropriate fireXXX method to make sure the table gets updated. You can use the setDataVector(...) method of the DefaultTableModel to reset the data in the existing TableModel. Or you can use the setRowCount(0) method of the DefaultTableModel to clear the model. Then you use the addRow(...) method to add the new rows.

Or your other option is to create a new TableModel and add the model to the table.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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