如何在Swing java中的JTable的一行中添加按钮

发布于 2024-08-05 23:19:54 字数 68 浏览 3 评论 0原文

我制作了一个 swing GUI,其中包含带有一些行和列的 JTable。我应该如何向 JTable 中的行添加一个按钮?

I have made one swing GUI which have JTable with some rows and Columns.How should I add a button to row in a JTable ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-08-12 23:19:54

您不将其添加到行中 - 而是将其添加到单元格中。 本教程描述了您需要的内容。

You don't add it to a row - you add it to the cell. This tutorial describes what you need.

你的笑 2024-08-12 23:19:54

您可以将组件添加为表格单元格。

首先,您应该实现一个以 JButton 作为其父类和两个接口的类:TableCellRendererTableCellEditor

它应该实现TableCellEditor的原因是为了接收按钮的ActionEvent

    public class TableButton extends JButton implements TableCellRenderer, TableCellEditor {
      private int selectedRow;
      private int selectedColumn;
      Vector<TableButtonListener> listener;
    
      public TableButton(String text) {
        super(text); 
        listener = new Vector<TableButtonListener>();
        addActionListener(new ActionListener() { 
          public void actionPerformed( ActionEvent e ) { 
            for(TableButtonListener l : listener) { 
              l.tableButtonClicked(selectedRow, selectedColumn);
            }
          }
        });
      }
     
      public void addTableButtonListener( TableButtonListener l ) {
        listener.add(l);
      }
    
      public void removeTableButtonListener( TableButtonListener l ) { 
        listener.remove(l);
      }
    
      @Override 
      public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        return this;
      }
    
      @Override
      public Component getTableCellEditorComponent(JTable table,
          Object value, boolean isSelected, int row, int col) {
        selectedRow = row;
        selectedColumn = col;
        return this;
      } 
    
      @Override
      public void addCellEditorListener(CellEditorListener arg0) {      
      } 
    
      @Override
      public void cancelCellEditing() {
      } 
    
      @Override
      public Object getCellEditorValue() {
        return "";
      }
    
      @Override
      public boolean isCellEditable(EventObject arg0) {
        return true;
      }
    
      @Override
      public void removeCellEditorListener(CellEditorListener arg0) {
      }
    
      @Override
      public boolean shouldSelectCell(EventObject arg0) {
        return true;
      }
    
      @Override
      public boolean stopCellEditing() {
        return true;
      }
    }

然后我添加了一个名为 TableButtonListener` 的 EventListener 用于处理按钮事件,如下所示。

    public interface TableButtonListener extends EventListener {
      public void tableButtonClicked( int row, int col );
    }

并使用上面的渲染器/编辑器。

    TableButton buttonEditor = new TableButton("Button");
    buttonEditor.addButtonListener(new TableButtonListener() {
      @Override
      public void tableButtonClicked(int row, int col) {
        // do something 
      }     
    }); 
     
    TableColumn col = new TableColumn(1, 80);
    col.setCellRenderer(buttonEditor);
    col.setCellEditor(buttonEditor);

    cols.addColumn(colPattern);

如果要为每一行显示不同的按钮标签,则应在 getTableCellRendererComponentgetTableCellEditorComponent 方法中插入代码块来修改按钮的标签。

You can add Component as a table cell.

First of all, you should implement a class that has JButton as its parent class and two interfaces: TableCellRenderer and TableCellEditor.

The reason that it should implement TableCellEditor is for receiving button's ActionEvent.

    public class TableButton extends JButton implements TableCellRenderer, TableCellEditor {
      private int selectedRow;
      private int selectedColumn;
      Vector<TableButtonListener> listener;
    
      public TableButton(String text) {
        super(text); 
        listener = new Vector<TableButtonListener>();
        addActionListener(new ActionListener() { 
          public void actionPerformed( ActionEvent e ) { 
            for(TableButtonListener l : listener) { 
              l.tableButtonClicked(selectedRow, selectedColumn);
            }
          }
        });
      }
     
      public void addTableButtonListener( TableButtonListener l ) {
        listener.add(l);
      }
    
      public void removeTableButtonListener( TableButtonListener l ) { 
        listener.remove(l);
      }
    
      @Override 
      public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        return this;
      }
    
      @Override
      public Component getTableCellEditorComponent(JTable table,
          Object value, boolean isSelected, int row, int col) {
        selectedRow = row;
        selectedColumn = col;
        return this;
      } 
    
      @Override
      public void addCellEditorListener(CellEditorListener arg0) {      
      } 
    
      @Override
      public void cancelCellEditing() {
      } 
    
      @Override
      public Object getCellEditorValue() {
        return "";
      }
    
      @Override
      public boolean isCellEditable(EventObject arg0) {
        return true;
      }
    
      @Override
      public void removeCellEditorListener(CellEditorListener arg0) {
      }
    
      @Override
      public boolean shouldSelectCell(EventObject arg0) {
        return true;
      }
    
      @Override
      public boolean stopCellEditing() {
        return true;
      }
    }

Then I added an EventListener namedTableButtonListener` for handling button event as follows.

    public interface TableButtonListener extends EventListener {
      public void tableButtonClicked( int row, int col );
    }

And use above Renderer/Editor.

    TableButton buttonEditor = new TableButton("Button");
    buttonEditor.addButtonListener(new TableButtonListener() {
      @Override
      public void tableButtonClicked(int row, int col) {
        // do something 
      }     
    }); 
     
    TableColumn col = new TableColumn(1, 80);
    col.setCellRenderer(buttonEditor);
    col.setCellEditor(buttonEditor);

    cols.addColumn(colPattern);

If you want to display different buttons label for each row, you should insert a code block into the getTableCellRendererComponent and getTableCellEditorComponent methods to modify button's label.

×纯※雪 2024-08-12 23:19:54

请查看表格按钮列

它演示了如何使用 JButton 作为自定义渲染器和编辑器,您可以单击并轻松调用 Action

Check out Table Button Column.

It demonstrates how to use a JButton as a custom renderer and editor that you can click an easily invoke an Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文