如何在jtable中使不可编辑的单元格变灰?
我想在JTable中灰化不可编辑的单元格。 我正在使用这样的TableCellRenderer:
TableColumn column = table.getColumnModel().getColumn(0); column.setCellRenderer(new GrayableCheckboxCellRenderer()); public class GrayableCheckboxCellRenderer extends JCheckBox implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int vRowIndex, int vColIndex) { boolean editable = isEditable(vRowIndex, vColIndex); setBackground(editable ? UIManager.getColor("Label.background") : Color.LIGHT_GRAY); setSelected((Boolean) value); if (isSelected) { // TODO: cell (and perhaps other cells) are selected, need to highlight it } return this; } // perfomance protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {} public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {} public void revalidate() {} public void validate() {} }
这有效,但有一个恼人的人工制品:最初“复选框”是“左排列”,当我按下鼠标左键时,它移动到“中心排列”,当我释放鼠标按钮时,它移回“左排列”。
如何避免这种恼人的人工制品,可能还有更好的解决方案来解决我的问题?
在TableCellEditor
返回GrayableCheckboxCellRenderer
的实例。
附录:在美学上,您可能希望根据当前外观提供的默认值来调整渲染器和编辑器的颜色,例如:
Color hilite = UIManager.getColor("Table.selectionBackground");
使用preparedRenderer最简单的方法
import java.awt.*; import java.util.Random; import java.util.Vector; import javax.swing.*; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellRenderer; public class Forum { private JFrame frame = new JFrame("Frame"); private JPanel fatherCenter = new JPanel(); private JScrollPane tableScroll = new JScrollPane(); private myTableModel tableModel; private JTable dialogTable; private ListSelectionModel lsDialog; private void addComponentsToPane(Container pane) { tableModel = new myTableModel(); dialogTable = new JTable(tableModel) { private static final long serialVersionUID = 1L; @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component comp = super.prepareRenderer(renderer, row, column); JComponent jc = (JComponent) comp;//for Custom JComponent if (!isRowSelected(row)) { int modelRow = convertRowIndexToModel(row); boolean type = (Boolean) getModel().getValueAt(modelRow, 2); boolean type1 = (Boolean) getModel().getValueAt(modelRow, 3); boolean type2 = (Boolean) getModel().isCellEditable(row, column); comp.setForeground(Color.black); if ((type) && (!type1)) { comp.setBackground(Color.yellow); } else if ((!type) && (type1)) { comp.setBackground(Color.orange); } else if ((!type) || (!type1)) { comp.setBackground(Color.red); //} else if ((!type2)) { //comp.setForeground(Color.red); //comp.setBackground(Color.magenta); } else { comp.setBackground(row % 2 == 0 ? getBackground() : getBackground().darker()); } dialogTable.convertRowIndexToView(0); } else { comp.setForeground(Color.blue); comp.setBackground(Color.lightGray); } if (!isCellEditable(row, column)) { comp.setForeground(Color.red); comp.setBackground(Color.magenta); } return comp; } }; tableScroll = new JScrollPane(dialogTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); tableScroll.setBorder(null); dialogTable.getTableHeader().setReorderingAllowed(false); dialogTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); lsDialog = dialogTable.getSelectionModel(); dialogTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); dialogTable.setRowHeight(20); dialogTable.setRowMargin(2); fatherCenter = new JPanel(); fatherCenter.setLayout(new BorderLayout(10, 10)); fatherCenter.add(tableScroll, BorderLayout.CENTER); pane.add(fatherCenter); } private void addData() { Runnable doRun1 = new Runnable() { @Override public void run() { tableModel.resetTable(); Vector tbl = new Vector (); Vector
只需在’getTableCellRendererComponent()’方法中明确设置对齐就可以解决问题。 在从方法返回之前添加以下行。
setHorizontalAlignment(SwingConstants.CENTER);