Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extended JComboBox

JörgJan 31 2012 — edited Jan 31 2012
Hello,

I just finished the following class, but I'm not capable of avoiding two warnings (except supressing them).
Would somebody kindly have a look at the green comments? I can provide test code if required, too.
import java.awt.*; 
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

public class DifferentPopupCombo extends JComboBox<DifferentPopupCombo.Item> {
  public static final long serialVersionUID= 80L;
  protected int popupWidth;

//  @SuppressWarnings("unchecked")
  public DifferentPopupCombo(Item[] items) {
    super(items);
    setUI(new DifferentPopupComboUI());
    setRenderer(new PopupRenderer()); // unchecked conversion here
  }


  public Dimension getPopupSize() {
    Dimension size = getSize();
    if (popupWidth < 1) popupWidth = size.width;
    return new Dimension(popupWidth, size.height);
  }


  public void setPopupWidth(int width) {
    popupWidth = width;
  }


  static class Item {
    private String id;
    private String resultView;

    public Item(String id, String resultView) {
      this.id = id;
      this.resultView = resultView;
    }

    public String getId() {
      return id;
    }

    public String getResultView() {
      return resultView;
    }

    public String toString() {
      return resultView;
    }
  }


  class PopupRenderer extends BasicComboBoxRenderer {
    public static final long serialVersionUID= 81L;

//    @SuppressWarnings("rawtypes")
//found raw type: JList
    public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
      super.getListCellRendererComponent(list, value, index, isSelected,
					 cellHasFocus);
      if (index>-1) setText( ((Item)value).getId() );
      return this;
    }
  }


  class DifferentPopupComboUI extends MetalComboBoxUI {

    protected ComboPopup createPopup() {
      BasicComboPopup popup = new BasicComboPopup(comboBox) {
	public static final long serialVersionUID= 82L;
	public void show() {
	  Dimension popupSize= getPopupSize();
	  popupSize.setSize(popupSize.width, getPopupHeightForRowCount
					(comboBox.getMaximumRowCount()));
	  Rectangle popupBounds = computePopupBounds(0,
		 comboBox.getBounds().height,popupSize.width, popupSize.height);
	  scroller.setMaximumSize(popupBounds.getSize());
	  scroller.setPreferredSize(popupBounds.getSize());
	  scroller.setMinimumSize(popupBounds.getSize());
	  list.invalidate();
	  int selectedIndex = comboBox.getSelectedIndex();
	  if (selectedIndex == -1)
	    list.clearSelection();
	  else
	    list.setSelectedIndex(selectedIndex);            
	  list.ensureIndexIsVisible(list.getSelectedIndex());
	  setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
	  show(comboBox, popupBounds.x, popupBounds.y);
	}
      };
      popup.getAccessibleContext().setAccessibleParent(comboBox);
      return popup;
    }
  }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2012
Added on Jan 31 2012
2 comments
3,094 views