Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add button in a row of JTable in Swing java

Tags:

java

swing

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 ?

like image 631
om. Avatar asked Sep 25 '09 05:09

om.


People also ask

How can I tell if a row is selected in JTable?

So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.

How do I sort a row in JTable?

We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.

How add data from Jtextfield to JTable?

To add the data entered in the JTextFields you will need to register an ActionListener to your add button, in this case jButton1 . To add entries to your table model you could use a mutable model such as DefaultTableModel : DefaultTableModel model = new DefaultTableModel(data, columns);


2 Answers

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

like image 155
Bostone Avatar answered Oct 19 '22 10:10

Bostone


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 named TableButtonListener` 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.

like image 21
xrath Avatar answered Oct 19 '22 11:10

xrath