Results 1 to 14 of 14
- 12-20-2010, 06:47 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
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:
Now, my doubt is how to change the text of a text area when pressing a button.Java Code:JButton generate = new JButton("Generate"); generate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { } });
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
- 12-20-2010, 07:04 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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"); }
- 12-20-2010, 07:06 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
I tried that and got this :(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)
- 12-20-2010, 07:07 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
Learn reading the console output, the solution is in the very first line of your error...
- 12-20-2010, 07:10 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
Yes, of course, I do understand what I need to do (theorically):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
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?Java Code:public void actionPerformed(ActionEvent ev) { TextArea.setText("Hello"); }
- 12-20-2010, 07:15 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
"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 =)
- 12-20-2010, 07:18 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
Thanks muchJava Code:final JTextArea TextArea = new JTextArea("Hello",5,20);
- 12-20-2010, 07:20 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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
- 12-20-2010, 07:22 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
- 12-20-2010, 07:32 PM #10
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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...
- 12-20-2010, 10:53 PM #11
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
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?
-
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.
- 12-20-2010, 11:06 PM #13
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
I already got it.
It's textField.getText() :D
- 12-21-2010, 02:07 PM #14
Member
- Join Date
- Dec 2010
- Posts
- 20
- Rep Power
- 0
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
-
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
How display text in listbox or text area right to left or center??
By sameer22 in forum CLDC and MIDPReplies: 0Last Post: 09-28-2010, 09:52 AM -
eol in text area....
By Nicholas Jordan in forum NetworkingReplies: 0Last Post: 09-14-2008, 10:59 PM -
Text Area problem
By mcal in forum New To JavaReplies: 0Last Post: 02-11-2008, 09:42 PM -
textfiled and text area to UTF-16?
By Mr tuition in forum AWT / SwingReplies: 0Last Post: 12-04-2007, 12:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks