Example usage for javax.swing JComboBox setRenderer

List of usage examples for javax.swing JComboBox setRenderer

Introduction

In this page you can find the example usage for javax.swing JComboBox setRenderer.

Prototype

@BeanProperty(expert = true, description = "The renderer that paints the item selected in the list.")
public void setRenderer(ListCellRenderer<? super E> aRenderer) 

Source Link

Document

Sets the renderer that paints the list items and the item selected from the list in the JComboBox field.

Usage

From source file:Main.java

public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fonts = ge.getAvailableFontFamilyNames();
    JComboBox<String> fontChooser = new JComboBox<>(fonts);
    fontChooser.setRenderer(new FontCellRenderer());
    JOptionPane.showMessageDialog(null, fontChooser);

}

From source file:Main.java

public static final void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel mainPanel = new JPanel();
    JPanel buttonsPanel = new JPanel();
    frame.add(mainPanel);//from  w  w w.  j a  v  a 2  s . c  om
    frame.add(buttonsPanel, BorderLayout.SOUTH);

    String[] options = { "S", "G", "I", "T" };

    JComboBox comboBox = new JComboBox(options);
    comboBox.setRenderer(new MyComboBoxRenderer("COUNTRY"));
    comboBox.setSelectedIndex(-1);
    mainPanel.add(comboBox);

    JButton clearSelectionButton = new JButton("Clear selection");
    clearSelectionButton.addActionListener(e -> {
        comboBox.setSelectedIndex(-1);
    });
    buttonsPanel.add(clearSelectionButton);

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:ComplexCellRenderer.java

public static void main(String args[]) {
    Object elements[][] = { { new Font("Helvetica", Font.PLAIN, 20), Color.RED, new MyIcon(), "A" },
            { new Font("TimesRoman", Font.BOLD, 14), Color.BLUE, new MyIcon(), "A" },
            { new Font("Courier", Font.ITALIC, 18), Color.GREEN, new MyIcon(), "A" },
            { new Font("Helvetica", Font.BOLD | Font.ITALIC, 12), Color.GRAY, new MyIcon(), "A" },
            { new Font("TimesRoman", Font.PLAIN, 32), Color.PINK, new MyIcon(), "A" },
            { new Font("Courier", Font.BOLD, 16), Color.YELLOW, new MyIcon(), "A" },
            { new Font("Helvetica", Font.ITALIC, 8), Color.DARK_GRAY, new MyIcon(), "A" } };

    JFrame frame = new JFrame("Complex Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ListCellRenderer renderer = new ComplexCellRenderer();
    JComboBox comboBox = new JComboBox(elements);
    comboBox.setRenderer(renderer);
    frame.add(comboBox, BorderLayout.NORTH);

    frame.setSize(300, 200);/*from ww w.  j av  a  2s.c  o m*/
    frame.setVisible(true);
}

From source file:ComboTableCellRenderer.java

public static void main(String args[]) {
    Color choices[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA };
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox comboBox = new JComboBox(choices);
    comboBox.setRenderer(renderer);

    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("Editable Color Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);
    TableColumn column = table.getColumnModel().getColumn(1);
    column.setCellRenderer(renderer);/* w w  w .j  a va2  s .com*/
    column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:Main.java

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);//w  w  w. j  a  v a  2s. co m
    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);
}

From source file:ComplexRenderingSample.java

public static void main(String args[]) {

    Object elements[][] = {// www.  java 2  s.com
            { new Font("Helvetica", Font.PLAIN, 20), Color.red, new DiamondIcon(Color.blue), "Help" },
            { new Font("TimesRoman", Font.BOLD, 14), Color.blue, new DiamondIcon(Color.green), "Me" },
            { new Font("Courier", Font.ITALIC, 18), Color.green, new DiamondIcon(Color.black), "I'm" },
            { new Font("Helvetica", Font.BOLD | Font.ITALIC, 12), Color.gray, new DiamondIcon(Color.magenta),
                    "Trapped" },
            { new Font("TimesRoman", Font.PLAIN, 32), Color.pink, new DiamondIcon(Color.yellow), "Inside" },
            { new Font("Courier", Font.BOLD, 16), Color.yellow, new DiamondIcon(Color.red), "This" },
            { new Font("Helvetica", Font.ITALIC, 8), Color.darkGray, new DiamondIcon(Color.pink),
                    "Computer" } };

    JFrame frame = new JFrame("Complex Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(elements);
    ListCellRenderer renderer = new ComplexCellRenderer();
    jlist.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(jlist);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    JComboBox comboBox = new JComboBox(elements);
    comboBox.setRenderer(renderer);
    contentPane.add(comboBox, BorderLayout.NORTH);

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

From source file:EditableColorColumn.java

public static void main(String args[]) {

    Color choices[] = { Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.magenta };
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox comboBox = new JComboBox(choices);
    comboBox.setRenderer(renderer);
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("Editable Color Table");
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);

    TableColumn column = table.getColumnModel().getColumn(3);
    column.setCellRenderer(renderer);/* w  w  w  .  j a v  a2  s . c om*/
    column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:ColorComboBox.java

public static void main(String args[]) {
    Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green,
            Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow };
    JFrame frame = new JFrame("Color JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JComboBox comboBox = new JComboBox(colors);
    comboBox.setMaximumRowCount(5);// w  w  w . j a  v  a2s.  com
    comboBox.setEditable(true);
    comboBox.setRenderer(new ColorCellRenderer());
    Color color = (Color) comboBox.getSelectedItem();
    ComboBoxEditor editor = new ColorComboBoxEditor(color);
    comboBox.setEditor(editor);
    contentPane.add(comboBox, BorderLayout.NORTH);

    final JLabel label = new JLabel();
    label.setOpaque(true);
    label.setBackground((Color) comboBox.getSelectedItem());
    contentPane.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);
}

From source file:DateComboBoxRenderer.java

public static void main(String[] str) {
    JComboBox combo = new JComboBox();
    GregorianCalendar calendar = new GregorianCalendar();
    combo.addItem(calendar.getTime());/*from  ww w .  j  a  v  a 2  s  .c o  m*/

    calendar.roll(GregorianCalendar.DAY_OF_MONTH, 1);
    combo.addItem(calendar.getTime());

    combo.setRenderer(new DateComboBoxRenderer());

    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.add(new JLabel("Date Combo: "));
    panel.add(combo);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:ColorComboRenderer.java

public static void main(String[] a) {
    JComboBox cbColor = new JComboBox();
    int[] values = new int[] { 0, 128, 192, 255 };
    for (int r = 0; r < values.length; r++)
        for (int g = 0; g < values.length; g++)
            for (int b = 0; b < values.length; b++) {
                Color c = new Color(values[r], values[g], values[b]);
                cbColor.addItem(c);/*from   w ww. j  a  v a2 s  .c  o m*/
            }
    cbColor.setRenderer(new ColorComboRenderer());

    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    f.getContentPane().add(cbColor);
    f.pack();
    f.setSize(new Dimension(300, 80));
    f.show();

}