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

Highlighted Item not appearing after automatically scrolling to the item

    Details

    • Subcomponent:
    • Resolved In Build:
      hopper
    • CPU:
      x86
    • OS:
      windows_nt
    • Verification:
      Verified

      Description


      I have an extension of JComboBox that is editable and when a user enters text in the combo box, it will automatically scroll to the closest matched item in the drop down list. When user then clicks on the hi-lighted item, the item is supposed to appear in the text field of the combo box. However, the mouse click on the item will not bring the item to the text field. To reproduce the problem, run the following program, click on the combo box arrow to drop down the list, then type "b" and the list will scroll to "B1", click "B1" entry and the text field is still showing "b".

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

      public class TestEditableCombo extends JComboBox {
          static String[] temp = {"A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"};

          public TestEditableCombo() {
              DefaultComboBoxModel model = new DefaultComboBoxModel(temp);
              this.setModel(model);
              this.setEditable(true);
              this.setSelectedItem(null);

              // Add key listener to combo box text field
              KeyAdapter keyAdapter = new KeyAdapter() {
                  public void keyReleased(KeyEvent e) {
                      // if the user enter text, call autoScroll to go to the closest
                      // match item in the combo box list
                      if (isPopupVisible() && !e.isActionKey()
                          && e.getKeyCode() != KeyEvent.VK_ENTER) {
                          String text = getTextField().getText();
                          autoScroll(text);
                      }
                  }
              };
              getTextField().addKeyListener(keyAdapter);
          }

          private JTextField getTextField() {
              return (JTextField)getEditor().getEditorComponent();
          }

          private void autoScroll(String text) {

              boolean bMatch = false;

              System.out.println("Typed text: |" + text + "|");
              if (text == null || text.equals("")) {
                  setSelectedItem(null);
                  return;
              }

              String itemText;
              for (int i=0; i<this.getItemCount(); i++) {
                  itemText = (String)this.getItemAt(i);
                  if (itemText.toLowerCase().startsWith(text.toLowerCase())) {
                      System.out.println("Found match");
                      setSelectedIndex(i);
                      // Set the text field back to the user entered text
                      getTextField().setText(text);
                      bMatch = true;
                      break;
                  }
              }

              // No match found, set selected item to null
              if (!bMatch) {
                  System.out.println("No match in the autoScroll()");
                  setSelectedItem(null);
                  getTextField().setText(text);
              }
          }

          public static void main(String[] args) {
              JFrame frame= new JFrame("Test");
              TestEditableCombo combo = new TestEditableCombo();
              combo.setBounds(10,10,100,20);
              frame.getContentPane().setLayout(null);
              frame.getContentPane().add(combo);
              frame.setSize(200, 200);
              frame.setVisible(true);
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              peterz Peter Zhelezniakov
              Reporter:
              atongschsunw Albert Tong-schmidt (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: