mangusbrother
mangusbrother

Reputation: 4154

JBTable ColumnInfo with button

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

Answers (2)

camickr
camickr

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

Related Questions