Swing Exploration: Create JComboBox with icons and indent

xiaoxiao2021-03-06  39

The default JCOMBOBOX cannot display icons, indentation and other patterns on each entry. However, Swing's MVC design structure provides unparalleled scalability for various components. To achieve this, we can create a new renderer to be drawn for each entry.

First we write a class imagedcomboBoxItem, which encapsulates a drop-down entry information, including icons, text, indentation, etc .:

Class imagedcomboBoxItem {private icon icon = null; private string text = null; private int indent = 0;

ImagedcomboBoxItem (String text, icon, int indent) {this.text = text; this.icon = icon;}

Public string getText () {returnization;

Public icon geticon () {return}

Public int getindent () {return index;}}

Then create a new JimageDcomboBox class and inherited from JCOMBOBOX. In the constructor, create a new defaultListCellRenderer as a new renderer and overwrite its getListCellrenderComponent method. In the new GetListCellrendererComponent method, the method of the parent object is first called to complete the normal entry; then determine if the entry is an ImageDcomboBoxItem instance. If so, image, icon, icon, and indentation are displayed. For more convenient display left indentation, we create an EmptyBorder directly and set the left to indent the number and set to the defaultListCellrenderer. DefaultListCellrendere is inherited from Jlabel, so you can directly accept a variety of border. Here we take each indent 10 pixels as an example.

Ok, the following is the full code:

Import java.util. *;

Import java.aw. *; import javax.swing. *;

public class JImagedComboBox extends JComboBox {public JImagedComboBox (Vector values) {super (values); ListCellRenderer renderer = new DefaultListCellRenderer () {public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {super.getListCellRendererComponent ( list, value, index, isSelected, cellHasFocus); if (value instanceof ImagedComboBoxItem) {ImagedComboBoxItem item = (ImagedComboBoxItem) value; this.setText (item.getText ()); this.setIcon (item.getIcon ()); if ( ISPOPUPVISible ()) {int offset = 10 * item.getindent (); this.setBorder (BorderFactory.createemptyborder (0, Offset, 0, 0));}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} THIS.SETRENDERER (RENDERER);

public static void main (String [] args) {JFrame frame = new JFrame (); frame.setSize (400, 400); Vector values ​​= new Vector (); Icon openIcon = new ImageIcon (JImagedComboBox.class.getResource ( "Open16 .gif ")); icon newicon = new imageicon (" new16.gif "); for (int i = 0; i <5; i ) {VALUES.Addelement (New ImagedcomboBoxItem (" Directory " I, OpenICON, I));} for (int i = 0; i <5; i ) {Values.addelement ("Image Item" i, newicon, 5);} jimagedcombobox ComboBox = New JimageDcomboBox (values); frame.getContentPane () add (comboBox, BorderLayout.NORTH);. frame.show ();}} class ImagedComboBoxItem {private icon icon = null; private String text = null; private int indent = 0;

ImagedcomboBoxItem (String text, icon, int indent) {this.text = text; this.icon = icon;}

Public string getText () {returnization;

Public icon geticon () {return}

Public int getindent () {return index;}}

The operation is as follows:

Among them, two icons are here: Open16.GIF, New16.gif

转载请注明原文地址:https://www.9cbs.com/read-118364.html

New Post(0)