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 11 of 11

Thread: ActionListener help

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry ActionListener help

    I receive this error from within my listener

    CallWindow.java:94: local variable txtStartTime is accessed from within inner class; needs to be declared final
    String startTime = txtStartTime.getText();
    ^
    startTime is outside this class because they are data that comes from JTextFields residing in a class method. Upon clicking a button, I need to get the code from a text box to manipulate the data.
    Here's a code snippet.

    add(new JLabel("Label 1"));
    JTextField txtStartTime = new JTextField(8);
    txtStartTime.addActionListener(listener);

    // button code here

    button.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){

    String startTime = txtStartTime.getText();

    Any ideas how I can get to the data in the Jtext fields?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: ActionListener help

    Two choices.

    Do as the error message says and make the variable final (the textfield not the string)
    Make the textfield an instance variable and not local to whatever constructor/method you are in.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Well, I got this one to work, a new problem emerges.

    Inside my ActionPerformed method I'm trying to call my CheckedTime fromString method which will instantiate a new PhoneCall object. However, my code is doing wierd things and not working...surprise. Here's the call to the method, but I can't figure out why its not working.

    This is from my CheckedTime class:

    public static CheckedTime fromString(String time) {

    //code

    }

    This is in my CallWindow class inside which has the actionPerformed method below.

    public void actionPerformed(ActionEvent e) {

    CheckedTime callStart = new CheckedTime.fromString(startTime);

    startTime is from the data from my TextField

    String startTime = txtStartTime.getText();

    Any assistance is appreciated.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: ActionListener help

    CheckedTime callStart = new CheckedTime.fromString(startTime);
    Using the new keyword means that you are creating a new object on that line. But your fromString method is creating and returning the object. It is a simple fix, you just need to delete ......
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,610
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: ActionListener help

    I can't figure out why its not working.
    Can you explain what does happen? "not working" has too many possibilities.

    my CheckedTimefromString method which will instantiate a new PhoneCall object
    Where is the PhoneCall object? the CheckedTimefromString method returns a CheckedTime object.

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    The fromString() returns another object that it is creating, which I will use to create a new object. So eventually I'll have two checkedTime objects, which I will use to create an object with these two. I don't see any difference in the info provided above than from what I originally posted.

    I have 3 classes I'm working with. The below line is in an actionListener within one class that also contains my main. CheckedTime is another class, PhoneCall is another class.

    CheckedTime callStart = new CheckedTime.fromString(startTime); //The fromString() returns an object of type CheckedTime

    I'll do this twice to create a new object below which I'll do more work on later which is also in it's own class.
    PhoneCall phoneCall = new PhoneCall(callStart, callEnd);

  7. #7
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Duh..I now know what you mean by delete...I have a lot of expletives to call myself right now.

  8. #8
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Yeah, I just figured out what Junky was saying. So simple I missed it.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,610
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: ActionListener help

    Sorry, I miss what "is not working".

    BTW to call a static method of a class, you do not need to create an instance of the class.
    Just use the class name: ClassName.staticMethod(parms...);

  10. #10
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    I'm close now, but having troubles with another part of my program. I need to output all this information and format it properly. I have this block of code, but due to the location of variables inside and outside the ActionListener I can't access the variables properly.

    JTextField cost = new JTextField("0", 8);
    add(cost); //field to hold total cost

    DecimalFormat dollarFormat = new DecimalFormat("$####.##");
    output = dollarFormat.format(phoneCall.computeCost());
    cost.setText(output);

    If I put this inside my listener the cost field isn't displayed at all. If I put it outside the listener it can't access the phoneCall object and this can't computeCost(). If I split it up in any way the setText() won't work if it can't see the variable "output".

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: ActionListener help

    A solution is not to put any code into the actionPerfomed method other than a call to another method in the enclosing class. This method should have access to everything you need. Something like...
    class Foo {
        private JTextfield text;
        private JButton button;
     
        Foo() {
            text = new JTextField();
            button = new JButton();
            button.addACtionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    someMethod();
                }
            });
        }
     
        private void someMethod() {
            // do what you want here
        }
    }
    Improving the world one idiot at a time!

Similar Threads

  1. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:25 AM
  2. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  3. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM
  4. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 07:21 PM
  5. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 10:00 AM