JComboBox: setRenderer(ListCellRenderer aRenderer) : JComboBox « javax.swing « Java by API






JComboBox: setRenderer(ListCellRenderer aRenderer)

  
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class MainClass {
  public static void main(String args[]) throws Exception {
    Color colors[] = { Color.GREEN, Color.ORANGE, Color.PINK, Color.RED,
        Color.WHITE, Color.YELLOW };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    frame.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    frame.add(label, BorderLayout.CENTER);

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Color selectedColor = (Color) comboBox.getSelectedItem();
        label.setBackground(selectedColor);
      }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}
class ColorCellRenderer implements ListCellRenderer {
  protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
  private final static Dimension preferredSize = new Dimension(0, 20);

  public Component getListCellRendererComponent(JList list, Object value, int index,
      boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
        isSelected, cellHasFocus);
    if (value instanceof Color) {
      renderer.setBackground((Color) value);
    }
    renderer.setPreferredSize(preferredSize);
    return renderer;
  }
}


           
         
    
  








Related examples in the same category

1.implements JComboBox.KeySelectionManager
2.new JComboBox()
3.new JComboBox(ComboBoxModel aModel)
4.new JComboBox(Vector items)
5.JComboBox: addActionListener(ActionListener lis)
6.JComboBox: addItem(Object o)
7.JComboBox: addItemListener(ItemListener lis)
8.JComboBox: addPopupMenuListener(PopupMenuListener l)
9.JComboBox: getActionMap()
10.JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
11.JComboBox: getItemAt(int index)
12.JComboBox: getItemCount()
13.JComboBox: getKeySelectionManager()
14.JComboBox: getSelectedIndex()
15.JComboBox: getSelectedItem()
16.JComboBox: insertItemAt(Object anObject, int index)
17.JComboBox: isPopupVisible()
18.JComboBox: removeAllItems()
19.JComboBox: removeItemAt(int anIndex)
20.JComboBox: setEditor(ComboBoxEditor anEditor)
21.JComboBox: setKeySelectionManager(KeySelectionManager aManager)
22.JComboBox: setEditable(boolean b)
23.JComboBox: setMaximumRowCount(int count)
24.JComboBox: setSelectedIndex(int anIndex)
25.JComboBox: setSelectedItem(Object anObject)
26.JComboBox: setUI(ComboBoxUI ui)