java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3942
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create a Editable combobox that include both text and icons E-mail
User Rating: / 33
PoorBest 

This Java Swing tip illustrates a method of creating a Editable combobox that include both text and icons. The tip demonstrates a fancy example of JComboBox with a custom renderer and editor. The custom renderer and editor are used to display a list of JLabel objects that include both text and icons.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class EditableComboBox extends JPanel {
    
    private BookEntry books[] {
        new BookEntry("Ant: The Definitive Guide""covers/ant.gif"),
        new BookEntry("Database Programming with JDBC and Java",
             "covers/jdbc.gif"),
        new BookEntry("Developing Java Beans""covers/beans.gif"),
        new BookEntry("Developing JSP Custom Tag Libraries",
             "covers/jsptl.gif"),
        new BookEntry("Java 2D Graphics""covers/java2d.gif"),
        new BookEntry("Java and XML""covers/jxml.gif"),
        new BookEntry("Java and XSLT""covers/jxslt.gif"),
        new BookEntry("Java and SOAP""covers/jsoap.gif"),
        new BookEntry("Java and XML Data Binding""covers/jxmldb.gif"),
        new BookEntry("Java Cookbook""covers/jcook.gif"),
        new BookEntry("Java Cryptography""covers/jcrypto.gif"),
        new BookEntry("Java Distributed Computing""covers/jdist.gif"),
        new BookEntry("Java I/O""covers/javaio.gif"),
        new BookEntry("Java in a Nutshell""covers/javanut.gif"),
        new BookEntry("Java Management Extensions""covers/jmx.gif"),
        new BookEntry("Java Message Service""covers/jms.gif"),
        new BookEntry("Java Network Programming""covers/jnetp.gif"),
        new BookEntry("Java Performance Tuning""covers/jperf.gif"),
        new BookEntry("Java RMI""covers/jrmi.gif"),
        new BookEntry("Java Security""covers/jsec.gif"),
        new BookEntry("JavaServer Pages""covers/jsp.gif"),
        new BookEntry("Java Servlet Programming""covers/servlet.gif"),
        new BookEntry("Java Swing""covers/swing.gif"),
        new BookEntry("Java Threads""covers/jthread.gif"),
        new BookEntry("Java Web Services""covers/jws.gif"),
        new BookEntry("Learning Java""covers/learnj.gif")
    };
    
    Map bookMap = new HashMap();
    
    public EditableComboBox() {
        // Build a mapping from book titles to their entries
        for (int i = ; i < books.length; i++) {
            bookMap.put(books[i].getTitle(), books[i]);
        }
        
        setLayout(new BorderLayout());
        
        JComboBox bookCombo = new JComboBox(books);
        bookCombo.setEditable(true);
        bookCombo.setEditor(
                new ComboBoxEditorExample(bookMap, books[0]));
        bookCombo.setMaximumRowCount(4);
        bookCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("You chose " (
                        (JComboBox)e.getSource()).getSelectedItem()  "!");
            }
        });
        bookCombo.setActionCommand("Hello");
        add(bookCombo, BorderLayout.CENTER);
    }
    
    public static void main(String s[]) {
        JFrame frame = new JFrame("Combo Box Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new EditableComboBox());
        frame.pack();
        frame.setVisible(true);
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.