Other |
---|
5.0 b32Fixed |
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
setBackground appears to set wrong background on JTextField in JTable on Windows XP. The test case works correctly on Win 2000 to reproduce, compile and run the example below on Win XP *** you will see a small jtable with two rows and two columns *** you will observe that the highlight of the selected row is missing from the second column ( Name2 ) import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.border.*; public class Test implements TableCellRenderer { JTable table = new JTable(); JPanel panel = new JPanel(new BorderLayout()); public Test() { init(); panel.add (new JScrollPane(table), BorderLayout.CENTER); } public JPanel getPanel() { return panel; } private void init() { int columnCount = 2; DefaultTableModel customTableModel = new DefaultTableModel(0, columnCount) { public Class getColumnClass(int columnIndex) { if (columnIndex == 0 ) { return String.class; } return TextFieldEditor.class; } public boolean isCellEditable(int rowIndex, int columnIndex) { return false; } }; String columns[] = {"Name", "Name2"}; customTableModel.setColumnIdentifiers(columns); table.setModel(customTableModel); TableColumnModel cm = table.getColumnModel(); while (cm.getColumnCount() > 0) { cm.removeColumn(cm.getColumn(0)); } for (int columnIndex = 0; columnIndex < columnCount; ++columnIndex) { String columnId = columns[columnIndex]; TableColumn column = new TableColumn(columnIndex); column.setResizable(false); column.setIdentifier(columnId); table.addColumn(column); } table.setDefaultRenderer(TextFieldEditor.class, this); table.setDefaultEditor(TextFieldEditor.class, new TextFieldEditor(new JTextField())); table.setShowHorizontalLines(false); table.setShowVerticalLines(true); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); table.getTableHeader().setReorderingAllowed(false); String rows[][] = { {"one1", "one2"}, {"two1", "two2"}, }; for (int index = 0; index < rows.length; index++) customTableModel.addRow( toRowElement(rows[index]) ); table.setRowSelectionInterval(0,0); } private Object[] toRowElement(String[] rows) { return new Object[] { rows[0], new JTextField(rows[1]), }; } private class TextFieldEditor extends DefaultCellEditor { public TextFieldEditor(JTextField textField) { super(textField); } public boolean isCellEditable(EventObject anEvent) { return false; } } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value != null) { Component component = null; if (value instanceof JTextField) { JTextField typeLabel = (JTextField)value; typeLabel.setOpaque(true); typeLabel.setFont(UIManager.getFont("Table.font")); //NORES typeLabel.setBorder(new EmptyBorder(0,4,0,0)); component = typeLabel; } if (component != null) { if (isSelected) { component.setForeground(UIManager.getColor("Table.selectionForeground")); //NORES component.setBackground(UIManager.getColor("Table.selectionBackground")); //NORES } else { component.setForeground(UIManager.getColor("Table.foreground")); //NORES component.setBackground(UIManager.getColor("Table.background")); //NORES } return component; } } return null; } public static void main (String args[]) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } Test test = new Test(); JFrame f = new JFrame ("Test"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.getContentPane().add (test.getPanel(), BorderLayout.CENTER); f.setSize (300, 200); f.show(); } }
|