A fancy example of JComboBox with a custom renderer and editor : ComboBox « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing JFC » ComboBoxScreenshots 
A fancy example of JComboBox with a custom renderer and editor
A fancy example of JComboBox with a custom renderer and editor
  
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// EditableComboBox.java
//A fancy example of JComboBox with a custom renderer and editor used to
//display a list of JLabel objects that include both text and icons.
//

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ComboBoxEditor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

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 = 0; 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 "
            ((JComboBoxe.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);
  }
}

class BookEntry {
  private final String title;

  private final String imagePath;

  private ImageIcon image;

  public BookEntry(String title, String imagePath) {
    this.title = title;
    this.imagePath = imagePath;
  }

  public String getTitle() {
    return title;
  }

  public ImageIcon getImage() {
    if (image == null) {
      image = new ImageIcon(imagePath);
    }
    return image;
  }

  // Override standard toString method to give a useful result
  public String toString() {
    return title;
  }
}

class ComboBoxEditorExample implements ComboBoxEditor {
  Map map;

  ImagePanel panel;

  ImageIcon questionIcon;

  public ComboBoxEditorExample(Map m, BookEntry defaultChoice) {
    map = m;
    panel = new ImagePanel(defaultChoice);
    questionIcon = new ImageIcon("question.gif");
  }

  public void setItem(Object anObject) {
    if (anObject != null) {
      panel.setText(anObject.toString());
      BookEntry entry = (BookEntrymap.get(anObject.toString());
      if (entry != null)
        panel.setIcon(entry.getImage());
      else
        panel.setIcon(questionIcon);
    }
  }

  public Component getEditorComponent() {
    return panel;
  }

  public Object getItem() {
    return panel.getText();
  }

  public void selectAll() {
    panel.selectAll();
  }

  public void addActionListener(ActionListener l) {
    panel.addActionListener(l);
  }

  public void removeActionListener(ActionListener l) {
    panel.removeActionListener(l);
  }

  //  We create our own inner class to handle setting and
  //  repainting the image and the text.
  class ImagePanel extends JPanel {

    JLabel imageIconLabel;

    JTextField textField;

    public ImagePanel(BookEntry initialEntry) {
      setLayout(new BorderLayout());

      imageIconLabel = new JLabel(initialEntry.getImage());
      imageIconLabel.setBorder(new BevelBorder(BevelBorder.RAISED));

      textField = new JTextField(initialEntry.getTitle());
      textField.setColumns(45);
      textField.setBorder(new BevelBorder(BevelBorder.LOWERED));

      add(imageIconLabel, BorderLayout.WEST);
      add(textField, BorderLayout.EAST);
    }

    public void setText(String s) {
      textField.setText(s);
    }

    public String getText() {
      return (textField.getText());
    }

    public void setIcon(Icon i) {
      imageIconLabel.setIcon(i);
      repaint();
    }

    public void selectAll() {
      textField.selectAll();
    }

    public void addActionListener(ActionListener l) {
      textField.addActionListener(l);
    }

    public void removeActionListener(ActionListener l) {
      textField.removeActionListener(l);
    }
  }
}

           
         
    
  
Related examples in the same category
1.Combobox combines a button or editable field and a drop-down list.
2.Using drop-down listsUsing drop-down lists
3.Editable ComboBoxEditable ComboBox
4.Create a simple combobox
5.Font Chooser ComboBoxFont Chooser ComboBox
6.Color combobox rendererColor combobox renderer
7.Select the combobox by choose the nearby labelSelect the combobox by choose the nearby label
8.ComoboBox loads and saves items automatically from a fileComoboBox loads and saves items automatically from a file
9.Sharing a Model between a JList and JComboBoxSharing a Model between a JList and JComboBox
10.ComboBox Demo 2ComboBox Demo 2
11.Custom ComboBox with ImageCustom ComboBox with Image
12.ComboBox DemoComboBox Demo
13.List: Shared Data SampleList: Shared Data Sample
14.Selecting Combo SampleSelecting Combo Sample
15.MultiKey ComboMultiKey Combo
16.PopupCombo SamplePopupCombo Sample
17.Dual Sample: JList and ComboBoxDual Sample: JList and ComboBox
18.ComboBox SampleComboBox Sample
19.Color ComboBox: ComboBoxEditor DemoColor ComboBox: ComboBoxEditor Demo
20.Determining When the Menu of a JComboBox Component Is Displayed
21.Listening for Action Events from a JComboBox Component
22.Listening for Changes to the Selected Item in a JComboBox Component
23.Setting the Number of Visible Items in the Menu of a JComboBox Component
24.Displaying the Menu in a JComboBox Component Using a Keystroke If the Selected Item Is Not Unique
25.Displaying the Menu in a JComboBox Component Using a Keystroke
26.Determining If the Menu of a JComboBox Component Is Visible
27.Selecting an Item in a JComboBox Component with Multiple Keystrokes
28.Adding and Removing an Item in a JComboBox Component
29.Add an item after the first item
30.Add an item to the end of the list
31.Remove first item
32.Remove the last item
33.Remove all items
34.Getting the Items in a JComboBox Component
35.Getting and Setting the Selected Item in a JComboBox Component
36.If the combobox is editable, the new value can be any value
37.Creating a read-only and editable JComboBox Component
38.ArrayListComboBoxModel DemoArrayListComboBoxModel Demo
39.A frame with a sample text label and a combo box for selecting font faces
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.