0

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?

2
  • 1
    Where would you expect the scrollbar to be? There isn’t room for it inside the cell. One common approach to long text in a table cell is to create a renderer with a tooltip that contains the entire expanded text.
    – VGR
    May 11, 2022 at 17:15
  • @VGR could you provide me with example?
    – marek
    May 11, 2022 at 17:21

2 Answers 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.)

0

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();
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.