How do I set and get the selected item in JComboBox?

The code below demonstrate how to set the selected item of JComboBox and then on how to get the value of the selected item. In this example we set the JComboBox component so that user can enter their own value.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class ComboBoxSelectedItem extends JFrame {
    public ComboBoxSelectedItem() {
        initialize();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new ComboBoxSelectedItem().setVisible(true));
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JComboBox Selected Item");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Create a combo box with four items and set it to editable so that 
        // user can
        // enter their own value.
        final JComboBox<String> comboBox = 
                new JComboBox<>(new String[]{"One", "Two", "Three", "Four"});
        comboBox.setEditable(true);
        getContentPane().add(comboBox);

        // Create two button that will set the selected item of the combo box. 
        // The first button select "Two" and second button select "Four".
        JButton button1 = new JButton("Set Two");
        getContentPane().add(button1);
        button1.addActionListener(e -> comboBox.setSelectedItem("Two"));

        JButton button2 = new JButton("Set Four");
        getContentPane().add(button2);
        button2.addActionListener(e -> comboBox.setSelectedItem("Four"));

        // Create a text field for displaying the selected item when we press 
        // the "Get Value" button. When user enter their own value the selected 
        // item returned is the string that entered by user.
        final JTextField textField = new JTextField("");
        textField.setPreferredSize(new Dimension(150, 20));

        JButton button3 = new JButton("Get Value");
        getContentPane().add(button3);
        getContentPane().add(textField);
        button3.addActionListener(
                e -> textField.setText((String) comboBox.getSelectedItem()));
    }
}

How do I create JComboBox?

A JComboBox allows user to select a value from a drop-down items available in the component. When the combo box set to editable user can enter their own value by typing it directly in the combo box editor. The code below demonstrate how you can create a simple combo box component.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class ComboBoxCreate extends JFrame {
    public ComboBoxCreate() {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JComboBox Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel label1 = new JLabel("Month  :");
        JLabel label2 = new JLabel("Number :");

        // Create some items for our JComboBox component. In this example we are
        // going to pass an array of string which are the name of the month.
        String[] months = {"January", "February", "March", "April", "Mei", "June",
                "July", "August", "September", "October", "November", "December"};

        // Create a month selection combo box.
        JComboBox<String> comboBox = new JComboBox<>(months);

        // Below, instead of passing directly a string array we create a ComboBoxModel
        // as the combo box data model. Using a model we can for example define the
        // selected item of the combo box.
        ComboBoxModel<String> model =
                new DefaultComboBoxModel<>(new String[]{"1", "2", "3", "4", "5"});
        model.setSelectedItem("3");
        JComboBox<String> numberComboBox = new JComboBox<>(model);

        // We also set the combo box to be editable so that user can enter their own
        // value other that those defined in the combo box.
        numberComboBox.setEditable(true);

        // Add the entire component to out frame content pane.
        getContentPane().add(label1);
        getContentPane().add(comboBox);

        getContentPane().add(label2);
        getContentPane().add(numberComboBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ComboBoxCreate().setVisible(true));
    }
}

How do I customize JButton icons?

The example below shows you how we can customize the icon for JButton Swing components.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class JButtonCustomIcon extends JFrame {
    public JButtonCustomIcon() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        JButton button = new JButton("Press Me!");

        // Below is how we can set various icons for the JButton swing
        // component. There are the default icon, selected, disabled, pressed
        // and rollover icons.
        button.setIcon(new ImageIcon("default.png"));
        button.setSelectedIcon(new ImageIcon("selected.png"));
        button.setDisabledIcon(new ImageIcon("disabled.png"));
        button.setDisabledSelectedIcon(new ImageIcon("disabledSelected.png"));
        button.setPressedIcon(new ImageIcon("pressed.png"));
        button.setRolloverIcon(new ImageIcon("rollover.png"));
        button.setRolloverSelectedIcon(new ImageIcon("rolloverSelected.png"));

        getContentPane().add(button);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JButtonCustomIcon().setVisible(true));
    }
}

How do I customize JCheckBox icons?

JCheckBox can have a different icon for its states, there are default icon, a selected icon, disabled icon, pressed icon or rollover icon, etc. See the code below and have a try on it.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class JCheckBoxCustomIcon extends JFrame {
    public JCheckBoxCustomIcon() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Icon Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        // Set default icon for checkbox
        checkBox.setIcon(new ImageIcon("icon.png"));
        // Set selected icon when checkbox state is selected
        checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
        // Set disabled icon for checkbox
        checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
        // Set disabled-selected icon for checkbox
        checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
        // Set checkbox icon when checkbox is pressed
        checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
        // Set icon when a mouse is over the checkbox
        checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
        // Set icon when a mouse is over a selected checkbox
        checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JCheckBoxCustomIcon().setVisible(true));
    }
}

How do I get or set the state of JCheckBox?

This simple example shows you how to get or set the state of a JCheckBox. The method to set the state is JCheckBox.setSelected(boolean) and the method for getting the state is JCheckBox.isSelected() which return a boolean value.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;

public class CheckBoxState extends JFrame {
    public CheckBoxState() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(500, 500);
        setTitle("JCheckBox Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        // Creating checkbox with text label
        JCheckBox checkBox = new JCheckBox("Check me!");
        checkBox.setSelected(true);

        // Get checkbox selection state
        boolean selected = checkBox.isSelected();
        if (selected) {
            System.out.println("Check box state is selected.");
        } else {
            System.out.println("Check box state is not selected.");
        }

        getContentPane().add(checkBox);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CheckBoxState().setVisible(true));
    }
}