JComboBox the dropdown box up

Source: Internet
Author: User
Tags static class

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.