File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JButton, JTextField, ActionListener" Watch "JButton, JTextField, ActionListener" New topic
Author

JButton, JTextField, ActionListener

Justin Yang
Greenhorn

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
Bartender

Joined: Oct 24, 2010
Posts: 557
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!


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Justin Yang
Greenhorn

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
Greenhorn

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
 
Similar Threads
How can I get the GridLayout to work in this program
Problem with the JButton.
Enter # in JTextField, draw = # ovals, use JBottons to color ovals???
Swing Program with Buttons
ActonListener