user5260030
user5260030

Reputation:

Java: JComboBox selection issue on image

In this GUI there are four images which place on array of DefaultComboBoxModel and label which helps to display the image. The default image is 1.png which appear on the frame. But when I select the other image from the combo list then it doesn't appear.

Code

public class Combo extends JFrame {

    public Combo() {
        super("Combo 2 GUI");

        setSize(400, 300);
        MethodG();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void MethodG() {

        JLabel l1;
        JComboBox box;
        JPanel p;

        Container pane = getContentPane();

        p = new JPanel();

        box = new JComboBox();
        box.setModel(new DefaultComboBoxModel(new String[] { "1.png", "2.png", "3.png", "4.png" }));
        box.setMaximumRowCount(3);

        String boxoption = (String) box.getSelectedItem();

        Icon[] icons = { new ImageIcon(getClass().getResource(boxoption)) };

        // Default display
        l1 = new JLabel(icons[0]);

        box.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {

                    l1.setIcon(icons[box.getSelectedIndex()]);

                }

            }

        });

        pane.add(p);

        p.add(box);
        p.add(l1);

    }
}

Main method

public class Main {

    public static void main(String[] args) {

        Combo obj = new Combo();
    }
}

And the second problem what does these two lines do and how to write in if syntax.

JComboBox schemaBox;
String schema = (schemaBox.isEnabled() ? schemaBox.getSelectedItem().toString() : null);
String selectTable = (schema == null ? "" : schema + ".") + tableName;

Upvotes: 2

Views: 235

Answers (2)

mKorbel
mKorbel

Reputation: 109813

To the first question: You have (easiest or ways) to get the icon or image icon directly from DefaultComboBoxModel, for example,

.

Enter image description here

.

Enter image description here

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class ComboBoxModels {

    private JComboBox comboBoxDouble;
    private JComboBox comboBoxInteger;
    private JComboBox comboBoxBoolean;
    private JComboBox comboBoxIcon;
    private JComboBox comboBoxDate;
    private JLabel label =  new JLabel();
    private Vector<Double> doubleVector = new Vector<Double>();
    private Vector<Integer> integerVector = new Vector<Integer>();
    private Vector<Boolean> booleanVector = new Vector<Boolean>();
    private Vector<Icon> iconVector = new Vector<Icon>();
    private Vector<Date> dateVector = new Vector<Date>();
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    public ComboBoxModels() {
        doubleVector.addElement(1.001);
        doubleVector.addElement(10.00);
        doubleVector.addElement(0.95);
        doubleVector.addElement(4.2);
        comboBoxDouble = new JComboBox(doubleVector);
        integerVector.addElement(1);
        integerVector.addElement(2);
        integerVector.addElement(3);
        integerVector.addElement(4);
        comboBoxInteger = new JComboBox(integerVector);
        booleanVector.add(Boolean.TRUE);
        booleanVector.add(Boolean.FALSE);
        comboBoxBoolean = new JComboBox(booleanVector);
        iconVector.addElement(icon1);
        iconVector.addElement(icon2);
        iconVector.addElement(icon3);
        iconVector.addElement(icon4);
        comboBoxIcon = new JComboBox(iconVector);
        comboBoxIcon.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    Icon icon = (Icon) comboBoxIcon.getModel().getSelectedItem();
                    label.setIcon(icon);
                }
            }
        });
        dateVector.addElement(parseDate("25.01.2013"));
        dateVector.addElement(parseDate("01.02.2013"));
        dateVector.addElement(parseDate("03.03.2013"));
        dateVector.addElement(parseDate("18.04.2013"));
        comboBoxDate = new JComboBox(dateVector);
        comboBoxDate.setRenderer(new ComboBoxRenderer());
        JFrame frame = new JFrame("");
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBoxDouble);
        frame.add(comboBoxInteger);
        frame.add(comboBoxBoolean);
        frame.add(comboBoxIcon);
        frame.add(comboBoxDate);
        frame.add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private Date parseDate(String str) {
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException ex) {
        }
        return date;
    }

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;

        public ComboBoxRenderer() {
            setOpaque(true);
            setBorder(new EmptyBorder(1, 1, 1, 1));
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Date)) {
                return this;
            }
            setText(sdf.format((Date) value));
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxModels comboBoxModel = new ComboBoxModels();
            }
        });
    }
}

Upvotes: 4

Amber
Amber

Reputation: 2463

Your icons array isn't being initialized as you expect. The code you have is creating an array with only one entry - 1.png. You'll need to initialize your array to contain all the images. Something like this:

    Icon[] icons = { new ImageIcon(getClass().getResource("1.png")),  
            new ImageIcon(getClass().getResource("2.png")), 
            new ImageIcon(getClass().getResource("3.png")), 
            new ImageIcon(getClass().getResource("4.png"))};

Upvotes: 0

Related Questions