首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTable不通过PropertyChangeListener更新repaint()

JTable不通过PropertyChangeListener更新repaint()
EN

Stack Overflow用户
提问于 2015-01-11 21:14:16
回答 2查看 214关注 0票数 0

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和堆栈溢出,没有找到合适的答案来解决我的问题。

以下是代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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);
}

如果需要,您可以在全码上查看整个代码

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-11 21:31:23

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

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

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

代码语言:javascript
复制
this.rowData = StringTools.makeRowData((ResultSet)e.getNewValue());
this.columnName = StringTools.makeColumnName((ResultSet) e.getNewValue());
table.setModel( new DefaultTableModel(rowDate, columnName);

不需要重新油漆()或任何东西。

票数 2
EN

Stack Overflow用户

发布于 2015-01-11 21:21:38

你在((DefaultTableModel)[JTable].getModel())上试过((DefaultTableModel)[JTable].getModel())吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27892183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档