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




Java - Create Rounded Buttons

How to Create and Design Rounded Button In Java Netbeans

How to Create a Rounded JButton Using Java Swing


In this Java Tutorial we will see How To Create two rounded Jbuttons, and clicking each button will trigger a message dialog.
The rounded buttons have a gradient-colored background

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.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicButtonUI;

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

    public RoundedButtonFrame(){
        
        setTitle("Rounded Button Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,150);
        setLocationRelativeTo(null);
        initializeUI();
    }
    
    private void initializeUI(){
        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER,20,40));
        
        RoundedButton button1 = new RoundedButton("Button 1");
        button1.setBackground(new Color(255,69,96));
        
        RoundedButton button2 = new RoundedButton("Button 2");
        button2.setBackground(new Color(70,130,180));
        
        button1.addActionListener((e) -> {
            JOptionPane.showMessageDialog(this, "Button 1 Clicked");
        });
        
        button2.addActionListener((e) -> {
            JOptionPane.showMessageDialog(this, "Button 2 Clicked");
        });
        
        panel.add(button1);
        panel.add(button2);
        
        add(panel);
        
    }
    
    public static void main(String[] args) {
        RoundedButtonFrame frame = new RoundedButtonFrame();
        frame.setVisible(true);
    }

}


// Create a custom JButton class for rounded buttons
class RoundedButton extends JButton{
    
    public RoundedButton(String text){
        
        super(text);
        setUI(new RoundedButtonUI());
        setFont(new Font("Arial",Font.BOLD, 16));
        setForeground(Color.WHITE);
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        
    }
    
}


// Create a custom UI class for rendering rounded buttons
class RoundedButtonUI extends BasicButtonUI{
    
    @Override
    public void installUI(JComponent c){
        super.installUI(c);
        AbstractButton button = (AbstractButton) c;
        
        button.setOpaque(false);
        button.setBorderPainted(false);
    }
    
    
    @Override
    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text){
        super.paintText(g, c, textRect, text);
    }
    
    
    @Override
    public void paint(Graphics g, JComponent c){
        AbstractButton btn = (AbstractButton) c;
        // Paint the background with rounded corners
        paintBackground(g, btn, btn.getModel().isPressed() ? 2 : 0);
        super.paint(g, c);
    }
    
    // Method to paint the background with rounded corners
    private void paintBackground(Graphics g, JComponent c, int yOffset){
        
        Dimension size = c.getSize();
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     
        // Fill a rounded rectangle with a darker color
        GradientPaint gradientPaint = new GradientPaint(0, yOffset, c.getBackground().brighter(), 0,size.height - yOffset,c.getBackground().darker());
        g2d.setPaint(gradientPaint);
        g2d.fillRoundRect(0, yOffset, size.width, size.height - yOffset, 25, 25);
        g2d.dispose();
    }
    
}



The Final Result:

Java Rounded Button




Java - Fonts Selector with Combobox

How to Create a Font Chooser ComboBox In Java Netbeans

How to Create a Font Chooser ComboBox In Java Netbeans


In this Java Tutorial we will see How To Create a graphical user interface for selecting and displaying different fonts using a JComboBox In Java Using Netbeans.
We Will Retrieves all the available font family names from the local graphics environment, and Creates a JComboBox (fontsComboBox) with the array of font family names.
When a font family is selected from the combo box, a JLabel in the frame is updated to use the selected font family.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class FontsComboBox extends JFrame{
    
    private final JLabel label;
    
    
    public FontsComboBox(){
        
        setTitle("Fonts Combobox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Create an array of font family names
        String[] fontFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        // Create a JComboBox with a custom renderer
        JComboBox<String> fontsComboBox = new JComboBox<>(fontFamilies);
        fontsComboBox.setRenderer(new FontComboBoxRenderer());
        
        fontsComboBox.addActionListener((e) -> {
           
            String selectedFontFamiliy = (String)fontsComboBox.getSelectedItem();
            updateLabelFont(selectedFontFamiliy);
            
        });
        
        label = new JLabel("Simple Text");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(fontsComboBox, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        getContentPane().add(panel);
        setSize(400,300);
        setLocationRelativeTo(null);
        
    }
    
    
    private void updateLabelFont(String fontFamily){
        // Update the font of the label based on the selected font family
        Font font = new Font(fontFamily, Font.PLAIN, 18);
        label.setFont(font);
    }
    
    
    private static class FontComboBoxRenderer extends DefaultListCellRenderer{
        
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus){
            // Use the default rendering for the item text
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
            
            // Set the font for the item text based on the font family
            String fontFamily = (String) value;
            Font font = new Font(fontFamily, Font.PLAIN, 24);
            label.setFont(font);
            return label;
        }
        
    }
    

    public static void main(String[] args) {
        
        FontsComboBox frame = new FontsComboBox();
        frame.setVisible(true);
        
    }
    
}


The Final Result:

Java - Fonts Selector with Combobox

How To Select And display Different Fonts Using a JComboBox In Java Using Netbeans

Select And display Different Fonts Using a JComboBox In Java Using Netbeans

How To Select And display Different Fonts Using a JComboBox In Java

Select And display Different Fonts Using a JComboBox In Java










Free Python Projects Source Code

Download Free Python Projects Source Code

Free Python Projects Source Code



here's a list of free python projects source code + video tutorial.

this list contains python games, mini apps, form designs.

if you want to get premium python projects source code click HERE .


 1 - Python Quiz App Project Source Code 


How to Make a Simple Quiz App Using Python And Tkinter.


 2 - Python Calculator Project Source Code 


How To Make A Calculator With Swith  To Do The Basic Operations (+, -, /, *) Using Python Tkinter.


 3 - Python Rock Paper Scissors Game Source Code 


How to Make a Simple Rock, Paper, Scissors Game Using Python Tkinter.


 4 - Python Login & Register Form With MySQL DataBase Source Code 


How To Make a Design a Login Form And a Register Form In Python Tkinter, And How To Connect Those Two Form With MySQL Database.


 5 - Python Drawing App Source Code 


How to Make Drawing Application Using Python And the Tkinter library.


 6 - Python Expenses And Incomes Tracker Project Source Code 


How To Create A Simple Expenses and Incomes Tracker Application Using Python And Tkinter.


 7 - Python Guess The Word Game Source Code 


How to Make a Simple Word Guessing Game App In Python Tkinter.


 8 - Python Dashboard Form Design Source Code 


How To Design A Dashboard Form In Python Tkinter.


 9 - Python Image Editor Project Source Code 


How To Ceate A Simple Photo Editor Application Using Tkinter And The Pillow Library.





▶ Download Premium Python Project Source Code