[英]JTable doesn`t update on repaint() via PropertyChangeListener
我正在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
特地為你們那么遠,那PropertyChangeEvent發送和正確的數據接收。 rowData和columnName具有新的和更新的數據。 如果創建了一個空表以在程序啟動時顯示。 盡管我擁有正確的數據,並且我調用repaint(),但它不會t update the JTable. I
t update the JTable. I
已經在網上和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);
}
如果需要,您可以查看完整的完整代碼
您的代碼正在更新rowData和columnName變量。 這些變量僅位於內存中,與JTable無關。 創建JTable時,來自這兩個變量的數據將復制到JTable使用的TableModel中。
一種方法是直接更新TableModel
。 然后,通過調用適當的fireXXX方法來確保TableModel更新。 您可以使用DefaultTableModel
的setDataVector(...)
方法來重置現有TableModel中的數據。 或者,您可以使用DefaultTableModel
的setRowCount(0)
方法清除模型。 然后,使用addRow(...)
方法添加新行。
或者您的另一個選擇是創建一個新的TableModel並將模型添加到表中。
this.rowData = StringTools.makeRowData((ResultSet)e.getNewValue());
this.columnName = StringTools.makeColumnName((ResultSet) e.getNewValue());
table.setModel( new DefaultTableModel(rowDate, columnName);
不需要repaint()或其他任何東西。
您是否在((DefaultTableModel)[JTable].getModel())
上嘗試了fireTableDataChanged()
((DefaultTableModel)[JTable].getModel())
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.