Adding a JScrollPane component to a JTable column

后端 未结 3 654
北荒
北荒 2021-01-20 23:52

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 tabl

相关标签:
3条回答
  • 2021-01-21 00:25

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

    0 讨论(0)
  • 2021-01-21 00:25

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

    0 讨论(0)
  • 2021-01-21 00:40

    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.

    0 讨论(0)
提交回复
热议问题