JBTable ColumnInfo with button

320 views Asked by At

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?

2

There are 2 answers

0
Tobias Otto On BEST ANSWER
0
camickr On

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.