How to Add Radiobuttons to a Combobox In Java Netbeans
In this Java Tutorial we will see How To Create a JCombobox With JRadioButtons Inside It In Java Using Netbeans.
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package new_tutorials;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.ListCellRenderer;
/**
 *
 * @author 1BestCsharp
 */
public class RadioComboboxFrame extends JFrame{
    private RadioComboboxElement selectedItem;
    private JComboBox<RadioComboboxElement> comboBox;
    private final JLabel selectedLabel;
    public RadioComboboxFrame(){
        // Set up the JFrame
        setTitle("Radio-Combobox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Set the layout manager for the JFrame
        setLayout(new FlowLayout());
        // Create a JComboBox and set its renderer
        comboBox = new JComboBox<>();
        comboBox.setRenderer(new RadioComboboxRenderer());
        // Add three selectable items to the combobox
        comboBox.addItem(new RadioComboboxElement("123", true));
        comboBox.addItem(new RadioComboboxElement("456", false));
        comboBox.addItem(new RadioComboboxElement("789", false));
        // Set the first item as the default selection
        selectedItem = comboBox.getItemAt(0);
        selectedItem.setSelected(true);
        // Add an item listener to handle item state changes
        comboBox.addItemListener((e) -> {
            RadioComboboxElement newItem = (RadioComboboxElement) comboBox.getSelectedItem();
            // Deselect the previously selected item
            if(selectedItem != null) { selectedItem.setSelected(false); }
            // Select the new item and update the reference
            if(newItem != null){ 
                newItem.setSelected(true);
                selectedItem = newItem;
            }
            // Update the label and repaint the combobox
            updateSelectedLabel();
            comboBox.repaint();
        });
        add(comboBox);
        // Create and add a label to display the selected item
        selectedLabel = new JLabel("Selected Item: " + selectedItem.getLabel());
        add(selectedLabel);
        setSize(300,200);
        setLocationRelativeTo(null);
    }
    // Method to update the selected item label
    private void updateSelectedLabel(){
        selectedLabel.setText("Selected Item: " + (selectedItem != null ? selectedItem.getLabel() : ""));
    }
    // Inner class representing elements in the combobox
    public static class RadioComboboxElement{
        private final String label;
        private boolean selected;
        public RadioComboboxElement(String label, boolean selected){
            this.label = label;
            this.selected = selected;
        }
        public String getLabel(){ return label; }
        public boolean isSelected(){ return selected; }
        public void setSelected(boolean selected){ this.selected = selected; }
    }
    // Inner class implementing the renderer for the combobox
    public class RadioComboboxRenderer extends JRadioButton implements ListCellRenderer<RadioComboboxElement>{
        @Override
        public Component getListCellRendererComponent(JList<? extends RadioComboboxElement> list, RadioComboboxElement value, int index, boolean isSelected, boolean cellHasFocus) {
            // Set the state of the radio button based on the element's selection
            setEnabled(list.isEnabled());
            setSelected(value.isSelected());
            setText(value.getLabel());
            // Set the background and foreground colors based on the selection state
            if(isSelected){
                setBackground(Color.BLACK);
                setForeground(Color.YELLOW);
            }
            else{
                setBackground(Color.RED);
                setForeground(Color.WHITE);
            }
            return this;
        }
    }
    public static void main(String[] args) {
        RadioComboboxFrame frame = new RadioComboboxFrame();
        frame.setVisible(true);
    }
}
The Final Result:
More Java Projects:
Download Projects Source Code
    
  
  
  


