Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JButton clickable inside a JTable

Here is the screenshot of what I want to do :

enter image description here

What's happening there is the JButton shows correctly but nothing happens when I click on it. After some search, I've found that the Object returned by table.getValueAt() is a String instead of a JButton...

Here is the code :

tblResult = new JTable(data,cols) {     public TableCellRenderer getCellRenderer( int row, int column ) {         return new ClientsTableRenderer();     } }; 

I use this for populating at run-time the JTable : (tblResult is now Clients.rblResult)

SwingUtilities.invokeLater( new Runnable() {     public void run() {           DefaultTableModel aModel = new DefaultTableModel() {             //setting the jtable read only             @Override             public boolean isCellEditable(int row, int column) {                 return false;             }                        };       String[] cols = {"N°","Société", "TVA", "CP", "Ville", ""};     aModel.setColumnIdentifiers(cols);      Object[] temp = new Object[6];     for(int i=0;i<result.length;i++) {          temp[0] = result[i].custNumber;         temp[1] = result[i].name;         temp[2] = result[i].tva;         temp[3] = result[i].cp;         temp[4] = result[i].city;         temp[5] = "Consulter";          aModel.addRow(temp);      }      Clients.tblResult.setModel(aModel);      Clients.tblResult.addMouseListener(new JTableButtonMouseListener(Clients.tblResult));     }}   );  

Here the ClientsTableRenderer class

public class ClientsTableRenderer extends JPanel implements TableCellRenderer {     @Override     public Component getTableCellRendererComponent( final JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {         setBackground(Color.WHITE);         if(column < 5) {             JLabel label =  new JLabel(value.toString());             JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,0,9));             panel.setBackground(Color.WHITE);             panel.add(label);             this.add( panel);         } else {              JButton button = new JButton(value.toString());             button.addActionListener(new ActionListener() {                 @Override                 public void actionPerformed(ActionEvent arg0) {                     System.out.println("Clicked !");                 }             });             JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,0,3));             panel.setBackground(Color.WHITE);             panel.add(button);             this.add(panel);         }           return this;     }   } 

And finaly, the JTableButtonMouseListener() :

public class JTableButtonMouseListener extends MouseAdapter {       private final JTable table;        public JTableButtonMouseListener(JTable table) {         this.table = table;       }        @Override public void mouseClicked(MouseEvent e) {         int column = table.getColumnModel().getColumnIndexAtX(e.getX());         int row    = e.getY()/table.getRowHeight();          System.out.println("Col :"+column + "row:"+row);          if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {           Object value = table.getValueAt(row, column);           System.out.println("Value :"+value.getClass().getName());           if (value instanceof JButton) {             ((JButton)value).doClick();           }          }       }     } 

I'm kindly new to Java, help would be very much appreciated :)

Thanks in advance !

like image 989
noli Avatar asked Apr 27 '12 09:04

noli


People also ask

Can I add buttons in JTable?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.

How do you add a link to a JButton?

browse command saying "method browse(URI) in the type Desktop is not applicable for the arguments (String)" To fix the error create a function/method that will take a string parameter the call the above function in the event Listener Action performed button. Call the method like this.

How do you make a cell editable in JTable?

The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.


1 Answers

This Table Button Column from Rob Camick may fit your needs.

like image 194
jalopaba Avatar answered Sep 20 '22 01:09

jalopaba