Recently learn swing, change the way jcombobox display, here is the JComboBox drop-down box up, the following is the detailed code:
Package Kissjava.jcombobox;
Import java.awt.Dimension;
Import Javax.swing.JButton;
Import Javax.swing.JComboBox;
Import javax.swing.JComponent;
Import Javax.swing.JFrame;
Import Javax.swing.plaf.ComboBoxUI;
Import Javax.swing.plaf.ComponentUI;
Import Javax.swing.plaf.basic.BasicArrowButton;
Import Javax.swing.plaf.basic.BasicComboBoxUI;
Import Javax.swing.plaf.basic.BasicComboPopup;
Import Javax.swing.plaf.basic.ComboPopup;
public class Popupcombosample extends JFrame ... {
String labels[] = ... {"Chardonnay", "Sauvignon", "Riesling", "Cabernet",
"Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc",
"Syrah", "Gewurztraminer"};
JComboBox JCB = null;
Public Popupcombosample () ... {
This.settitle ("dropdown box up");
This.setlayout (NULL);
JCB = new JComboBox (labels);
Setui .....
Jcb.setui ((Comboboxui) Mycomboboxui.createui (JCB));
Jcb.setbounds (50, 150, 200, 20);
This.add (JCB, NULL);
SetSize (300, 300);
SetVisible (TRUE);
}
public static void Main (String args[]) ... {
New Popupcombosample ();
}
Static class Mycomboboxui extends Basiccomboboxui ... {
int newsbartitlewidth = 0;
public static Componentui Createui (JComponent c) ... {
return new Mycomboboxui ();
}
Change the direction of the arrow
Protected JButton Createarrowbutton () ... {
JButton button = new Basicarrowbutton (Basicarrowbutton.north);
return button;
}
Change the combobox direction of the pop-up
Protected Combopopup createpopup () ... {
Basiccombopopup popup = new Basiccombopopup (ComboBox) ... {
public void Show () ... {
Size changes as needed
Dimension popupsize = new Dimension (200-newsbartitlewidth-5, 17 * 5);
Scroller.setmaximumsize (popupsize);
Scroller.setpreferredsize (popupsize);
Scroller.setminimumsize (popupsize);
Show (ComboBox, Newsbartitlewidth, -17 * 5);//getpopupheightforrowcount (10));
}
};
Popup.getaccessiblecontext (). Setaccessibleparent (ComboBox);
return popup;
}
}
}
The display effect is:
But look at the effect of JCB's input box does not show the line, it seems to be changed again, learning.