How to Make a Jbutton in a Jtable Cell Click-Able

How to make a JButton in a JTable cell click-able?

I based my last example on the code provided by mKrobels answer to How to implement dynamic GUI in swing

The main difference between his and my example in the question is that he use DefaultTableModel and I use AbstractTableModel. His example does work, but not mine.

The solution I found was that I had to implement isCellEditable() in the TableModel, so with this method added, my example works:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}

Making a JButton clickable inside a JTable

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

Button in JTable cell not click-able

Thanks to @alex2410 for the solution

I had to make sure the cell was Editable

this can be done by either extending the Table upon declaration and overriding the isCellEditable(int row, int col): boolean method,
or in my case I overrode isCellEditable(EventObject e):boolean in the Cell Editor which I apply to the column,

hence the snippet within the Cell Editor I am using would be

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

This is as all cells to which the editor is applied need to be editable, as they are all buttons in my case.

Adding Jbutton to JTable

###Add button to JTable

JTable table = new JTable(new JTableModel()); 
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);

TableCellRenderer buttonRenderer = new JTableButtonRenderer();
table.getColumn("Button1").setCellRenderer(buttonRenderer);
table.getColumn("Button2").setCellRenderer(buttonRenderer);

###Sample JTableModel, This is manage the columns and rows, Setting components

public static class JTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private static final String[] COLUMN_NAMES = new String[] {"Id", "Stuff", "Button1", "Button2"};
private static final Class<?>[] COLUMN_TYPES = new Class<?>[] {Integer.class, String.class, JButton.class, JButton.class};

@Override public int getColumnCount() {
return COLUMN_NAMES.length;
}

@Override public int getRowCount() {
return 4;
}

@Override public String getColumnName(int columnIndex) {
return COLUMN_NAMES[columnIndex];
}

@Override public Class<?> getColumnClass(int columnIndex) {
return COLUMN_TYPES[columnIndex];
}

@Override public Object getValueAt(final int rowIndex, final int columnIndex) {
/*Adding components*/
switch (columnIndex) {
case 0: return rowIndex;
case 1: return "Text for "+rowIndex;
case 2: // fall through
/*Adding button and creating click listener*/
case 3: final JButton button = new JButton(COLUMN_NAMES[columnIndex]);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button),
"Button clicked for row "+rowIndex);
}
});
return button;
default: return "Error";
}
}
}

###Sample Button click listener , This manage the when mouse is clicked over the component

private static class JTableButtonMouseListener extends MouseAdapter {
private final JTable table;

public JTableButtonMouseListener(JTable table) {
this.table = table;
}

public void mouseClicked(MouseEvent e) {
int column = table.getColumnModel().getColumnIndexAtX(e.getX()); // get the coloum of the button
int row = e.getY()/table.getRowHeight(); //get the row of the button

/*Checking the row or column is valid or not*/
if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
Object value = table.getValueAt(row, column);
if (value instanceof JButton) {
/*perform a click event*/
((JButton)value).doClick();
}
}
}
}

###Sample JTable Cell Renderer, Managing the cell component

private static class JTableButtonRenderer implements TableCellRenderer {        
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JButton button = (JButton)value;
return button;
}
}

How to add buttons inside a cell of jtable and give it action

You need both a renderer and and editor, as shown in this example. See How to Use Tables: Editors and Renderers for details. Tangentially, you should override the method isCellEditable() in your TableModel rather than extending JTable.



Related Topics



Leave a reply



Submit