Custom combo box

Source: Internet
Author: User

Import javax. swing .*;
Import java. awt .*;

Public class JComboBoxCustomTest {
JFrame mainFrame;
JPanel mainPanel;
JComboBox customComboBox;
CustomComboBoxRenderer customComboBoxRenderer;

Public JComboBoxCustomTest (){
MainFrame = new JFrame ("JComboBoxCustomTest ");
MainPanel = new JPanel ();
CustomComboBoxRenderer = new CustomComboBoxRenderer ();
CustomComboBox = new JComboBox ();

CustomComboBoxRenderer. setPreferredSize (new Dimension (200,50 ));
CustomComboBox. setRenderer (customComboBoxRenderer );
CustomComboBox. addItem ("dd"); // Add two items at will. "dd" is useless here.
CustomComboBox. addItem ("dd ");

MainPanel. add (customComboBox );
MainFrame. getContentPane (). add (mainPanel, BorderLayout. PAGE_START );
MainFrame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
MainFrame. pack ();
MainFrame. setVisible (true );
}
Public static void main (String [] args ){
SwingUtilities. invokeLater (new Runnable (){
Public void run (){
New JComboBoxCustomTest ();
}
});
}
}
Class customcomboboxrenderer implements listcellrenderer {
Public customcomboboxrenderer (){
Setopaque (true );
}
Public component getlistcellrenderercomponent (
Jlist list,
Object value,
Int index,
Boolean isselected,
Boolean cellhasfocus)
{
Return new JCheckBox ("chekcbox ");
}
}

 

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.