ben
ben

Reputation: 97

Using JRadioButton to display images

I need help with a small little problem. I'm not sure how to display an image properly after clicking a JRadioButton. Nothing shows up after I click any button and idk where to begin. Any help is appreciated.

public class DisplayImage extends JFrame{
private JRadioButton pic1Button, pic2Button;
private ImageIcon pic1Image,pic2Image;
private JLabel pic1Label,pic2Label;


public DisplayImage(){
    super("JRadioButton Image");
    setLayout(new FlowLayout());

    pic1Button = new JRadioButton("pic1");
    pic1Button.addActionListener(new displayListener());

    pic2Button = new JRadioButton("pic2");
    pic2Button.addActionListener(new displayListener());
    ButtonGroup bg = new ButtonGroup();
    bg.add(pic1Button);
    bg.add(pic2Button);
    JPanel panel = new JPanel();
    panel.add(pic1Button);
    panel.add(pic2Button);
    add(panel);
}
private class displayListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==pic1Button){
            pic1Image = new ImageIcon(getClass().getResource("car.jpg"));
            pic1Label = new JLabel(pic1Image);
            add(pic1Label);
        }
        if(e.getSource()==pic2Button){
            pic2Image = new ImageIcon(getClass().getResource("plane.jpg"));
            pic2Label = new JLabel(pic2Image);
            add(pic2Label);
        }

    }

}
public static void main(String[]args){
    DisplayImage gui = new DisplayImage();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(300,300);
    gui.setVisible(true);
}

Upvotes: 2

Views: 1333

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

You'd probably need to call revalidate and repaint after adding the picture labels, but a better solution would be to add a single JLabel in the constructor (along with your radio buttons) and simply use it's setIcon method to change the image

  import java.awt.FlowLayout;
  import java.awt.event.ActionListener;
  import javafx.event.ActionEvent;
  import javax.swing.ButtonGroup;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JRadioButton;
  import static sun.misc.ClassFileTransformer.add;

  public class DisplayImage extends JFrame {

    private JRadioButton pic1Button, pic2Button;
    private JLabel pic1Label;

    public DisplayImage() {
      super("JRadioButton Image");
      setLayout(new FlowLayout());

      pic1Button = new JRadioButton("pic1");
      pic1Button.addActionListener(new DisplayListener());

      pic2Button = new JRadioButton("pic2");
      pic2Button.addActionListener(new DisplayListener());
      ButtonGroup bg = new ButtonGroup();
      bg.add(pic1Button);
      bg.add(pic2Button);
      JPanel panel = new JPanel();
      panel.add(pic1Button);
      panel.add(pic2Button);
      add(panel);
      pic1Label = new JLabel();
      add(pic1Label);
    }

    private class DisplayListener implements ActionListener {

      public void actionPerformed(ActionEvent e) {
        ImageIcon icon = null;
        if (e.getSource() == pic1Button) {
          icon = new ImageIcon(getClass().getResource("car.jpg"));
        }
        if (e.getSource() == pic2Button) {
          icon = new ImageIcon(getClass().getResource("plane.jpg"));
        }
        pic1Label.setIcon(icon);

      }

    }

    public static void main(String[] args) {
      DisplayImage gui = new DisplayImage();
      gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gui.setSize(300, 300);
      gui.setVisible(true);
    }
  }

Upvotes: 4

Related Questions