Adding value to JRadioButton

325 views Asked by At

I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i choose a team in group 1 and a team in group 2 then i click the simulate button a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;



public class sim extends JPanel {

  public sim() {
    // JFrame constructor
      super(true);

    JRadioButton chelsea, arsenal, chelsea2, arsenal2;

    this.setLayout(new GridLayout(3,0));

    ButtonGroup group = new ButtonGroup();
    ButtonGroup group2 = new ButtonGroup();

    // takes image and saves it the the variable
    Icon a = new ImageIcon(getClass().getResource("a.PNG"));
    Icon c = new ImageIcon(getClass().getResource("c.JPG"));


    chelsea = new JRadioButton("Chelsea",c);
    chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal = new JRadioButton("Arsenal",a);
    arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);

    group.add(chelsea);
    group.add(arsenal);

    JLabel label = new JLabel("");
    TitledBorder titled = new TitledBorder("Team 1");
    label.setBorder(titled);

    chelsea.setBorder(titled);
    arsenal.setBorder(titled);

    JButton button = new JButton("Simulate");
    button.setHorizontalAlignment(JButton.CENTER);
    add(button, BorderLayout.CENTER);

    chelsea2 = new JRadioButton("Chelsea",c);
    chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
    chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);

    arsenal2 = new JRadioButton("Arsenal",a);
    arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
    arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);

    group2.add(chelsea2);
    group2.add(arsenal2);

    JLabel label2 = new JLabel("");
    TitledBorder titled2 = new TitledBorder("Team 2");
    label2.setBorder(titled2);

    chelsea2.setBorder(titled);
    arsenal2.setBorder(titled);

    add(label);
    add(chelsea);
    add(arsenal);

    add(button);

    add(chelsea2);
    add(arsenal2);

    HandlerClass handler = new HandlerClass();
    chelsea.addActionListener(handler);
  }

  private class HandlerClass implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {

          //JOptionPane.showMessageDialog(null, String.format("%s",     e.getActionCommand()));

      }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Final");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1920, 1080);
    frame.setContentPane(new sim());
    frame.setVisible(true);
  }
}
2

There are 2 answers

0
g4genuis On

You can create a JLabel Associated with each of your JRadioButton

Let visibility of that JLabel be false so they are not visible.

Then you can compare values of corresponding JLabels on click of your JRadioButtons

1
skrtbhtngr On

If you want to associate an additional value with each JRadioButton, you can create a HashMap.

To associate a value with the JRadioButton:

HashMap<JRadioButton,Integer> = new HashMap<>();
hm.put(chelsea2, new Integer(10));
hm.put(arsenal2, new Integer(1));

To fetch the value of a particular JRadioButton (for example chelsea2):

int i = (Integer) hm.get(chelsea2);