JTable中的可滚动单元格 [英] Scrollable Cells in JTable
问题描述
我有一个Jtable,我必须在其中显示一些大数据。我不能增加单元格的大小所以我需要在表格的每个单元格中添加一个滚动条,通过它我可以滚动单元格的文本。
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.
推荐答案
添加 JScrollPane
和将 JLabel
放在 JScrollPane
中解决了这个问题。所以我想与大家分享。
Adding a JScrollPane
and 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();
}
}
这篇关于JTable中的可滚动单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!