Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Swing Code Examples

Color selected cells in a JTable

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
Color selected cells in a JTable public class TableColorSelection extends JFrame { String[] columns = { "mon","tue","wed" }; public static Color givenColor = new Color(255,128,54); static JTable table; public TableColorSelection() { DefaultTableModel model = new DefaultTableModel(columns,0) { public boolean isCellEditable(int row,int col) { return false; } }; model.addRow(new Object[]{ "1","2","3" }); model.addRow(new Object[]{ "4","5","6" }); model.addRow(new Object[]{ "7","8","9" }); Object[] defaultOffDays = { "3","6","9" }; table = new MT(model,defaultOffDays); table.setCellSelectionEnabled(true); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroller = new JScrollPane(table); scroller.setBounds(10,10,300,200); JPanel panel = new JPanel(); panel.setLayout(null); panel.add(scroller); getContentPane().add(panel); setSize(400,400); setVisible(true); } public static void main(String[] arg) { TableColorSelection tt = new TableColorSelection(); } } class MT extends JTable { public MT(TableModel dm,Object[] defaultOffDays) { super(dm); setDefaultRenderer(Object.class,new MyCellRenderer(defaultOffDays)); } public void changeSelection(int r,int c,boolean toggle,boolean extend) { super.changeSelection(r,c,toggle,extend); MyCellRenderer renderer = (MyCellRenderer) getDefaultRenderer(Object.class); Object val = getValueAt(r,c); if (renderer.isRed(val)) { renderer.makeBlue(val); } else { renderer.clearValue(); } } class MyCellRenderer extends DefaultTableCellRenderer { java.util.ArrayList selectedValues = new java.util.ArrayList(); Object v = null; public MyCellRenderer(Object[] defaultOffDays) { for (int i = 0; i < defaultOffDays.length; i++) { selectedValues.add(defaultOffDays); } } public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected,boolean hasFocus,int row,int column) { JLabel label = (JLabel) super.getTableCellRendererComponent(table,value,isSelected, hasFocus,row,column); setHorizontalAlignment(javax.swing.SwingConstants.CENTER); setText(value.toString()); if (isSelected && (v != value)) { setForeground(Color.RED); if (!selectedValues.contains(value)) { //value already remembered, and reselected, make FG blue selectedValues.add(value); } } else { //not selected setForeground(selectedValues.contains(value) ? Color.RED : Color.BLUE); } return label; } public boolean isRed(Object value) { // System.out.println("isRed: " + selectedValues.contains(value)); return selectedValues.contains(value); } public void makeBlue(Object value) { v = value; selectedValues.remove(value); } public void clearValue() { v = null; } } }