0

I have to make sure that when one presses the start game button opens the gaming frame.

This is the menu:

enter image description here

This is the controller class:

@Override
public void actionPerformed(ActionEvent e) {
    JRadioButton source = (JRadioButton)e.getSource();
    JButton sourceButton = (JButton)e.getSource();

    if(source.isSelected() && source.getText().equals("Custom")){
        MenuView.heightLabel.setEnabled(true);
        MenuView.height.setEnabled(true);

        MenuView.widthLabel.setEnabled(true);
        MenuView.width.setEnabled(true);

        MenuView.minesLabel.setEnabled(true);
        MenuView.mines.setEnabled(true);
    }
    else{
        MenuView.heightLabel.setEnabled(false);
        MenuView.height.setEnabled(false);

        MenuView.widthLabel.setEnabled(false);
        MenuView.width.setEnabled(false);

        MenuView.minesLabel.setEnabled(false);
        MenuView.mines.setEnabled(false);
    }

    if(source.getText().equals("Beginner")){
        if(sourceButton.getText().equals("Start Game")){
            MenuView.fullRandom = new FullRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
            Frame frame = new Frame(MenuView.fullRandom);
            frame.setSize(270, 380);
            frame.setResizable(false);
            frame.setVisible(true);

        }
    }   
}

The problem is that when I press Start Game I have this exception:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JRadioButton

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • Can you post the full stack trace? – Carter Brainerd Jun 21 '17 at 20:32
  • Why would you expect **e.getSource()** to return instance of **JButton** and **JRadioButton** too?? JRadioButton has no **inheritance** relationship with JButton, meaning the **object** returned by e.getSource() could only be a **JButton** or **JRadioButton** instance, which in my opinion is JButton though, because the user pressed a JButton :) – ShayHaned Jun 21 '17 at 20:34
  • *"..new JFrame.."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jun 21 '17 at 20:56

2 Answers2

1

Problem is in this line :

JRadioButton source = (JRadioButton)e.getSource();

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

JRadioButton is not an subclass of e.getSource(), so a ClassCastException will be thrown.


JRadioButton source = (JRadioButton)e.getSource();
JButton sourceButton = (JButton)e.getSource();

How e.getSource() can be cast in both, no child parent relationship.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • you've reference of your `JRadioButton` globally ? If not make it and use in your method. – Abhishek Aryan Jun 21 '17 at 20:43
  • Take a look of this part `MenuView.heightLabel`, heightLabel is static reference of `JLabel` so you're using with class name, In same way create reference of `JRadioButton` and use in `actionPerformed` method instead of `source` variable – Abhishek Aryan Jun 21 '17 at 20:50
0

The issue is in line JRadioButton source = (JRadioButton)e.getSource(); - you are pressing the JButton object, which causes event and then you are casting wrong JButton type to JRadioButton.

Ivan Pronin
  • 1,768
  • 16
  • 14