1

I have been trying this for hours. I want to change each of the JButton components to image buttons (They were radio buttons that's why the variable names don't make sense). The for loop inside is just displaying all the images under the button's. Each image is called Option1, Option2, etc. So what I have been trying to do is get rid of the for loop and just have 10 buttons with images on them that you can click.

If someone could help me figure that out that would be great. All I've been able to do is have the button created with an image but the button appears on a completely different window.

public class JDialog2 extends JDialog {

private final JPanel contentPanel = new JPanel();

/**
 * Create the dialog.
 */
public JDialog2(Quiz quiz) {

    setBounds(100, 100, 450, 600);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);

    JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
    lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
    lblNewLabel_1.setBounds(94, 10, 248, 15);
    contentPanel.add(lblNewLabel_1);
    ButtonGroup btGroup = new ButtonGroup();

    for(int i=1; i<=7; i++) {
          ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
          image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
          JLabel imageLablel1=new JLabel(image1);  
          contentPanel.add(imageLablel1);  
          imageLablel1.setBounds(29, 75 * i - 25, 50, 50); 
    }

    JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
    radioButton_1.setBounds(29, 25, 226, 23);
    contentPanel.add(radioButton_1);

    JButton radioButton_2 = new JButton("2.Charms");
    radioButton_2.setBounds(29, 50, 158, 23);
    contentPanel.add(radioButton_2);

    JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
    radioButton_3.setBounds(29, 75, 255, 23);
    contentPanel.add(radioButton_3);

    JButton radioButton_4 = new JButton("4.Divination");
    radioButton_4.setBounds(29, 100, 121, 23);
    contentPanel.add(radioButton_4);

    JButton radioButton_5 = new JButton("5.Herbology");
    radioButton_5.setBounds(29, 125, 179, 23);
    contentPanel.add(radioButton_5);

    JButton radioButton_6 = new JButton("6.History of Magic");
    radioButton_6.setBounds(29, 150, 158, 23);
    contentPanel.add(radioButton_6);

    JButton radioButton_7 = new JButton("7.Muggle Studies");
    radioButton_7.setBounds(29, 175, 121, 23);
    contentPanel.add(radioButton_7);

    JButton radioButton_8 = new JButton("8.Potions");
    radioButton_8.setBounds(29, 200, 121, 23);
    contentPanel.add(radioButton_8);

    JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
    radioButton_9.setBounds(29, 220, 255, 23);
    contentPanel.add(radioButton_9);

    JButton radioButton_10 = new JButton("10.Transfiguration");
    radioButton_10.setBounds(29, 245, 179, 23);
    contentPanel.add(radioButton_10);

    btGroup.add(radioButton_1);
    btGroup.add(radioButton_2);
    btGroup.add(radioButton_3);
    btGroup.add(radioButton_4);
    btGroup.add(radioButton_5);
    btGroup.add(radioButton_6);
    btGroup.add(radioButton_7);
    btGroup.add(radioButton_8);
    btGroup.add(radioButton_9);
    btGroup.add(radioButton_10);

    JButton btnCommit = new JButton("Commit");
    btnCommit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Question question = quiz.getNextQuestion();
            int choice = 0;
            Enumeration<AbstractButton> en = btGroup.getElements();
            while (en.hasMoreElements()) {
                AbstractButton ab = en.nextElement();
                choice++;
                if (ab.isSelected()) {
                    break;
                }
            }
            question.setSelectedAnswer(choice - 1);
            JDialog3 dialog3 = new JDialog3(quiz);
            dialog3.setVisible(true);
            exit();
        }
    });
    btnCommit.setBounds(300, 138, 93, 23);
    contentPanel.add(btnCommit);


}
public void exit(){
    this.setVisible(false);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user7520940
  • 81
  • 2
  • 9
  • [`JButton#setIcon`](https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#setIcon-javax.swing.Icon-)? – MadProgrammer Apr 07 '18 at 08:05
  • 1) For better help sooner, post 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). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with .. – Andrew Thompson Apr 07 '18 at 08:58
  • .. layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 07 '18 at 08:58
  • `They were radio buttons that's why the variable names don't make sense` - so change the name. I'm sure your editor has "change all" functionality. Make the code readable for us. – camickr Apr 07 '18 at 14:50

0 Answers0