Reputation: 4154
I have a JBtable and in one of the columns I would like to insert a button.
My attempt was as follows:
private class DeleteColumn extends ColumnInfo<Field, JButton> {
public DeleteColumn() {
super("Delete");
}
@Nullable
@Override
public JButton valueOf(final Field field) {
final JButton removalButton = new JButton();
removalButton.setText("-");
removalButton.addActionListener((e) -> {
// do something
});
return removalButton;
}
@Override
public Class<?> getColumnClass() {
return JButton.class;
}
}
However when this is rendered, it still only displays the .toString() of the JButton. How can I display a button in the table?
Upvotes: 0
Views: 369
Reputation: 324147
I would like to insert a button
You should NOT be adding a JButton to the table. The data of the JTable should not be a Swing component. You should just be storing text in the column and then use a JButton to render the text.
For example check out Table Button Column for a class that allow you to use a button as the renderer/editor.
Upvotes: 1
Reputation: 1676
You should write a custom renderer. Please look at:
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender
There are also examples you could look at:
Upvotes: 2