Affichage des articles dont le libellé est Java JCombobox With JCheckboxes. Afficher tous les articles
Affichage des articles dont le libellé est Java JCombobox With JCheckboxes. Afficher tous les articles

Java Combobox With Checkbox Items

How to Add CheckBoxes to a Combobox In Java Netbeans

How to Add CheckBoxes to a Combobox In Java Netbeans



In this Java Tutorial we will see How To Create a Custom JCombobox With Checkable Options Using Checkboxes 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.Component;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author 1BestCsharp
 */
public class CheckComboBox extends JFrame{

    private CheckComboBoxElement selectedItem; // The currently selected item
    private JComboBox<CheckComboBoxElement> combobox; // JComboBox for the checkable items
    private final JLabel selectedLabel; // JLabel to display the selected item
    
    public CheckComboBox(){
        
        setTitle("Check Combobox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        
        combobox = new JComboBox<>();
        // Set custom renderer for the combo box
        combobox.setRenderer(new CheckComboBoxRenderer());
        // Add three checkable items to the combo box
        combobox.addItem(new CheckComboBoxElement("ABCD", true)); // Default selection
        combobox.addItem(new CheckComboBoxElement("EFGH", false));
        combobox.addItem(new CheckComboBoxElement("IJKL", 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) -> {
           
            CheckComboBoxElement newItem = (CheckComboBoxElement) combobox.getSelectedItem();
           // Deselect the previously selected item
            if(selectedItem != null){
                selectedItem.setSelected(false);
            }
            // Select the newly selected item and update the reference
            if(newItem != null){
                newItem.setSelected(true);
                selectedItem = newItem;
            }
            
            updateSelectedLabel();
            combobox.repaint();
            
        });
        
        add(combobox);
        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() : ""));
    }
    
    
    
    
    // Static nested class representing the checkable items in the combo box
    public static class CheckComboBoxElement{
        private final String label;     // Label for the item
        private boolean selected;       // Selection state of the item
        
        // Constructor for the checkable item
        public CheckComboBoxElement(String label, boolean selected)
        {
            this.label = label;
            this.selected = selected;
        }
        
        // Getters And Setters
        public String getLabel(){ return label;}
        
        public boolean isSelected(){ return selected;}
        
        public void setSelected(boolean selected){ this.selected = selected; } 
        
    }
    
    
    
    // Static nested class representing the custom renderer for the combobox
    private class CheckComboBoxRenderer extends JCheckBox implements ListCellRenderer<CheckComboBoxElement>{

        @Override
        public Component getListCellRendererComponent(JList<? extends CheckComboBoxElement> list, CheckComboBoxElement value, int index, boolean isSelected, boolean cellHasFocus) {
            
            setEnabled(list.isEnabled());
            setSelected(value.isSelected());
            setText(value.getLabel());
            
            // Set background and foreground based on selection state
            if(isSelected){
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            }
            else{
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            
            return this; // Return the renderer component
            
        }
        
    }
    
    public static void main(String[] args) {
        
        CheckComboBox frame = new CheckComboBox();
        frame.setVisible(true);
        
    }
    
    
}


The Final Result:

Java Combobox With Checkboxes