Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with scope, please help!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with scope, please help!

    I have this program with a fairly modest goal: There's a text field, and a set of keywords. Everytime the user types in the text field, my TestListener triggers and if the text in the field matches one of the predefined keywords, a new button needs to appear in a GridLayout that I've set up below the text field.

    However, my issue is that in the context of the program I've written, I can't tell how to add a button to my grid once a keyword is found. Specifically, please have a look at the createAndShowGUI method, and the "keywords" method.

    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
     
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
     
    import java.text.*;
     
     
    public class RecognizeText extends JPanel
                                        implements TextListener {
     
        private TextField textField;
     
        public RecognizeText() {
            super(new BorderLayout());
     
            textField = new TextField(30);
            textField.addTextListener(this);
     
            JPanel labelPane = new JPanel(new FlowLayout());
            labelPane.add(textField);
     
            add(labelPane, BorderLayout.NORTH);
     
        }
     
        public void textValueChanged(TextEvent e) {
     
            String currentText = textField.getText().toString();
     
            if (keywords(currentText)) {
                playSound(currentText + ".wav");
                textField.setText("");
            }
        }
     
        public static void createAndShowGUI() {
            JFrame frame = new JFrame("Austin's Program");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // This 5x5 panel will store the buttons I intend to add.
            JPanel wordPanel = new JPanel(new GridLayout(5,5));
     
            frame.add(new RecognizeText(), BorderLayout.NORTH);
            frame.add(wordPanel, BorderLayout.SOUTH);
     
            frame.setSize(new Dimension(600,600));
            frame.setVisible(true);
     
        }
     
        public boolean keywords(String toCheck) {
            if (toCheck.equals("one")) {
     
                //THIS IS WHERE I DONT KNOW WHAT TO PUT
     
                return true;
            } 
            if (toCheck.equals("two")) {
                return true;
            } 
            if (toCheck.equals("three")) {
                return true;
            } 
            return false;
        }
     
        // You can ignore this method entirely. It's unrelated to the issue.
        public static void playSound(String filename) {
            new PlaySound("Sounds/" + filename).start();
        }
     
        public static void main(String[] args) {
            createAndShowGUI();
        }
    }

    I'm also interested in being able to add Action listeners to these buttons, however I'm fairly confident I can work that part out myself once I learn how to do this!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Problem with scope, please help!

    //THIS IS WHERE I DONT KNOW WHAT TO PUT
    What do you want done at that location?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with scope, please help!

    Quote Originally Posted by Norm View Post
    What do you want done at that location?
    Good point. My explanation was a bit vague. At that spot, I need something like:

    wordPanel.add(new JButton(toCheck));

    But that's out of scope. So how can I accomplish that action?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Problem with scope, please help!

    Move the variable into scope or make a copy of it that is in scope.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with scope, please help!

    Quote Originally Posted by Norm View Post
    Move the variable into scope or make a copy of it that is in scope.
    I have no idea how I might accomplish either of those :/

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Problem with scope, please help!

    Where is the variable defined that is out of scope? Is it defined local to a method?
    Move it out of the method to the class level.

Similar Threads

  1. Scope problem
    By TP-Oreilly in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2011, 07:41 AM
  2. Variable Scope
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: October 5th, 2011, 04:08 PM
  3. Java Scope in future
    By Shemil in forum Member Introductions
    Replies: 2
    Last Post: July 20th, 2011, 04:45 PM
  4. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  5. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM