-1

I want to display an Image, when a JComboxBox is selected. When I select "Tesla Model S" from the combo box I want to display an Image above the combo.

Adding the Price into the JTextField works fine. I'm writing a Program where I can select a Car and accessories. After I had selected the two Items, the price will show up in both text fields and I can add them.

Unfortunately I can't post any picture to show you my example.

I'm sorry if there is a lot of mess in my code. I tried different things but none of them worked

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Auto extends JFrame implements ActionListener, ItemListener {

    JTextField AutoFeld;
    JTextField AussFeld;
    JTextField GesamtFeld;

    JButton ResButton;
    JButton ClearButton;

    JCheckBox displayButton;

    ImageIcon image1;
    ImageIcon image2;
    ImageIcon image3;

    JLabel label1;
    JLabel label2;
    JLabel label3;

    JLabel Ergebnis;
    JLabel GesamtLabel;

    JPanel panel0;

    StringBuffer choices;


    public Auto() {

        this.setTitle("Auto");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLayout(new GridLayout(0, 10));

        /*image1 = new ImageIcon(getClass().getResource("AMGgt.jpg"));
        image2 = new ImageIcon(getClass().getResource("AudiA7.jpg"));
        image3 = new ImageIcon(getClass().getResource("TeslaS.jpg"));
        */
        GesamtLabel = new JLabel("Gesamt");

        image1 = new ImageIcon(getClass().getResource("AMGgt.jpg"));
        label1 = new JLabel(image1);
        image2 = new ImageIcon(getClass().getResource("AudiA7.jpg"));
        label2 = new JLabel(image2);
        image3 = new ImageIcon(getClass().getResource("TeslaS.jpg"));
        label3 = new JLabel(image3);

        AutoFeld = new JTextField("0", 10);
        AutoFeld.setEditable(true);
        AussFeld = new JTextField("0", 10);
        AussFeld.setEditable(true);
        GesamtFeld = new JTextField("0", 15);
        GesamtFeld.setEditable(false);

        ResButton = new JButton("Ergebnis");
        //ResButton.addActionListener(this);
        ResButton.addActionListener(e -> {
            Object source = e.getSource();
            String s1 = AutoFeld.getText();
            String s2 = AussFeld.getText();
            int o1 = Integer.parseInt(s1);
            int o2 = Integer.parseInt(s2);
            if (source == ResButton) {
                GesamtFeld.setText("" + (o1 + o2));
            }
        });
        ClearButton = new JButton("Clear");
        //ClearButton.addActionListener(this);
        ClearButton.addActionListener(e -> {
            Object source = e.getSource();
            if (source == ClearButton) {
                AutoFeld.setText("0");
                AussFeld.setText("0");
                GesamtFeld.setText("0");
            }
        });

        displayButton = new JCheckBox("Helles Display");
        displayButton.setSelected(true);

        JPanel panel0 = new JPanel();
        panel0.add(label1);
        panel0.add(label2);
        panel0.add(label3);

        JPanel panel1 = new JPanel();

        panel1.setLayout(new GridLayout(3, 2));
        String[] Autos = {"---Bitte Model auswählen---", "Tesla Model S", "Mercedes-AMG GT", "Audi A7"};
        JComboBox AutoList = new JComboBox(Autos);
        AutoList.addActionListener(e -> {
            Object source = e.getSource();
            JComboBox selectedChoice = (JComboBox) e.getSource();
            if ("Tesla Model S".equals(selectedChoice.getSelectedItem())) {
                AutoFeld.setText("108420");
                label1.getIcon();

            } else if ("Mercedes-AMG GT".equals(selectedChoice.getSelectedItem())) {
                AutoFeld.setText("134351");
                label2.getIcon();
            } else if ("Audi A7".equals(selectedChoice.getSelectedItem())) {
                AutoFeld.setText("58350");
                //label3.getIcon();
            }
        });

        panel1.add(AutoList);
        AutoList.setSelectedIndex(0);
        //AutoList.addActionListener(this);
        panel1.add(AutoFeld);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(3, 2));
        String[] Ausstattung = {"---Bitte Ausstattung auswählen---", "Sportsitze", "Navigationsgerät", "Kaffeehalter"};
        JComboBox AussList = new JComboBox(Ausstattung);
        panel2.add(AussList);
        AussList.setSelectedIndex(0);
        AussList.addActionListener(e -> {
            JComboBox selectedChoice = (JComboBox) e.getSource();
            if ("Sportsitze".equals(selectedChoice.getSelectedItem())) {
                AussFeld.setText("1679");
            } else if ("Navigationsgerät".equals(selectedChoice.getSelectedItem())) {
                AussFeld.setText("90");
            } else if ("Kaffeehalter".equals(selectedChoice.getSelectedItem())) {
                AussFeld.setText("20");
            }
        });
        panel2.add(AussFeld);

        JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayout(3, 2));
        panel3.add(GesamtLabel);
        panel3.add(GesamtFeld);

        JPanel panel4 = new JPanel();
        panel4.add(ResButton);
        panel4.add(ClearButton);

        JPanel panel5 = new JPanel();
        panel5.add(displayButton);
        displayButton.setSelected(true);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(panel0);
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(panel4);
        panel.add(panel5);
        panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
        setContentPane(panel);

        pack();
        setResizable(true);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //JComboBox selectedChoice = (JComboBox) e.getSource();
        Object source = e.getSource();
        JComboBox selectedChoice = (JComboBox) e.getSource();
        String s1 = AutoFeld.getText();
        String s2 = AussFeld.getText();
        double o1 = Double.valueOf(s1);
        double o2 = Double.valueOf(s2);

             /*if(source == ClearButton) {
                AutoFeld.setText("0");
                AussFeld.setText("0");
                GesamtFeld.setText("0");
            } */
    }

    public static void main(String[] args) {
        JFrame myApplication = new Auto();
        myApplication.setVisible(true);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it .. – Andrew Thompson Feb 23 '19 at 23:13
  • .. is an `UPPER_CASE_CONSTANT`) and use it consistently. 4) For the little I understand about this question so far, I'd recommend putting a single label above the combo for displaying the image of the car, then set the image (/ image icon) as needed. – Andrew Thompson Feb 23 '19 at 23:14

1 Answers1

0

If you want to display one image, you only need one JLabel, so remove label2 and label3.
Initialize label1 with out any icon : label1 = new JLabel();
and have the action listener set the icon:

   AutoList.addActionListener(e -> {  
        Object source = e.getSource();
        JComboBox selectedChoice = (JComboBox) e.getSource();
        if ("Tesla Model S".equals(selectedChoice.getSelectedItem())) {
            AutoFeld.setText("108420");
            label1.setIcon(image1);
        } else if ("Mercedes-AMG GT".equals(selectedChoice.getSelectedItem())) {
            AutoFeld.setText("134351");
            label1.setIcon(image2);
        } else if ("Audi A7".equals(selectedChoice.getSelectedItem())) {
            AutoFeld.setText("58350");
            label1.setIcon(image3);
        }
    });

You will also need to change the code of the clear button.

c0der
  • 18,467
  • 6
  • 33
  • 65