Write control by yourself using java

Source: Internet
Author: User

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;
   }
}

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.