Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking a JButton clickable inside a JTable
    primarykey
    data
    text
    <p>Here is the screenshot of what I want to do :</p> <p><img src="https://i.stack.imgur.com/LwB40.png" alt="enter image description here"></p> <p>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 <code>Object</code> returned by <code>table.getValueAt()</code> is a String instead of a JButton...</p> <p>Here is the code :</p> <pre><code>tblResult = new JTable(data,cols) { public TableCellRenderer getCellRenderer( int row, int column ) { return new ClientsTableRenderer(); } }; </code></pre> <p>I use this for populating at run-time the JTable : (<code>tblResult</code> is now <code>Clients.rblResult</code>)</p> <pre><code>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&lt;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)); }} ); </code></pre> <p>Here the <code>ClientsTableRenderer</code> class</p> <pre><code>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 &lt; 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; } } </code></pre> <p>And finaly, the JTableButtonMouseListener() :</p> <pre><code>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 &lt; table.getRowCount() &amp;&amp; row &gt;= 0 &amp;&amp; column &lt; table.getColumnCount() &amp;&amp; column &gt;= 0) { Object value = table.getValueAt(row, column); System.out.println("Value :"+value.getClass().getName()); if (value instanceof JButton) { ((JButton)value).doClick(); } } } } </code></pre> <p>I'm kindly new to Java, help would be very much appreciated :)</p> <p>Thanks in advance !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload