Adding a JScrollPane component to a JTable column

喜欢而已 提交于 2019-12-01 23:36:09

问题


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?


回答1:


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.




回答2:


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




回答3:


As an alternative, consider placing a single scroll pane in a separate container and updating it's view in your selection listener.



来源:https://stackoverflow.com/questions/6388453/adding-a-jscrollpane-component-to-a-jtable-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!