customize jcomboboc

Solutions on MaxInterview for customize jcomboboc by the best coders in the world

showing results for - "customize jcomboboc"
Ilyana
29 Aug 2019
1package net.codejava.swing.combobox;
2 
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.FlowLayout;
6import java.awt.Font;
7 
8import javax.swing.JLabel;
9import javax.swing.JPanel;
10import javax.swing.plaf.basic.BasicComboBoxEditor;
11 
12public class MyComboBoxEditor extends BasicComboBoxEditor {
13    private JLabel label = new JLabel();
14    private JPanel panel = new JPanel();
15    private Object selectedItem;
16     
17    public MyComboBoxEditor() {
18         
19        label.setOpaque(false);
20        label.setFont(new Font("Arial", Font.BOLD, 14));
21        label.setForeground(Color.BLACK);
22         
23        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 2));
24        panel.add(label);
25        panel.setBackground(Color.GREEN);
26    }
27     
28    public Component getEditorComponent() {
29        return this.panel;
30    }
31     
32    public Object getItem() {
33        return "[" + this.selectedItem.toString() + "]";
34    }
35     
36    public void setItem(Object item) {
37        this.selectedItem = item;
38        label.setText(item.toString());
39    }
40     
41}
42
Christian
28 May 2020
1package net.codejava.swing.combobox;
2 
3import javax.swing.DefaultComboBoxModel;
4import javax.swing.JComboBox;
5 
6/**
7 * A custom combo box with its own renderer and editor.
8 * @author wwww.codejava.net
9 *
10 */
11public class CountryComboBox extends JComboBox {
12    private DefaultComboBoxModel model;
13     
14    public CountryComboBox() {
15        model = new DefaultComboBoxModel();
16        setModel(model);
17        setRenderer(new CountryItemRenderer());
18        setEditor(new CountryItemEditor());
19    }
20     
21    /**
22     * Add an array items to this combo box.
23     * Each item is an array of two String elements:
24     * - first element is country name.
25     * - second element is path of an image file for country flag.
26     * @param items
27     */
28    public void addItems(String[][] items) {
29        for (String[] anItem : items) {
30            model.addElement(anItem);
31        }
32    }
33}
34
Giulia
03 Apr 2020
1package net.codejava.swing.combobox;
2 
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.GridBagConstraints;
6import java.awt.GridBagLayout;
7import java.awt.Insets;
8 
9import javax.swing.ImageIcon;
10import javax.swing.JLabel;
11import javax.swing.JList;
12import javax.swing.JPanel;
13import javax.swing.ListCellRenderer;
14 
15/**
16 * Customer renderer for JComboBox
17 * @author www.codejava.net
18 *
19 */
20public class CountryItemRenderer extends JPanel implements ListCellRenderer {
21    private JLabel labelItem = new JLabel();
22     
23    public CountryItemRenderer() {
24        setLayout(new GridBagLayout());
25        GridBagConstraints constraints = new GridBagConstraints();
26        constraints.fill = GridBagConstraints.HORIZONTAL;
27        constraints.weightx = 1.0;
28        constraints.insets = new Insets(2, 2, 2, 2);
29         
30        labelItem.setOpaque(true);
31        labelItem.setHorizontalAlignment(JLabel.LEFT);
32         
33        add(labelItem, constraints);
34        setBackground(Color.LIGHT_GRAY);
35    }
36     
37    @Override
38    public Component getListCellRendererComponent(JList list, Object value,
39            int index, boolean isSelected, boolean cellHasFocus) {
40        String[] countryItem = (String[]) value;
41 
42        // set country name
43        labelItem.setText(countryItem[0]);
44         
45        // set country flag
46        labelItem.setIcon(new ImageIcon(countryItem[1]));
47         
48        if (isSelected) {
49            labelItem.setBackground(Color.BLUE);
50            labelItem.setForeground(Color.YELLOW);
51        } else {
52            labelItem.setForeground(Color.BLACK);
53            labelItem.setBackground(Color.LIGHT_GRAY);
54        }
55         
56        return this;
57    }
58 
59}
60
Sara Sofía
26 Jul 2017
1package net.codejava.swing.combobox;
2 
3import java.awt.Dimension;
4import java.awt.FlowLayout;
5 
6import javax.swing.JFrame;
7import javax.swing.SwingUtilities;
8 
9public class CustomComboBoxTester extends JFrame {
10     
11    public CustomComboBoxTester() {
12        super("Demo program for custom combobox");
13        setLayout(new FlowLayout());
14         
15        CountryComboBox customCombobox = new CountryComboBox();
16        customCombobox.setPreferredSize(new Dimension(120, 30));
17        customCombobox.setEditable(true);
18        customCombobox.addItems(countryList);
19         
20        add(customCombobox);
21 
22        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23        setSize(400, 100);
24        setLocationRelativeTo(null);    // center on screen
25    }
26     
27    public static void main(String[] args) {
28        SwingUtilities.invokeLater(new Runnable() {
29            @Override
30            public void run() {
31                new CustomComboBoxTester().setVisible(true);
32            }
33        });
34    }
35 
36    private String[][] countryList = {{"USA", "us.png"},
37                                      {"India", "in.png"},
38                                      {"Vietnam", "vn.png"},
39                                      {"Germany", "de.png"},
40                                      {"Canada", "ca.png"},
41                                      {"Japan", "jp.png"},
42                                      {"Great Britain", "gb.png"},
43                                      {"France", "fr.png"}};
44}
45
Luca
04 Feb 2016
1package net.codejava.swing.combobox;
2 
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.Font;
6 
7import javax.swing.JLabel;
8import javax.swing.JList;
9import javax.swing.ListCellRenderer;
10 
11public class MyComboBoxRenderer extends JLabel implements ListCellRenderer {
12 
13    public MyComboBoxRenderer() {
14        setOpaque(true);
15        setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
16        setBackground(Color.BLUE);
17        setForeground(Color.YELLOW);
18    }
19     
20    @Override
21    public Component getListCellRendererComponent(JList list, Object value,
22            int index, boolean isSelected, boolean cellHasFocus) {
23        setText(value.toString()); 
24        return this;
25    }
26 
27}
28
Isabel
04 Sep 2019
1package net.codejava.swing.combobox;
2 
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.GridBagConstraints;
6import java.awt.GridBagLayout;
7import java.awt.Insets;
8 
9import javax.swing.ImageIcon;
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12import javax.swing.plaf.basic.BasicComboBoxEditor;
13 
14/**
15 * Editor for JComboBox
16 * @author wwww.codejava.net
17 *
18 */
19public class CountryItemEditor extends BasicComboBoxEditor {
20    private JPanel panel = new JPanel();
21    private JLabel labelItem = new JLabel();
22    private String selectedValue;
23     
24    public CountryItemEditor() {
25        panel.setLayout(new GridBagLayout());
26        GridBagConstraints constraints = new GridBagConstraints();
27        constraints.fill = GridBagConstraints.HORIZONTAL;
28        constraints.weightx = 1.0;
29        constraints.insets = new Insets(2, 5, 2, 2);
30         
31        labelItem.setOpaque(false);
32        labelItem.setHorizontalAlignment(JLabel.LEFT);
33        labelItem.setForeground(Color.WHITE);
34         
35        panel.add(labelItem, constraints);
36        panel.setBackground(Color.BLUE);        
37    }
38     
39    public Component getEditorComponent() {
40        return this.panel;
41    }
42     
43    public Object getItem() {
44        return this.selectedValue;
45    }
46     
47    public void setItem(Object item) {
48        if (item == null) {
49            return;
50        }
51        String[] countryItem = (String[]) item;
52        selectedValue = countryItem[0];
53        labelItem.setText(selectedValue);
54        labelItem.setIcon(new ImageIcon(countryItem[1]));       
55    }   
56}
57
similar questions
queries leading to this page
customize jcomboboc