Note that there are some explanatory texts on larger screens.

plurals
  1. POIn a JTable, how do I render a checkbox column with a dirty marker?
    primarykey
    data
    text
    <p>I'm trying to show a marker on checkbox columns in a JTable to indicate that the value is dirty.</p> <p>I'm having trouble coming up with a way to render the marker. I've tried setting an icon on the JCheckbox but this just renders the icon instead of the checkbox. I've tried using a Panel but it messes up the layout.</p> <p>Does anyone have an idea what is the best way to do this?</p> <p>Thanks</p> <p>This is the sort of thing I've tried so far:</p> <pre><code>import java.awt.Component; import javax.swing.Icon; import javax.swing.JCheckBox; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.table.TableCellRenderer; public class DirtyCheckboxRenderer extends JCheckBox implements TableCellRenderer { private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); public DirtyCheckboxRenderer() { setHorizontalAlignment(SwingConstants.CENTER); setBorderPainted(true); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setForegroundColor(table, isSelected); setBackgroundColor(table, isSelected); setCheckboxState(value); setBorder(hasFocus); setDirtyMarkerIcon(); return this; } private void setCheckboxState(Object value) { boolean checked = value != null &amp;&amp; ((Boolean) value).booleanValue(); setSelected(checked); } private void setBorder(boolean hasFocus) { if (hasFocus) { setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); } else { setBorder(this.noFocusBorder); } } private void setForegroundColor(JTable table, boolean isSelected) { if (isSelected) { setForeground(table.getSelectionForeground()); } else { setForeground(table.getForeground()); } } private void setBackgroundColor(JTable table, boolean isSelected) { if (isSelected) { setBackground(table.getSelectionBackground()); } else { setBackground(table.getBackground()); } } private void setDirtyMarkerIcon() { boolean columnIsDirty = true; //TODO if (columnIsDirty) { Icon icon = getDirtyMarkerIcon(); setHorizontalTextPosition(SwingConstants.TRAILING); setIcon(icon); } else { setIcon(null); } } private Icon getDirtyMarkerIcon() { //TODO return null; // } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload