-1

I am very new to Java and Swing, so I am trying to make a little application to practice it. In it the user chooses a pizza type from a group of radio buttons then "orders" the pizza and an image to match the pizza type is displayed. My problem is that the image will not be displayed unless I re-size the window. Where am I going wrong?
Here's my source code:

package gui;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame{
    //all my variables
    private JRadioButton pepperoni;
    private JRadioButton cheese;
    private JRadioButton jalepeno;
    private JRadioButton sausage;
    private JRadioButton hawiian;
    private JRadioButton spinach;
    private ButtonGroup group;
    private ImageIcon pizza;
    private JLabel displayed;
    private JButton submit;
    public GUI () {
        super("Order a Pizza");
        setLayout(new FlowLayout());
        //give instructions and assign variables
        JLabel instructions = new JLabel("Choose which Pizza you want to order");
        add(instructions);
        pepperoni = new JRadioButton("Pepperoni Pizza", true);
        cheese = new JRadioButton("Cheese Pizza", false);
        jalepeno = new JRadioButton("Hot Jalepeno Pizza", false);
        sausage = new JRadioButton("Sausage Pizza", false);
        hawiian = new JRadioButton("Hawaii Style - PineApple and sausage", false);
        spinach = new JRadioButton("Spinach Pizza", false);
        submit = new JButton("Order Pizza");
        //add to jframe
        add(pepperoni);
        add(cheese);
        add(jalepeno);
        add(sausage);
        add(hawiian);
        add(submit);
        add(spinach);
        //add to button group
        group = new ButtonGroup();
        group.add(pepperoni);
        group.add(cheese);
        group.add(jalepeno);
        group.add(sausage);
        group.add(hawiian);
        group.add(spinach);
        //add event listeners
        pepperoni.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("pepperoni.jpg"))));
        cheese.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("download.jpg"))));
        jalepeno.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("jalepeno.jpg"))));
        sausage.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("sausage.jpg"))));
        hawiian.addItemListener(new RadioHandler(new ImageIcon(getClass().getResource("hawiian.png"))));
        submit.addActionListener(new SubmitHandler());
    }
    //class that sets file when radio is clicked
    public class RadioHandler implements ItemListener {

        ImageIcon file;
        //constructor for whatever image should be displayed when clicked
        public RadioHandler (ImageIcon img) {
            file = img;
        }
        //when clicked set that image
        public void itemStateChanged(ItemEvent event) {
            pizza = file;
            displayed = new JLabel(pizza);
        }

    }
    //class that displays image when user submits
    public class SubmitHandler implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            if (event.getSource() instanceof JButton) {//if the button is clicked - i dont think i really need this
                add(displayed);//add image
            }

        }

    }
}

Main method:

package main;
 import gui.GUI;
public class Main {

    public static void main(String[] args) {
        GUI go = new GUI();
        go.setSize(800, 600);
        go.setVisible(true);
    }

}
  • 1
    Wrap everythingy from main, inside `EventQueue.invokeLater(new Runnable () { // here goes all code from main } );` . See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more information. Moreover, try to use `ImageIO.read (... )` for loading images, since this will block till an image is fully loaded. Further, just use `JLabel.setIcon ( newImage )` instead of recreating the `JLabel` for no reasons – nIcE cOw Jul 18 '15 at 16:49
  • 1
    @nIcEcOw *"Further, just use JLabel.setIcon ( newImage ) instead of recreating the JLabel for no reasons"* My experimentation suggests that is the root cause of the observed behavior, and that the duplicate question linked above is certainly not a duplicate of *this* question.. Added 1 vote to reopen. – Andrew Thompson Jul 18 '15 at 18:30

1 Answers1

1

As mentioned by @nIcEcOw..

..just use JLabel.setIcon ( newImage ) instead of recreating the JLabel for no reasons

Here is a version of the code that implements that idea, but makes few other changes. It still needs to have the GUI creation put on the EDT as mentioned in comments, and other tweaks.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class GUI extends JFrame {

    //all my variables
    private JRadioButton pepperoni;
    private JRadioButton cheese;
    private JRadioButton jalepeno;
    private JRadioButton sausage;
    private JRadioButton hawiian;
    private JRadioButton spinach;
    private ButtonGroup group;
    private ImageIcon pizza;
    private JLabel displayed = new JLabel();
    private JButton submit;

    public GUI() {
        super("Order a Pizza");
        setLayout(new FlowLayout());
        //give instructions and assign variables
        JLabel instructions = new JLabel("Choose which Pizza you want to order");
        add(instructions);
        pepperoni = new JRadioButton("Pepperoni Pizza", true);
        cheese = new JRadioButton("Cheese Pizza", false);
        jalepeno = new JRadioButton("Hot Jalepeno Pizza", false);
        sausage = new JRadioButton("Sausage Pizza", false);
        hawiian = new JRadioButton("Hawaii Style - PineApple and sausage", false);
        spinach = new JRadioButton("Spinach Pizza", false);
        submit = new JButton("Order Pizza");
        //add to jframe
        add(pepperoni);
        add(cheese);
        add(jalepeno);
        add(sausage);
        add(hawiian);
        add(submit);
        add(spinach);
        //add to button group
        group = new ButtonGroup();
        group.add(pepperoni);
        group.add(cheese);
        group.add(jalepeno);
        group.add(sausage);
        group.add(hawiian);
        group.add(spinach);
        //add event listeners
        pepperoni.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 80))));
        cheese.addItemListener(new RadioHandler(new ImageIcon(getImage(20, 40))));
        jalepeno.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 20))));
        sausage.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 60))));
        hawiian.addItemListener(new RadioHandler(new ImageIcon(getImage(80, 40))));
        submit.addActionListener(new SubmitHandler());

        add(displayed);//add image
    }

    private static Image getImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    //class that sets file when radio is clicked
    public class RadioHandler implements ItemListener {

        ImageIcon file;

        //constructor for whatever image should be displayed when clicked
        public RadioHandler(ImageIcon img) {
            file = img;
        }

        //when clicked set that image
        public void itemStateChanged(ItemEvent event) {
            pizza = file;

        }
    }

    //class that displays image when user submits
    public class SubmitHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.getSource() instanceof JButton) {//if the button is clicked - i dont think i really need this
                displayed.setIcon(pizza);
            }
        }
    }

    public static void main(String[] args) {
        GUI go = new GUI();
        go.setSize(800, 600);
        go.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433