0

I'm trying to plug my code into my GUI at the moment i can't find a way to call text in the ActionListener with out breaking my code I know there are some things in findAndReplace() i will need to work on to get it to work in my GUI... but at the moment I'm just trying to account the ActionListener.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.*;



public class HangmanPanel extends JPanel {
    static Boolean FOUND;
    private static final long serialVersionUID = -5793357804828609325L;

    public static String answerKey() {
        //get random array element
        String array[] = new String[10];
        array[0] = "hamlet";
        array[1] = "mysts of avalon";
        array[2] = "the iliad";
        array[3] = "tales from edger allan poe";
        array[4] = "the children of hurin";
        array[5] = "the red badge of courage";
        array[6] = "of mice and men";
        array[7] =  "utopia"; 
        array[8] =  "chariots of the gods";
        array[9] =  "a brief history of time";

        ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
        Collections.shuffle(list);
        String s = list.get(0);
        return s;
    }

    public StringBuilder dashReplace(String s) {
        //replace non-white space char with dashes and creates StringBuilder Object
        String tW = s.replaceAll("\\S", "-"); 
        System.out.print(tW + "\n");  
        StringBuilder AnswerKey = new StringBuilder(tW);
        return AnswerKey;
    }





    public HangmanPanel(){
        this.setLayout(null);

        JLabel heading = new JLabel("Welcome to the Hangman App");
        JButton Button = new JButton("Ok");
        //get input
        Button.addActionListener(new input()); 

        JLabel tfLable = new JLabel("Please Enter a Letter:");


        JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString());

        JTextField text = new JTextField(10);


        heading.setSize(200, 50);
        tfLable.setSize(150, 50);
        text.setSize(50, 30);
        Button.setSize(60, 20);
        AnswerKey.setSize(200, 100);

        heading.setLocation(300, 10);
        tfLable.setLocation(50, 40);
        text.setLocation(50, 80);
        Button.setLocation(100, 85);
        AnswerKey.setLocation(100,85);

        this.add(heading);
        this.add(tfLable);
        this.add(text);
        this.add(Button);
        this.add(AnswerKey);
    }
    public class input implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // can't access text
            sc = text.getText();
            char ch = sc.charAt(0);
            findAndReplace(s, AnswerKey, input, ch);
        }


    } 
    public static int findAndReplace(String s, StringBuilder AnswerKey, String sc,
            char ch) {
        //find position of user input and replace
        int pos = -1;
        FOUND = false;
        while(true){
        pos = s.indexOf(sc, pos+1);
        if(pos < 0){

            break;
        }else{
            FOUND = true;
            AnswerKey.setCharAt(pos, ch);
        }

        }
        System.out.println(AnswerKey);
        return pos;
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sage1216
  • 83
  • 1
  • 2
  • 7
  • 1
    1) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout manages, or combinations of them, along with layout padding & borders for white space, to organize the components. 3) One line white-space is enough. – Andrew Thompson May 09 '13 at 13:36

2 Answers2

3

The text JTextField variable is declared inside of the constructor and is invisible to other methods of your class since it does not have class "scope". You need to make this variable a class field by declaring it in the class, not a constructor in order to give it visibility throughout the class.

e.g.,

public class HangmanPanel extends JPanel {
   private static final long serialVersionUID = -5793357804828609325L;
   private Boolean found; // this should not be static nor capitalized
   private JTextField text; // declare your class field

   // .... etc...

   public HangmanPanel() {
      this.setLayout(null);  // *** don't do this. Use layout managers.

      // ... etc

      // JTextField text = new JTextField(10);
      text = new JTextField(10);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

First of all do not use lowercase class names and uppercase variable names, your code is really hard to read.

You can use anonymous inner class to add event listener like this:

Button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // can't access text
        sc = text.getText();
        char ch = sc.charAt(0);
        findAndReplace(s, AnswerKey, input, ch);
    }
});

Mark text variable as final in order to access it in anonymous class:

final JTextField text = new JTextField(10);
hoaz
  • 9,883
  • 4
  • 42
  • 53
  • @Sage1216: still it will be better to use a class field as you may need to access the text field by multiple methods in your program. – Hovercraft Full Of Eels May 09 '13 at 13:52
  • Syntax error on token "}", delete this token... How ever this is the ending bracket for the previous method – Sage1216 May 09 '13 at 14:00
  • @Hovercraft Full Of Eels: I'm not sure how i would use a class field.. I'm only a beginner to java who was thrown into the deep end to soon... I have this hangman program done but it has no gui interface which is what my teacher wants... This is all very new to me. anything i know about gui's are things i learn yesterday... – Sage1216 May 09 '13 at 14:03
  • i think you mistyped something, `Button.addActionListener` should be a last call in your constructor – hoaz May 09 '13 at 14:03
  • @Hovercraft Full Of Eels: you post didn't appear for me till after i had fixed this issue with hoaz post "a last call in your constructor" or at the very least i didn't see it. Please do not take offense you were both very helpful... Beginning programmer such as my self are luck to have such a supportive programming community to help us, as the two of you have helped me. :) – Sage1216 May 09 '13 at 14:40