File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton, JTextField, ActionListener

 
Justin Yang
Greenhorn
Posts: 3
BSD Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Jared Malcolm
Ranch Hand
Posts: 54
Java MySQL Database PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Greg Brannon
Bartender
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Justin Yang
Greenhorn
Posts: 3
BSD Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
BSD Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic