'Make JTable cells in specific column scrollable

I'm creating GUI in java using swing. I use JTable that looks like this.My table

I need to make cells of last column("Popis") to be scrollable as there will be description so String containing this description has variable lenght for each row. This is how I created my JTable.

this.tableModel = new DefaultTableModel(
                new Object[][] { },
                new String[] {"ID", "Nazov", "Naplnena kapacita / kapacita", "Popis"}
        );
this.table1.setModel(this.tableModel);

I add data to table using this.tablemodel.addRow(); Is there a way to make cells of last column of my table scrollable?



Solution 1:[1]

It can be solved by creating private class that extends AbstractCellEditor and implements TableCellEditor. Then create and customize the scrollbar. Here is example.

public class MainForm {
    private JTable table1;
    private JPanel panel1;
    private JTextArea text = new JTextArea();

    public MainForm() {
        text.setEditable(false);
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{}, new String[]{"Meno", "Priezvisko", "Datum", "Popis"});
        table1.setModel(tableModel);
        table1.setRowHeight(40);
        tableModel.addRow(new Object[]{"Jaimi", "Parker", "2022", "fdjfadufaouifasoifjasifhasiofa \nasdasdasdasdasdasdas \nasdasdasdasdasdasd\nasdasdasdasd "});
        tableModel.addRow(new Object[]{"William", "Mcdonald", "2021", "fdjfadufaouasfadfdsfafifasoifjasifhasiofa"});
        tableModel.addRow(new Object[]{"Matt", "Ashley", "2020", "asfasfafdsfgdafgdfgasgsdg"});
        tableModel.addRow(new Object[]{"Ariana", "Burns", "2019", "fdjfadufaouifasfdsgdgagsgsdgsdgsdagsdgsdgsdgsagsdgoifjasifhasiofa"});

        TableColumn column = table1.getColumn("Popis");
        column.setCellEditor(new HScrollableTCE());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MainForm");
        frame.setContentPane(new MainForm().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private class HScrollableTCE extends AbstractCellEditor implements TableCellEditor
    {
        JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex)
        {
            if (value != null) {
                scroll.setHorizontalScrollBar(scroll.createHorizontalScrollBar());
                scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 5));

                scroll.setBorder(new EmptyBorder(0,0,0,0));
                scroll.setToolTipText(value.toString());
                text.setText(value.toString());
                return scroll;
            }
            return null;
        }
        public Object getCellEditorValue()
        {
            return text.getText();
        }
    }

}

Solution 2:[2]

It would be difficult and awkward to embed a scrolling component inside a JTable. A common approach to showing long cell values is to make the cell’s tooltip (hover text) display the full value:

table1.setModel(tableModel);

table1.getColumnModel().getColumn(3).setCellRenderer(
    new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table,
                                                       Object value,
                                                       boolean selected,
                                                       boolean focused,
                                                       int row,
                                                       int column) {

            Component component = super.getTableCellRendererComponent(
                table, value, selected, focused, row, column);

            if (component instanceof JComponent) {
                ((JComponent) component).setToolTipText(
                    Objects.toString(value, null));
            }

            return component;
        }
    });

(Objects is the java.util.Objects class.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marek
Solution 2 VGR