Home > database >  Getter and setter not working for JButton
Getter and setter not working for JButton

Time:06-25

I am creating a maze, where a player can be moved using the arrow keys. When the program is started, a frame is created which contains two JPanels Menu and MazePanel. The menu has a button "One step back" which allows the player to undo the last move.

This button should be disabled using "setEnable(false)" before the first move and after it has been clicked once. After it has been clicked, it should be enabled again if the player moves again.

The Player class contains the boolean clickable = false and sets it to true after every move.

This is how the frame is created. Note that the mazepanel gets the menu instance as a parameter.

public class Main extends JFrame {
    public Main(){
       frame = new JFrame();

       menu = new Menu();
       frame.add(menu);
    
       mazepanel = new MazePanel(frame, menu);
       frame.add(mazepanel);
    }
}

The Button is created and disabledin the constructor of the Menu class and a getter and setter are created.

public class Menu extends JPanel {
    
    private JButton one_step_back;

    public Menu() {
        one_step_back = new JButton("One step back");
        one_step_back.setEnabled(false);
    }

    public JButton getOne_step_back() {
        return one_step_back;
    }

    public void setOne_step_back(JButton one_step_back) {
        this.one_step_back = one_step_back;
    }
}

The MazePanel class looks like this:

public class MazePanel extends JPanel {
    private JFrame frame;
    private JPanel menu;
    
    public MazePanel(JFrame frame, JPanel menu) {
        this.frame = frame;
        this.menu = menu;
        
        play = new Player(); 
    }

    public JPanel getMenu() {
        return menu;
    }

    public void setMenu(JPanel menu) {
        this.menu = menu;
    }
}

And the Player class: The function move gets called by a Keylistener every time a key is pressed. I'm just using one key in this case to keep it short.

I am trying to disable the button in menu from the Player class whenever a move is made. Right now. For now, I am just trying to activate the Button after a move, so don't worry about deactivating the button after it is clicked.

public class Player implements KeyListener{
    boolean clickable = false;
    private JPanel menu;

    public Player(){
         menu = panel.getMenu();
    }
    
    public void move() {
        clickable = true;
        menu.setOne_step_back(getOne_step_back().setEnable(clickable));

    }
    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            move();
        }
    }
}

However, this line in the Player class

menu.setOne_step_back(getOne_step_back().setEnable(clickable));

gets the following issue: The method getOne_step_back() is undefined for the type Player.

If I just use instead

menu.getOne_step_back();

I get the following:

The method One_step_back() is undefined for the type JPanel.

What do I have to change in order make the getter and setter work and enable the button?

CodePudding user response:

Thanks for your help in the comments. One issue was in fact that it has to be

private Menu menu;

For the second part, to disable the button, it is enough to use the code below, so there's no need to use a setter.

menu.getOne_step_back().setEnabled(true);
  • Related