• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i set a text inside JTextField through ActionPerformed event (but outside editing area) ?  RSS feed

 
Adalberto José Brasaca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys...

I'm breaking my head here... I have a JTextField's 2D array and I need to put one word inside each one, but outside of editing area. Even doing JTextField's casting on "e.getSource()" the text is not printed in JTextField. If I using array's name and indexes directly, it works...

Below is the code...



Thanks a lot for any help.

 
Simon Roberts
Author
Ranch Hand
Posts: 176
9
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you didn't include, I believe that there's an instance variable "c" that is the container in which your general GUI components are being added. On that basis, you can add a new label to that in the way you describe when the line is commented out.

But you cannot add a component to another component in general, they can only be added to containers. JTextField will display text, not labels.

Try changing your last line to:



And see if it makes sense to you.
 
Adalberto José Brasaca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Roberts wrote:In the code you didn't include, I believe that there's an instance variable "c" that is the container in which your general GUI components are being added. On that basis, you can add a new label to that in the way you describe when the line is commented out.

But you cannot add a component to another component in general, they can only be added to containers. JTextField will display text, not labels.

Try changing your last line to:



And see if it makes sense to you.


Simon...

My JFrame's container is set to null. I can place text (Label) in JTextField (outside editing area - see attachments), but only by making direct reference to an element of the JTextField's array, as jt [0] [0] .add (label). I can not put the text using the element returned by the ActionEvent event, which returns the element's array clicked at that time (e.getSource ()), as mentioned in the above code. This is my question. Anyway, thanks for your feedback.

Texto.png
[Thumbnail for Texto.png]
 
Carey Brown
Saloon Keeper
Posts: 2538
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although a JTextField is technically a "container", adding a JLabel to it makes no sense. You can set the font on a JTextField and then use setText() to change the field's text.

You can add a JLabel to a container, such as JPanel, that would make sense.

I might add a println(e) inside your actionPerformed() method to see if the action you are getting is really what you're expecting.
 
Adalberto José Brasaca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Although a JTextField is technically a "container", adding a JLabel to it makes no sense. You can set the font on a JTextField and then use setText() to change the field's text.

You can add a JLabel to a container, such as JPanel, that would make sense.

I might add a println(e) inside your actionPerformed() method to see if the action you are getting is really what you're expecting.


Dear Carey...

I'm doing a college job (Distributed Systems). My group decided to make a game (Crosswords) where the user will create the words and he will have to indicate the word number and its direction, which is done by a context menu (see figures below). Does it make sense now? I don't know which JTextField the user will click to create a new word, but through the event (ActionEvent e) which returns JTextField the user clicked is not working. Thats it.

CW1.png
[Thumbnail for CW1.png]
CW2.png
[Thumbnail for CW2.png]
CW3.png
[Thumbnail for CW3.png]
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me like you are using a JPopupMenu, so the ActionEvent is for the menu item you added to the popup menu, not the text field.

If you want the text field then I think the code would be something like:



Or the better way would be to use an Action (which can also be used as the ActionListener)


This will return the last text component that had focus, which should be the text field that you generated the popup for.

Read the section from the Swing tutorial on How to Use Actions for more information.

I also am not sure why you are adding a label to the text field. Since it looks like you only have a single character I would just set the text and make the text field non-editable.

 
Adalberto José Brasaca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Looks to me like you are using a JPopupMenu, so the ActionEvent is for the menu item you added to the popup menu, not the text field.

If you want the text field then I think the code would be something like:



Or the better way would be to use an Action (which can also be used as the ActionListener)


This will return the last text component that had focus, which should be the text field that you generated the popup for.

Read the section from the Swing tutorial on How to Use Actions for more information.

I also am not sure why you are adding a label to the text field. Since it looks like you only have a single character I would just set the text and make the text field non-editable.



Camick,

Through its idea I got another way: KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

And it worked perfectly.

Thank you so much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!