Scrollable Cells in JTable

后端 未结 1 1882
既然无缘
既然无缘 2021-01-19 05:51

I have a Jtable in which I have to show some big data. I cann\'t increase the size of the Cells So I need to add a scrollbar in each cell of the table through which I can sc

1条回答
  •  被撕碎了的回忆
    2021-01-19 06:30

    Adding a JScrollPaneand placing the JLabel in the JScrollPane solved the issue. So I would like to share it with you all.

    private class ExtendedTableCellEditor extends AbstractCellEditor implements TableCellEditor
    {
      JLabel _component = new JLabel();
      JScrollPane _pane = new JScrollPane(_component, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
     /**
      * Returns the cell editor component.
      *
      */
      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex)
      {
        if (value == null) return null;
        _component.setText(value != null ? value.toString() : "");
        _component.setToolTipText(value != null ? value.toString() : "");
    
        _component.setOpaque(true);
        _component.setBackground((isSelected) ? Color.BLUE_DARK : Color.WHITE);
        _component.setForeground((isSelected) ? Color.WHITE : Color.BLACK);
    
        _pane.setHorizontalScrollBar(_pane.createHorizontalScrollBar()); 
        _pane.setVerticalScrollBar(_pane.createVerticalScrollBar());
        _pane.setBorder(new EmptyBorder(0,0,0,0));
        _pane.setToolTipText(value != null ? value.toString() : "");
        return _pane;
      }
      public Object getCellEditorValue()
      {
        return _component.getText();
      }
    }
    

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