I dont know how to kill this JOptionPane pop out eveytime i do something... it just pops

Object[] options = {"Option 1",
                    "Option 2"};
      Component frame = null;
      int n = JOptionPane.showOptionDialog(frame,
          "Chose Game Option \n *Option 1 - Enter Secret Word\n *Option 2 - Random word selection from secret list",
          "Chose game mode",
          JOptionPane.YES_NO_OPTION,
          JOptionPane.QUESTION_MESSAGE,
          null,     //do not use a custom Icon
          options,  //the titles of buttons
          options[0]); //default button title

Some lines below that i have this

private void TryLetterActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String letter = aLetter.getText();
    int charPos = 0;
    if(n == JOptionPane.YES_OPTION)
      {
          String text = (String)evt.getActionCommand();
             if (text.equals("Start"))
             {
             TryLetter.setText("Try letter");
             }
          String FindWord = JOptionPane.showInputDialog(null,"Enter secret word");
          MyMessage.setText("position is" + charPos);
          charPos = FindWord.indexOf(letter);
          if(charPos == 0) Char1.setText(letter);
          if(charPos == 1) Char2.setText(letter);
          if(charPos == 2) Char3.setText(letter);
          if(charPos == 3) Char4.setText(letter);
          if(charPos == 4) Char5.setText(letter);
          if(charPos == 5) Char6.setText(letter);

      }
      if(n == JOptionPane.NO_OPTION)
      {
          System.out.println("haha");
      }

basicly what i'm tryting to achive here is run program, window pops out with 2 options when you click option one then you enter a words that is going to be used in this aplication(game), and then you start playing, what happens though is you enter word, and when you click on Tryletter button this JOption pops out asking you to enter word again, and its like a loop does it all the time you click a button try letter... how to solve this ? so i enter word ONCE and i can start playing ?
Also when i put this JOption so its global, then i have bunch of errors on whole aplication

Recommended Answers

All 2 Replies

Member Avatar for ztini

As far as placing the JOptionPane as a global; you don't want to do this--not very good design. You could place the recipient of the JOptionPane string as a global. You'll also want to change the name of it from FindWord to findWord -- classes start with a capital, methods don't.

You'll want to invoke the JOptionPane within an appropriate constructor/method. For instance:

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class GameGUI extends JFrame {
	
	private String findWord;
	
	public GameGUI() {
		configFrame();
		getInput();
	}
	
	private void configFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		// etc.
	}
	
	private void getInput() {
		findWord = JOptionPane.showInputDialog(null,"Enter secret word");
	}
	
	public static void main(String[] args) {
		new GameGUI();
	}
}

ok got it thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.