Results 1 to 14 of 14
  1. #1
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Unhappy Swing Button and Text Area Function

    Hello everybody,

    I started learning Java two months ago and started Swing today :)

    After several attempts on how to give a button a function, I succeeded:

    Java Code:
    JButton generate = new JButton("Generate");
                    generate.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        
                    }
                    });
    Now, my doubt is how to change the text of a text area when pressing a button.

    So, for example:

    The user opens the program and the text "HELLO DUDE 1" is written in a text area. When the button is clicked the text of a textArea will be changed to "YOU PRESSED THIS BUTTON". How can I do it? In C# this would be very easy but I'm new to Java GUI and to Java-Forums.org as well :)

    Thanks in advance,

    ScoutDavid

  2. #2
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Once you pressed the button, you will call the actionPerformed method, do in it whatever you want...

    Java Code:
    public void actionPerformed(ActionEvent ev) {
            // catch your JTextArea in the variable textArea
            textArea.setText("Whatever you want");
        }

  3. #3
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - local variable TextArea is accessed from within inner class; needs to be declared final
            at javagui.Main$1.actionPerformed(Main.java:24)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6267)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
            at java.awt.Component.processEvent(Component.java:6032)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4630)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2478)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    I tried that and got this :(

  4. #4
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Learn reading the console output, the solution is in the very first line of your error...

  5. #5
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - local variable TextArea is accessed from within inner class; needs to be declared final
    Yes, of course, I do understand what I need to do (theorically):

    Java Code:
    public void actionPerformed(ActionEvent ev) {
                        TextArea.setText("Hello");
                    }
    This is my code and I use TextArea which is defined outside the function, inside the function. This can't be done but it's what I need to do :s Any ideas?

  6. #6
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    "local variable TextArea is accessed from within inner class; needs to be declared final"

    Note that when you access a local variable from within an inner class, you need to declare it as final to refer to the same object during its lifetime.

    Also, rename your variable so that the first letter is never a capital. Else it is seen as a Class Type.

    edit: posting your entire class would help =)

  7. #7
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    final JTextArea TextArea = new JTextArea("Hello",5,20);
    Thanks much

  8. #8
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Please make a habbit of naming variables without capitals, and also declare the variable all the way, such as this:

    private final JTextArea textArea = new JTextArea("Hello", 5, 20);

    You will understand why once you program some more :)
    Cheers

  9. #9
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by fex View Post
    Please make a habbit of naming variables without capitals, and also declare the variable all the way, such as this:

    private final JTextArea textArea = new JTextArea("Hello", 5, 20);

    You will understand why once you program some more :)
    Cheers
    Thanks.

    In fact I always do that, this was a rare example.

    I got used to that in .NET because they were automatically defined as so:

    textBox1
    webBrowser1
    richTextBox1
    etc.

  10. #10
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Ah, good to hear :)

    Btw, I don't know what Java editor you are using, but a somewhat smarter editor would have noticed that you needed to make the variable final. Or maybe you just didn't see it because you 're new to Java...

  11. #11
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default New Doubt

    Hello everyone,

    Now I want to use the text in a text field!!

    So, for example,

    I have a textfield and what to define the text that is there to a variable.

    1. User Inputs text;
    2. User presses button;
    3. Program stores the text in the JTextField to a variable;
    4. ...

    How to do it?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you gone through the Oracle Swing tutorials on how to use text fields, buttons and action listeners? If not, you need to start there.

  13. #13
    ScoutDavid is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    I already got it.

    It's textField.getText() :D

  14. #14
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Have you ever heard of an API ? All the methods that you can perform on a class object are listed there...
    Java Platform SE 6

Similar Threads

  1. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  2. Replies: 0
    Last Post: 09-28-2010, 10:52 AM
  3. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 11:59 PM
  4. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 10:42 PM
  5. textfiled and text area to UTF-16?
    By Mr tuition in forum AWT / Swing
    Replies: 0
    Last Post: 12-04-2007, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •