我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已经搜索了好几个小时的web和堆栈溢出,没有找到合适的答案来解决我的问题。
以下是代码:
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);
}如果需要,您可以在全码上查看整个代码
发布于 2015-01-11 21:31:23
您的代码正在更新rowData和columnName变量。这些变量只是停留在内存中,与JTable无关。创建JTable时,来自这两个变量的数据被复制到JTable使用的TableModel中。
一种方法是直接更新TableModel。然后调用适当的TableModel方法,以确保表得到更新。可以使用DefaultTableModel的setDataVector(...)方法重置现有TableModel中的数据。或者您可以使用setRowCount(0)方法的DefaultTableModel来清除模型。然后使用addRow(...)方法添加新行。
或者您的另一个选项是创建一个新的TableModel并将模型添加到表中。
this.rowData = StringTools.makeRowData((ResultSet)e.getNewValue());
this.columnName = StringTools.makeColumnName((ResultSet) e.getNewValue());
table.setModel( new DefaultTableModel(rowDate, columnName);不需要重新油漆()或任何东西。
发布于 2015-01-11 21:21:38
你在((DefaultTableModel)[JTable].getModel())上试过((DefaultTableModel)[JTable].getModel())吗?
https://stackoverflow.com/questions/27892183
复制相似问题