wood burning stoves 2.0
The moose likes Beginning Java and the fly likes JButton, JTextField, ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JButton, JTextField, ActionListener" Watch "JButton, JTextField, ActionListener" New topic

JButton, JTextField, ActionListener

Justin Yang

Joined: May 07, 2011
Posts: 3

Hi, there:
I'm now learning Java AWT from scratch by myself. Now I just encounter a problem which I don't know why. I just wanna create two JButtons marked with "1" and "2", and a JTextField; what's more, I wish if I click Jbutton"1", then there'll be a "1" showed in this JTextfield and "2" for the similar action, at least in theory. Unfortunately, when running my program after compiling with JDK 1.6, I get only a "2" showed in my JTextField no matter which JButton I click. So, with my code showing below, could you help me, please? Thanks in advance.

We are all in the gutter, but some of us are looking at the stars.
----------Oscar Wilde
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

Ok, pay attention to where your braces are then ask yourself how many showtext objects you have. Will there be two? One for each CtextAction class? Or one total for everything to share?

SCJA 6 (Studying for SCJP 6)
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
You may be learning AWT from scratch, but JButton and JTextField are Swing components, extensions of their AWT predecessors, Button and TextField. You're using other Swing components in your program. Swing components usually start with the letter 'J'. Be careful not to mix Swing components with AWT components in the same program. Results can be unpredictable and returning the program's performance to predictable can be painful. There are many good Swing tutorials on the web, in the Oracle Java tutorials, and probably here at the Ranch, to aid in your quest to learn Swing, and there's always excellent support here at the Ranch.

Good luck!

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Justin Yang

Joined: May 07, 2011
Posts: 3

Thank you for your reply. I've only one showtext object which can be shared by two JButtons objects here. What's more, there are two CTextAction objects created and attached to each JButtton, namely Button1 and Button2; I wish that when I click the Button1, it'll answered and invoke CTextAction's Constructor to set the object showtext to "text" I give when I create CtextAction object, that's to say, "1".
P.S.: If I uncomment line 48 and recompile my code, there'll be "1" and "2" showed in terminal as soon as I run the program. I'm not sure why.
Justin Yang

Joined: May 07, 2011
Posts: 3

Well, I've found my "bug" in the code. Now I put the showtext into the class CtextAction, which fixes my problem:

From this I've learned that the Constructor will only be invoked once, the time an object created. So, when I click the Button1, only the method actionPerformed( ) will be invoked, not the Constructor CtextAction( ); I should put the showtext into class CtextAction to make it become an inner Attribute whose value differs in different objects, namely Button1 and Button2 , hence. Thank you for your help very much.
I agree. Here's the link: http://aspose.com/file-tools
subject: JButton, JTextField, ActionListener
It's not a secret anymore!