【问题标题】:How to find the selected row in the JTable to remove it?如何在 JTable 中找到选定的行以将其删除?
【发布时间】:2015-04-10 02:52:44
【问题描述】:

我尝试创建一个JTable,其中有一列为JButton,用于删除选定的行。 但是我仍然不知道在按钮ActionListener 中添加什么来识别它的行并将其删除。

这是我的代码:

public class JavaApplication81 {
    JFrame frame;
    JPanel panel;
    JTable table;
    JScrollPane tableScroll = new JScrollPane();
    DefaultTableModel tableModel;
    public JavaApplication81(){
        frame = new JFrame("Frame");
        panel = new JPanel();

        String col[] = {" ", "File", "Remove"};
        tableModel = new DefaultTableModel(col,0);
        table = new JTable(){
            private static final long serialVersionUID = 1L;
            //Returning the Class of each column will allow different
            //renderes to be used based on class
            @Override
            public Class getColumnClass(int column){
                return getValueAt(0, column).getClass();
            }
        };
        table.setModel(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(400,200));
        tableScroll.setViewportView(table);

        Object[] data = {"icon", "file", "Remove"};
        tableModel.addRow(data);

        table.getColumn("Remove").setCellRenderer(new ButtonRenderer());
        table.getColumn("Remove").setCellEditor(new ButtonEditor(new JCheckBox()));

        panel.add(tableScroll);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new JavaApplication81();
    }

    ///////////////////////
    public class ButtonRenderer extends JButton implements TableCellRenderer {

      public ButtonRenderer() {
        setOpaque(true);
      }

      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {

        setText("Remove");
        return this;
      }
    }

    public class ButtonEditor extends DefaultCellEditor {
        protected JButton button;

        public ButtonEditor(JCheckBox checkBox) {
          super(checkBox);
          button = new JButton();
          button.setOpaque(true);
          button.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {

            }
          });
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                         boolean isSelected, int row, int column) {


          button.setText("Remove");
          return button;
        }
    }
}

有什么想法可以在按下每个按钮时删除它的行吗?

【问题讨论】:

  • JTable#getSelectedRow,您需要通过JTable#convertRowIndexToModel 来获取模型索引。从那里开始,一切都归结为模型,但DefaultTableModel#removeRow 应该可以工作
  • 对于它的价值,在每一行上放置一个“删除”按钮只是,嗯,80 年代的网络。最好使用键绑定、工具栏/菜单或按钮的组合,因为删除 100 行会很乏味,而额外的列可以用来更好地显示数据。对于exampleexample
  • 非常感谢。好吧,我有一张小桌子,所以在每一行都有一个自定义外观的按钮可以解决这个问题。第二个链接是我的答案。如果您再次在这里提交,我会选择它作为答案。但是我为那篇帖子 +1。
  • 无论如何,这几乎都是基于camickr的建议,所以你不妨接受他的回答,因为他们做同样的事情
  • @Dan, The second link was my answer. I would choose it as answer.. - 不知道您是否真的在使用我的解决方案,但我只想指出,“第二个链接”确实存在我注意到的一个问题。尝试在第一行上执行 mousePressed,然后将鼠标移离该列(以取消删除)。现在将鼠标移到不同的行(比如最后一行)并执行 mousePressed。即使在释放鼠标之前,该行也会被删除。我的解决方案避免了这种不必要的删除,因为它使用 MouseListener 在释放鼠标时停止对列进行编辑。

标签: java swing jtable actionlistener defaulttablemodel


【解决方案1】:

Table Button Column 显示了一种简单的方法。

它提供了渲染器/编辑器和获取行的简单方法。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 2012-07-23
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多