Scrollable cells in JTable

I have a Jtable in which I have to show some big data. I can not increase the size of the cells. Therefore, I need to add a scroll bar to each cell of the table through which I can scroll through the text of the cells.

I tried to add 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 can see the scroll bar on the cells, but I can not click and scroll them.

Any suggestions on this would be great. Thanks at Advance.

+4
source share
1 answer

Adding JScrollPane and placing JScrollPane in JScrollPane solved the problem. Therefore, I would like to share it with all of you.

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

Source: https://habr.com/ru/post/1397804/


All Articles