Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a JScrollPane component to a JTable column

I'm trying to add scrolling capabilities to a certain column in my JTable. I've implemented a custom TableCellRenderer component and I can see the scroll pane inside the table just fine, but I am not able to scroll it. I've tried implementing TableCellEditor as well and didn't have any luck.

    public Component getTableCellEditorComponent(JTable arg0, Object arg1,
        boolean arg2, int arg3, int arg4) {
    return scrollPane;
}

Does anyone have any ideas how to make those cells which contain a scrollPane scrollable?

like image 526
Lithium Avatar asked May 17 '26 10:05

Lithium


2 Answers

With TableCellRenderer it's not possible to add any scrolling behaviour, as it does not receive any events and only draws the component. It is possible - however - to accomplish this by using a custom TableCellEditor with getTableCellEditor being:

public Component getTableCellEditorComponent(JTable table, Object value, boolean   isSelected, int row, int column) {
    JTextArea area = new JTextArea();
    area.setLineWrap(true);
    area.setText((String) value);

    JScrollPane pane = new JScrollPane(area);

    return pane;
}

Additionally, you have to control the editing behaviour of your CellEditor. To make the cell editable and scrollable always, isCellEditable should look like this:

public boolean isCellEditable(EventObject anEvent) {
    return true;
}

Personally, I find this solution to be more of a hack than anything, though. Also, this should only be for testing. You really do have to implement a better editing behaviour in my opinion.

like image 63
vehk Avatar answered May 19 '26 00:05

vehk


A Renderer just paints the cells. I believe you need to implement a TableCellEditor to scroll.

like image 24
jzd Avatar answered May 19 '26 00:05

jzd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!