Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4412921
Votes 2
Synopsis Populate a JComboBox model in a popupMenuWillBecomeVisible() listener.
Category java:classes_swing
Reported Against 1.3
Release Fixed
State 11-Closed, duplicate of 4331058, bug
Priority: 4-Low
Related Bugs 4331058
Submit Date 07-FEB-2001
Description




Q:\>java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

Please try the added source code. The purpose is to populate the combobox at
the time the user clicks on it, just before the drop down is displayed. The
contents of the combobox in our application changes outside the context of the
program (on the server), so when the combobox is opened, a new list of items to
be showed in the dropdown is retrieved.

1. I used the solution of the article "Watch for the JComboBox Pulldown to
Open" article of John Zukowski
@java.about.com/compute/java/library/weekly/aa080400a.html in order to
repopulate the DefaultComboBoxModel with new values in the PopupMenuListener.
When I instantiate the JComboBox, I added one item to the JComboBox.

2. The first time I click on the combobox, I add 5 new items to the model in
the popupMenuWillBecomeVisible() listener. The problem is that only one item is
displayed although the model contains 5 items. This one item is displayed in a
scrolling popup which actually dispays only the first item, so the scrollbar is
very tiny.

3. The second time I click on the combobox, I add 5 more items to the model in
the popupMenuWillBecomeVisible() listener. Now only 5 items are shown in the
list, although at this time the model contains 10 items.

4. The next time I click on the combobox, I add 5 more items to the model in
the popupMenuWillBecomeVisible() listener. From now off the list shows 8 items
as expected.

SOURCE CODE to demonstrate the problem:
=======================================
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

public class MetalComboBoxUIEx extends MetalComboBoxUI {
  public BasicComboPopup pup;

  public static ComponentUI createUI(JComponent c) {
    return new MetalComboBoxUIEx() {
      protected ComboPopup createPopup() {
        pup = (BasicComboPopup)super.createPopup();
        return pup;
      }
    };
  }

  public static void main(String args[]) {
    PopupComboSample frame = new PopupComboSample();
    frame.setSize (300, 300);
    frame.setVisible (true);
  }
}

class PopupComboSample extends JFrame {
  int count = 0;
  String labels[] = { "Chardonnay", "Sauvignon", "Riesling", "Cabernet", "Zinfandel", "Merlot",
      "Pinot Noir", "Sauvignon Blanc", "Syrah", "Gew?rztraminer", "Chardonnay", "Sauvignon",
      "Riesling", "Cabernet", "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
      "Gew?rztraminer","Chardonnay", "Sauvignon", "Riesling", "Cabernet", "Zinfandel", "Merlot",
      "Pinot Noir", "Sauvignon Blanc", "Syrah", "Gew?rztraminer","Chardonnay", "Sauvignon",
      "Riesling", "Cabernet", "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
      "Gew?rztraminer","Chardonnay", "Sauvignon", "Riesling", "Cabernet", "Zinfandel", "Merlot",
      "Pinot Noir", "Sauvignon Blanc", "Syrah", "Gew?rztraminer","Chardonnay", "Sauvignon",
      "Riesling", "Cabernet", "Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
      "Gew?rztraminer","Chardonnay", "Sauvignon", "Riesling", "Cabernet", "Zinfandel", "Merlot",
      "Pinot Noir", "Sauvignon Blanc", "Syrah", "Gew?rztraminer"};

  class JComboBoxEx extends JComboBox {
    public void setUI(ComboBoxUI ui) {
      super.setUI((MetalComboBoxUIEx) MetalComboBoxUIEx.createUI(this));
    }

    public void addPopupMenuListener( PopupMenuListener listener ) {
      ((MetalComboBoxUIEx)ui).pup.addPopupMenuListener( listener );
    }

    public void removePopupMenuListener( PopupMenuListener listener ) {
      ((MetalComboBoxUIEx)ui).pup.removePopupMenuListener( listener );
    }
  };

  JComboBoxEx comboBox = new JComboBoxEx();

  PopupMenuListener l = new PopupMenuListener() {
    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
      DefaultComboBoxModel model = (DefaultComboBoxModel) comboBox.getModel();
      model.removeAllElements();
      count = (count + 5) % labels.length;
      for( int i = 0 ; i < count; ++i ) model.addElement( "" + (i + 1) + " " + labels[i]);
    }
    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
    public void popupMenuCanceled(PopupMenuEvent e) {}
  };

  public PopupComboSample() {
    this.setTitle("Popup JComboBox");
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Container contentPane = getContentPane();
    comboBox.setModel( new DefaultComboBoxModel() );
    comboBox.addItem("Test");
    comboBox.addPopupMenuListener(l);
    contentPane.add(comboBox, BorderLayout.NORTH);
    Component[] c = comboBox.getComponents();
  }
}
(Review ID: 116494) 
======================================================================
Work Around
N/A
Evaluation
This functionality has been added for 1.4 under the bug ids 4331058 and 4287690.

Specifically, you can add popup menu listeners directly to the JComboBox. I tested the case in 1.4 using JComboBox instead of JComboBoxEx and it appeared to work correctly.

  xxxxx@xxxxx   2002-05-24
Comments
  
  Include a link with my name & email   

Submitted On 29-OCT-2008
DavidACampbell
this isn't fixed at all...the above program shows the symptoms in jdk 1.6.0



PLEASE NOTE: JDK6 is formerly known as Project Mustang