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);
}
}