简体   繁体   English

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

[英]How to add button in a row of JTable in Swing java

我制作了一个带有一些行和列的 JTable 的摆动 GUI。我应该如何向 JTable 中的行添加一个按钮?

You don't add it to a row - you add it to the cell.您不会将其添加到行中 - 您将其添加到单元格中。 This tutorial describes what you need. 本教程描述了您需要什么。

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 .首先,您应该实现一个以JButton作为其父类和两个接口的类: TableCellRendererTableCellEditor

The reason that it should implement TableCellEditor is for receiving button's ActionEvent .它应该实现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;
      }
    }

Then I added an EventListener named TableButtonListener` for handling button event as follows.然后我添加了一个EventListener named TableButtonListener` 的EventListener named来处理按钮事件,如下所示。

    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.如果你想为每一行显示不同的按钮标签,你应该在getTableCellRendererComponentgetTableCellEditorComponent方法中插入一个代码块来修改按钮的标签。

表按钮列给出了一种方法。

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 .它演示了如何使用 JButton 作为自定义渲染器和编辑器,您可以单击并轻松调用Action

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM