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

Java Combobox With Images

How to Add Images to a Combobox In Java Netbeans

How to Add Images to a Combobox In Java Netbeans


In this Java Tutorial we will see How To Create a JCombobox With Images 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.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class ComboboxWithImagesFrame {
    
    // Custom renderer for displaying images in the combo box
    static class ImageComboboxRenderer extends DefaultListCellRenderer{
        
        
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus){
            
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
            
            if(value instanceof ImageItem item){
                label.setText(item.getText());
                label.setIcon(item.getImageIcon());
            }
            
            return  label;  
        }
    }
    
    
    // Method to load images
    private static ImageIcon loadImage(String imageName){
        // Load image from the "images" package
        String imagePath = "images/" + imageName;
        return new ImageIcon(ComboboxWithImagesFrame.class.getResource(imagePath));
        
    }
    
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Combobox With Images");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,250);
        frame.setLocationRelativeTo(null);
        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        
        List<ImageItem> items = new ArrayList<>();
        items.add(new ImageItem("analyst", loadImage("analyst.png")));
        items.add(new ImageItem("designer", loadImage("designer.png")));
        items.add(new ImageItem("developer", loadImage("developer.png")));
        items.add(new ImageItem("manager", loadImage("manager.png")));
        
        JComboBox<ImageItem> comboBox = new JComboBox<>(items.toArray(ImageItem[] :: new));
        comboBox.setRenderer(new ImageComboboxRenderer());
        comboBox.setBackground(Color.WHITE);
        comboBox.setPreferredSize(new Dimension(150, 50));
        
        panel.add(comboBox);
        frame.add(panel);
        frame.setVisible(true);
        
    }
    
 
}


    // Define a simple data class to represent items in the combo box
    class ImageItem{
        
        private final String text;
        private final ImageIcon imageIcon;
        
        // Constructor
        public ImageItem(String text, ImageIcon icon){
            this.text = text;
            this.imageIcon = icon;
        }
        
        // Getters
        public String getText(){ return text; }
        
        public ImageIcon getImageIcon(){ return imageIcon; }
            
    }
    


The Final Result:

Java Combobox With Images