There is a table JTable, on it I hang up such a render:
public class TableCellRender extends DefaultTableCellRenderer{ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); String text; if(value.getClass() == Integer.class) text = Integer.toString((int)value); else if(value.getClass() == Double.class) text = Double.toString((double)value); else if(value.getClass() == String.class) text = " "+(String) value; else text = Long.toString((long)value); if(column == 2) text += "\u0584"; // стоимость if(column == 3) text += "ч."; // время if(column == 6){ // дата if((long)value == -1) text = ""; else{ Date d = new Date(); d.setTime((long)value); text = org.AppLoader.DATE_FRMT.format(d) + "г."; } } if(column == 7) text += "%"; // процент if(text.contains("-1")) text =""; JLabel lbl = new JLabel(text); // установка шрифтов if(column == 0)lbl.setFont(new java.awt.Font("Times New Roman", 0, 12)); // шрифт первой колонки else if(column != 1) lbl.setFont(new java.awt.Font("Times New Roman", 0, 16)); else lbl.setFont(new java.awt.Font("Times New Roman", 1, 16)); // шрифт второй колонки if(column != 1)lbl.setHorizontalAlignment(SwingConstants.CENTER); // настройка цветов оцкнуи и УЦ if(column == 5){ if((int)value > 8) lbl.setForeground(new Color(0, 255, 255)); else if((int)value > 6) lbl.setForeground(Color.GREEN); else if((int)value > 4) lbl.setForeground(new Color(240,240,0)); else if((int)value > 2) lbl.setForeground(Color.ORANGE); else lbl.setForeground(Color.RED); } if(column == 4){ if((double)value == 0) lbl.setForeground(new Color(0, 255, 255)); else if((double)value <= 10) lbl.setForeground(Color.GREEN); else if((double)value <= 30) lbl.setForeground(Color.ORANGE); else lbl.setForeground(Color.RED); } lbl.setFocusable(true); return lbl; } }
I suppose that because I return the JLabel, the ability to edit and select cells is removed. What can JLabel be replaced with so that the formatting remains, but was it possible to select a cell?