JDK-4919687 : XP L&F: setBackground doesn't work on JtextField in JTable
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_2.6
  • CPU: generic
  • Submitted: 2003-09-09
  • Updated: 2003-12-15
  • Resolved: 2003-12-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b32Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
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();
  }
}


Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b32 tiger-beta
14-06-2004

EVALUATION Applications should be careful about using values returned from the UIManager.getXXX() methods. These values often implement UIResource which is used as a signal to the UI classes that it's safe to ignore them. In this case, a better way is to create a new Color object: Color c = new Color(UIManager.getColor("Table.selectionBackground").getRGB()); component.setBackground(c); Not a bug. ###@###.### 2003-09-11 Bug reopened. The XP L&F code has been fixed to not override the background color when painting. Added a new UI property TextField.disabledBackground which is set on Windows to have the same initial value as TextField.inactiveBackground. The latter will continue to be used for readonly text fields. ###@###.### 2003-12-03
03-12-2003

WORK AROUND There are two workarounds to this problem: 1) This workaround requires the user to be running with jdk1.4.2_01 NOT 1.4.2fcs. In the customers testcase make the following change: <<component.setBackground(UIManager.getColor("Table.selectionBackground")); ----- >>Color bgc = UIManager.getColor("Table.selectionBackground"); >>Color bgcolor = new Color(bgc.getRgb()); >>component.setBackground(bgcolor); 2) run with -Dswing.noxp ###@###.### 2003-09-12
12-09-2003