JTable Cell Widgets

Widgets in JTable columns are JTable expected to be different from normal ones, right? Looks like a behavioral difference, take an example of the Swing documentation and move the mouse over the checkboxes in the vegetarian column ... They don't react at all. I understand that these are just surrogates for the widget, so backlighting must be done manually, so how can I fix this? I tried widget.requestFocusInWindow(); in mouseMoved() for a surrogate widget event handler without success. Any other workaround?

+4
source share
3 answers

You can create your own cell rendering that applies the rollover effect. Then add a mouse listener that tracks mouse movement and repaints the corresponding cells. You need to apply the effect to the current cell under the cursor and clear the previous cell.

The following is a brief example demonstrating this approach in the checkbox renderer. The example extends the default value of BooleanRenderer . The only change is getModel().setRollover(...) in getTableCellRendererComponent() .

 import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.plaf.UIResource; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; public class TableRolloverDemo { private static void createAndShowGUI() { JFrame frame = new JFrame("TableRolloverDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTable table = new JTable(); final DefaultTableModel model = new DefaultTableModel(new Object[][] { { false }, { false }, { true }, { true } }, new Object[] { "Column" }) { public Class<?> getColumnClass(int columnIndex) { return Boolean.class; } }; RolloverMouseAdapter rolloverAdapter = new RolloverMouseAdapter(table); RolloverBooleanRenderer renderer = new RolloverBooleanRenderer(rolloverAdapter); table.addMouseListener(rolloverAdapter); table.addMouseMotionListener(rolloverAdapter); table.setDefaultRenderer(Boolean.class, renderer); table.setModel(model); frame.add(new JScrollPane(table)); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } static class RolloverMouseAdapter extends MouseAdapter { private int row = -1; private int column = -1; private JTable table; public RolloverMouseAdapter(JTable table) { this.table = table; } public boolean isRolloverCell(int row, int column) { return this.row == row && this.column == column; } @Override public void mouseMoved(MouseEvent e) { int lastRow = row; int lastColumn = column; row = table.rowAtPoint(e.getPoint()); column = table.columnAtPoint(e.getPoint()); if (row == lastRow && column == lastColumn) return; if (row >= 0 && column >= 0) { table.repaint(table.getCellRect(row, column, false)); } if (lastRow >= 0 && lastColumn >= 0) { table.repaint(table.getCellRect(lastRow, lastColumn, false)); } } @Override public void mouseExited(MouseEvent e) { if (row >= 0 && column >= 0) { table.repaint(table.getCellRect(row, column, false)); } row = column = -1; } } static class RolloverBooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource { private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); private RolloverMouseAdapter adapter; public RolloverBooleanRenderer(RolloverMouseAdapter adapter) { super(); this.adapter = adapter; setHorizontalAlignment(JLabel.CENTER); setBorderPainted(true); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { getModel().setRollover(adapter.isRolloverCell(row, column)); if (isSelected) { setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setSelected((value != null && ((Boolean) value).booleanValue())); if (hasFocus) { setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); } else { setBorder(noFocusBorder); } return this; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } 
+6
source

This is a rollover (or lack thereof) that makes the widgets in the table seem dead. If JPanel is a genuine widget container, why aren't JTable and JTree ?

Both JTable and JTree use flies for rendering. rendering and editor by default do not have built-in mouse behavior. You must ensure the desired behavior yourself, as @Max shows .

+3
source

Jtable does not put real elements in cells. They use component layout methods to display cell contents.

0
source

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


All Articles