简体   繁体   中英

JButton in JTable doesn't show

I'm trying to put a JButton in a JTable . It doesn't show. I did a lot of trying by what I found in the web, but nothing was working.

This is my code:

public  class ButtonRenderer extends JButton implements TableCellRenderer {

    JButton button;     
    boolean enabled = true; 

    public ButtonRenderer() 
    {     
        button = new JButton();     
    }



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


            if (table.getValueAt(row, column) == "clicked") {
                button.setEnabled(false);
            } else {

                button.setEnabled(true);
            }

            return button;
            }
      public void setEnabled(boolean enabled) 
        {     
            this.enabled = enabled;     
        }     
        }     

        class ButtonEditor extends AbstractCellEditor implements   
        TableCellEditor,ActionListener 
        {     
        JButton button;    
        private JTable cwwObjTblMainTable = null;
        boolean enabled = true;     
        int clickCountToStart = 1;     

        public ButtonEditor() 
        {     
            button = new JButton();     
            button.addActionListener(this);     
        }   

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

            cwwObjTblMainTable=table;
            button.setText("Voir");     
            button.setEnabled(enabled);     
            return button;     
        }     

        public void setEnabled(boolean enabled) {     
            this.enabled = enabled;     
        }     

        public Object getCellEditorValue() {     
            return button.getText();     
        }     

        public boolean isCellEditable(EventObject anEvent) {     
            if (anEvent instanceof MouseEvent) {     
                return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;     
            }     
            return true;     
        }     

        public void actionPerformed(ActionEvent e) 
        {  
            enabled=false;
            button.setEnabled(false);
            //Business logic execution 
            System.out.println("Clicked");

        }
    }

And the calling on the view:

 tableau.setDefaultRenderer(JButton.class, new ButtonRenderer());
 tableau.getColumn("Voir l'évenement").setCellEditor(new ButtonEditor());

How you have tried to use setDefaultRenderer() in below line is incorrect. First parameter of this method must be the "data type" of the column which intends to use this renderer; Not the type of the renderer.

tableau.setDefaultRenderer(JButton.class, new ButtonRenderer());

Below program demonstrates how you can add a JButton to a JTable .

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.table.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.EventObject;

public class ButtonInTable {

  public static void main(String[] args) {

    DefaultTableModel tableModel = new DefaultTableModel(
        new Object[][] {
            {"aa", "aaa", ""},
            {"bb", "bbb", ""},
            {"cc", "ccc", ""},
            {"dd", "ddd", ""},
            {"ee", "eee", ""}},
        new Object[] {"Column 1", "Column 2", "Column 3"});

    JTable table = new JTable(tableModel);
    table.getColumn("Column 3").setCellRenderer(new RendererAndEditor());
    table.getColumn("Column 3").setCellEditor(new RendererAndEditor());

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(table));
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}

class RendererAndEditor implements TableCellRenderer, TableCellEditor {

  private JButton button;

  RendererAndEditor() {
    button = new JButton("Button");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Button clicked");
      }
    });
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                 boolean hasFocus, int row, int column) {
    return button;
  }

  @Override
  public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
                                                        int column) {
    return button;
  }

  @Override
  public Object getCellEditorValue() {
    return null;
  }

  @Override
  public boolean isCellEditable(EventObject anEvent) {
    return true;
  }

  @Override
  public boolean shouldSelectCell(EventObject anEvent) {
    return true;
  }

  @Override
  public boolean stopCellEditing() {
    return true;
  }

  @Override
  public void cancelCellEditing() { }

  @Override
  public void addCellEditorListener(CellEditorListener l) { }

  @Override
  public void removeCellEditorListener(CellEditorListener l) { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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