Import java. awt .*;
Import java. awt. event .*;
Import javax. swing .*;
// Color selection drop-down box demonstration
Public class IconComboBoxDemo extends JFrame {
JLabel iconLabel = null; // used to respond to changes selected in the list box
JComboBox iconComboBox = null; // custom selection drop-down box
Public IconComboBoxDemo (){
// Defines the two-dimensional array of objects, which is used to initialize the drop-down box. The parameters are icons in sequence, and the text is displayed.
Object [] [] obj = {
{New ImageIcon ("1.gif")," travel "," provide travel latest information "},
{New ImageIcon ("2.gif")," music "," provides the latest music information, classical and popular ..."},
{New ImageIcon ("3.gif")," chat "," chat with friends "},
{New ImageIcon ("4.gif")," movie "," movie and entertainment "},
{New ImageIcon ("5.gif")," home "," Home World "},
};
// Initialize the drop-down list
IconComboBox = new JComboBox ();
IconComboBox. setMaximumRowCount (3); // you can specify the maximum number of visible rows.
IconComboBox. setRenderer (new IconRenderer (); // you can specify a cell Paster.
For (int I = 0; I <obj. length; I ++) {// add all elements in the array to the drop-down box.
IconComboBox. addItem (obj [I]);
}
// Initialize iconLabel information
IconLabel = new JLabel ();
// The Event Processing in the drop-down box is implemented using an anonymous class.
IconComboBox. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent evt) {// process the event
Object [] obj = (Object []) iconComboBox. getSelectedItem (); // Obtain the selected content, which is a one-dimensional array.
IconLabel. setIcon (Icon) obj [0]); // Set the iconLabel Icon
IconLabel. setText (obj [1]. toString (); // Set the iconLabel text
}
});
// Add the component to the main form
This. getContentPane (). setLayout (new BorderLayout (); // sets the layout manager.
This. getContentPane (). add (iconComboBox, BorderLayout. NORTH); // add a drop-down box above
This. getContentPane (). add (iconLabel, BorderLayout. CENTER); // add iconLabel in the middle to respond to selected changes
This. Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the window is closed.
This. setSize (350,260); // Set the window size
This. setVisible (true); // Display window
}
Public static void main (String [] args ){
New IconComboBoxDemo ();
}
}
Import java. awt .*;
Import javax. swing .*;
Import javax. swing. border. LineBorder;
// The cell Paster with the icon drop-down box, which is extended from the JLabel class to implement the ListCellRenderer interface
Public class IconRenderer extends JLabel implements ListCellRenderer {
Public Component getListCellRendererComponent (JList list, Object obj, int row, boolean sel, boolean hasFocus ){
Object [] cell = (Object []) obj; // Get the row parameter
SetIcon (Icon) cell [0]); // sets the Icon
SetText (cell [1]. toString (); // sets the text
SetToolTipText (cell [2]. toString (); // sets the prompt text
SetBorder (new LineBorder (Color. WHITE); // set the boundary
If (sel ){
SetForeground (Color. MAGENTA); // if selected, set the text Color to the Product Red
}
Else {
SetForeground (list. getForeground (); // if not selected, set the text color to the default color.
}
Return this;
}
}