Carson
Carson

Reputation: 1257

Waiting for a button to be pressed in Java Swing before moving on?

So I have this GUI

public ChangePokemonView(Controller c)
{
    this.controller = c;
    this.currentBattleEnvironment = currentBattleEnvironment.getInstance();
    populateInactivePokemon(); //REMOVE LATER REMOVE LATER  REMOVE LATER  REMOVE LATER  REMOVE LATER 
    this.pokemonList = new JList(inactivePlayerPokemon);
    pokemonList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //Only one thing can be selected at a time.
    this.pokemonLabel = new JLabel("Choose your Pokemon!");
    this.confirmSelection = new JButton("Confirm Selection");
    this.confirmSelection.addActionListener(this);

    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes GUI window when close, may need to change later
    JPanel centerPanel = new JPanel(new GridLayout(3, 1));
    centerPanel.add(pokemonLabel);
    centerPanel.add(pokemonList);
    centerPanel.add(confirmSelection);

    add("Center", centerPanel);
    pack();
    setVisible(true);
}

That just creates a list of items and a button. When the button is clicked, it will get the selected item and return it to a controller to then process it (in that, for our project it changes the players pokemon).

 */
@Override
public void actionPerformed(ActionEvent event)
{
    if (event.getSource() == confirmSelection)
    {
        this.pokemonSelected = (String) pokemonList.getSelectedValue();
        this.controller.setCurrentPokemon(this.pokemonSelected);
        //JOptionPane.showConfirmDialog(null, "You pressed: "+output); //USED FOR TESTING, THIS WILL OUPUT JUST THE NAME THAT WAS SELECTED
    }

}

The setCurrentPokemon has nothing really in it with the controller. I am just trying to make sure it can get the selection right now. However I am having trouble with the rest of the code waiting till the selection is made.

I thought Swing and input in general with Java should pause and wait for the input before moving on with the rest of the code. However, right now it runs opens the selection menu but then sets the selected pokemon to null in the controller. I thought about adding a while loop to kind of wait and get around this, but I feel like there is an easier way to do this built into Swing.

Is there a way so I can have the rest of my code wait until the button is selected and the action is handled?

Thanks in advance.

Upvotes: 2

Views: 220

Answers (1)

camickr
camickr

Reputation: 324108

Use a JOptionPane. It will build the modal dialog and buttons for you. A modal dialog stops execution until it is closed.

Read the section from the Swing tutorial on How to Make Dialogs for more information and working examples.

add("Center", centerPanel);

Don't use "magic" values. The API will define the values that should be used. That is also not the way to add components to a Container. Instead you should be using:

add(centerPanel, BorderLayout.CENTER);

Upvotes: 7

Related Questions