且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JTable中的可滚动单元格

更新时间:2023-12-03 11:56:58

添加 JScrollPane 和将 JLabel 放在 JScrollPane 中解决了这个问题。所以我想与大家分享。

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();
  }
}