Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4335575

Set unlisted item in JComboBox does not work for Windows L&F

XMLWordPrintable

    • generic, x86
    • generic, windows_nt

      Name: skT45625 Date: 05/03/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      When you want to display value, which is not on JComboBox list, you have to set
      JComboBox to editable, set value and turn back to non-editable. It's stoped to
      work for JDK1.2.2 for Windows L&F: value unvisible. It's working before and it
      works in jdk1.2.2 for Java L&F. It's visible only, if you not turn JComboBox
      back to non-editable after seting new unlisted value. But it's unacceptible,
      because reqirements are just to display value in this moment and not to allow
      selecting or editing.
      Run code below. Select Java L&F. Press "set unlisted button". It will bevisible.
      Press "Windows L&F" radio button. Same operation will not work.

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      class TTT extends JFrame
      {
        final public static Color backgroundColor = new Color(222,222,222);
        JComboBox cb = new JComboBox(new String[]{"a","b","c"});
        JButton b1 = new JButton("Set to unlisted item");
        JButton b2 = new JButton("Set to first item");
        JRadioButton rb1 = new JRadioButton("Java L&F", true);
        JRadioButton rb2 = new JRadioButton("Windows L&F");
        TTT() {
          getContentPane().add(BorderLayout.NORTH, cb);
          cb.setEnabled(false);
          cb.setEditable(false);
          
          Box centralBox = Box.createHorizontalBox();
          ButtonGroup gr = new ButtonGroup();
          gr.add(rb1);
          gr.add(rb2);
          centralBox.add(rb1);
          centralBox.add(rb2);
          getContentPane().add(BorderLayout.CENTER, centralBox);
          
          Box southBox = Box.createHorizontalBox();
          southBox.add(b1);
          southBox.add(b2);
          getContentPane().add(BorderLayout.SOUTH, southBox);
          
          rb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              try {
                
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(TTT.this);
              }catch(Exception e){}
            }
          });
          rb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                SwingUtilities.updateComponentTreeUI(TTT.this);
              }catch(Exception e){}
            }
          });
          b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              cb.setEditable(true);
              cb.setSelectedItem("9");
              cb.setEditable(false);
            }
          });
          b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              cb.setSelectedIndex(0);
            }
          });
          pack();
          show();
        }
        

        public static void main(String[] args) {
          try{
            UIManager.put("ComboBox.disabledForeground", Color.black);
            UIManager.put("ComboBox.disabledBackground", Color.white);
            new TTT();
          }catch(Exception e) {
            e.printStackTrace();
          }
        }
      }

      Additional Information:
      ----------------
      Problem is that you can not set item, which is not in the JComboBox list and keep it
      this JComboBox not editable (for Windows L&F; for Java L&F it's working).
      Do following:
      1. Run application.
      2. Select Java L&F
      3. Click "Set to unlisted item" button.
      4. You will see value "9" set in JComboBox.
      5. So, it's working for Java L&F. JComboBox has values "a","b" and "c". You can
         set JComboBox to "9" and keep this combobox not editable and not enabled, which we need.
      6. Now, Set JComboBox back to one of it's values, pushing button "Set to first item".
         You will se "a" again.
      7. Swith to Windows L&F.
      8. Click "Set to unlisted item" button.
      9. You will not be able to see anything in combo box. And this is a problem.

       So again, I need to keep JComboBox not editable, that customer could not type anything,
      but be able to set any value, which not belong to JComboBox items list.
      And it does not work.

      Thank you.
      Andrei
      (Review ID: 103949)
      ======================================================================

            dnguyen Damon Nguyen
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: