单击JTable的一行中的按钮可更改所有表记录中的标签

时间:2018-12-16 09:38:04

标签: java swing listview jtable

我尝试用按钮,edittext字段和其他Swing组件制作JPanel列表,但找不到在swing中制作列表视图的示例。我尝试使用jTable来做到这一点,该jTable在单元格中具有一列JPanel,但是单击按钮后,所有jPanel元素中的标签字段都会更改

这是我从不同示例中编写的代码。此类具有render和editor方法。单击按钮时,我尝试更改标签的文本。但是所有标签(我只想更改一个需要的标签)都会更改其文本...可能是我将onMouseListener设置在了那个地方...

public class RssFeedCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
    JPanel panel;
    JLabel text;
    JButton showButton;
    JLabel label;

    RssFeed feed;

    public RssFeedCell() {
        label = new JLabel();
        text = new JLabel();
        showButton = new JButton("View Articles");
        showButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                label.setText("ed");
            }
        });

        panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(text);
        panel.add(showButton);
        panel.add(label);
    }

    private void updateData(RssFeed feed, boolean isSelected, JTable table) {
        this.feed = feed;

        text.setText("" + feed.name + "" + feed.url + "Articles " + feed.articles.length + "");

        if (isSelected) {
            panel.setBackground(table.getSelectionBackground());
        } else {
            panel.setBackground(table.getBackground());
        }
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        RssFeed feed = (RssFeed) value;
        updateData(feed, true, table);
        return panel;
    }

    public Object getCellEditorValue() {
        return null;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column) {
        RssFeed feed = (RssFeed) value;
        updateData(feed, isSelected, table);
        return panel;
    }
}

1 个答案:

答案 0 :(得分:0)

只有一个label。您需要将单元格状态合并到TableModel数据中。