|
A custom combobox editor for use with the EditableComboBox class |
|
|
This Java Swing tip illustrates a method of creating a custom combobox
editor for use with the EditableComboBox class. A combo box uses a renderer
to display each item in its menu. An editable combo box, on the other hand,
uses an editor to display the selected item.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public 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 = (BookEntry)map.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 Tips
|
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.