4

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 scroll the text of cells.

I have tried to add a Custom Cell Renderer

private class ExtendedTableCellEditor extends AbstractCellEditor implements TableCellEditor
{
    JLabel area = new JLabel();
    String text;

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex)
    { 
        area.setText(text);
        return new JScrollPane(area);
        }
            public Object getCellEditorValue()
        {
        return text;
    }
}

Now I am able to see the Scroll bar on the cells but not able to click and scroll them.

Any suggestions to this issue will be great. Thanks in Advance.

DIF
  • 2,470
  • 6
  • 35
  • 49
Ram Dutt Shukla
  • 1,351
  • 7
  • 28
  • 55

1 Answers1

5

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();
  }
}
Ram Dutt Shukla
  • 1,351
  • 7
  • 28
  • 55