Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Populate the 'current' JTextField via a button/label Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populate the Watch "Populate the New topic
Author

Populate the 'current' JTextField via a button/label

Lee Quinlan
Greenhorn

Joined: Aug 25, 2009
Posts: 9
This is my first post.! I'm hoping it's clear and concise enough for constructive replies !
I'm working on a very simple app for my PPC, trying to make best use of screen real estate and simplify text entry.
The application has a simple form with numerous text fields and I'd like to be able to click a button/label titled 'N/A' to populate the textfield that currently has focus with the same value ('N/A').
In other words, I want to click in the text field I want to popluate with N/A and instead of using the SIP keyboard and typing 'N/A' every time, I want to use a single '"N/A' button/label for any field.
I'm trying to write efficient code and so trying to avoid multiple buttons that just populate their neighboring textfield.
I've done plenty of goolge'ing before making this post but couldn't find anything that fit my scenario.
I get the feeling I should be capturing focus events, determining the component name that triggered the event and then '.setText'ing that component.
Easier said than done for me though.
Any help greatly appreciated.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Welcome to JavaRanch!

Well, I agree - you should be using a focus event assigned to all JTextFields to capture the focusGained event. You would have some location to then assign the 'current' text field to. Your button would then setText on the 'current' text field (and since pressing a button changes the focus, probably re-focus on the current text field to make navigation easier).

So it seems you have the concept correct, what problems are you having with the implementation?


Steve
Lee Quinlan
Greenhorn

Joined: Aug 25, 2009
Posts: 9
Hi Steve. Thanks for reply.
The thing is, I'm learning two new skills right now. Java and Netbeans !
I'm using Netbeans for my project and can see that the generated 'initComponents()' code is adding 'java.awt.event.ActionListener()' s to the componenets I'm hoping to populate in this way. Maybe I'm misunderstanding a basic java principle here but I kind of assumed that there would be some way to 'tap into' underlying code somewhere that knows a cursor is being put in a field and can tell my button/label 'mouseClicked' event method which one, so that i can send the 'N/A' text to it.
Is there some way to achieve my goal without having to add a 'focusGained event' method to each textfield control I may want to add 'N/A' to, that will then call my 'tell me what control called me and .addText("N/A") to it' method ?
Does that make sense ?
Lee Quinlan
Greenhorn

Joined: Aug 25, 2009
Posts: 9
Can I send 'N/A' text to the current cursor position ? This would save me adding focusGained events to all the fields.
My JTextFields are on a JPanel which in turn is on a JTabbedPane.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Lee Quinlan wrote:Hi Steve. Thanks for reply.
The thing is, I'm learning two new skills right now. Java and Netbeans !


Well, there is your mistake ;-)

I would strongly suggest you drop the IDE and learn java first. You will find it MUCH easier to learn Netbeans after you understand Java, and what the IDE does. But when you try to learn both at the same time you get confused about what jobs the language does and what jobs the IDE does. Then you start to go down the route you are - designing application around limited knowledge of how the IDE works. This is particularly true with GUI code, where the IDE generates a tone of impossible to understand (and poorly written in my opinion) code.

Anyway, here is a very quick example of hand-written code which does what you want. It uses 3 classes (They don't have to be separate classes, but I think it works better either like this or using inner classes. Since you are just learning I avoided inner classes).

1) The UI class holds a 'buildComponents()' method that makes the window, the button, and text fields, puts them together. It also holds a reference to the 'current' JTextComponent and methods for setting the current text field, changing its text, and making it in focus. You can easily add more methods to help, for example, get its text value.
2) The ButtonAction class is a subclass of AbstractAction. It provides both the name and functionality to the JButton used to fill in the 'N/A' text in UserInterface's current JTextComponent. It requires a reference to the UserInterface so it can call the set...Text and focus methods.
3) The FieldListener class is a FocusListener I assign to the JTextFields. When focusGained is called, it assigns the component that triggered the focus event to the UserInterface's current JTextComponent.

Here is the code:






Finally a fourth class just to run the application


If you look at it line for line, it isn't that tough to read or do. Maybe there are things you haven't seen before, and maybe you would do things differently when beginning. But it is readable, I think (if not let me know and I will explain).

I'm using Netbeans for my project and can see that the generated 'initComponents()' code is adding 'java.awt.event.ActionListener()' s to the componenets I'm hoping to populate in this way. Maybe I'm misunderstanding a basic java principle here but I kind of assumed that there would be some way to 'tap into' underlying code somewhere that knows a cursor is being put in a field and can tell my button/label 'mouseClicked' event method which one, so that i can send the 'N/A' text to it.
Is there some way to achieve my goal without having to add a 'focusGained event' method to each textfield control I may want to add 'N/A' to, that will then call my 'tell me what control called me and .addText("N/A") to it' method ?
Does that make sense ?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Lee Quinlan wrote:Can I send 'N/A' text to the current cursor position ? This would save me adding focusGained events to all the fields.
My JTextFields are on a JPanel which in turn is on a JTabbedPane.


Nope. The 'current cursor' is relative to the component, so you have to get the current text field to get its cursor position. You can get the 'currently focused component' but when you press a button the button becomes the currently focused component, so that doesn't help.
Lee Quinlan
Greenhorn

Joined: Aug 25, 2009
Posts: 9
lol. I knew learning both at the same time going to be a challenge and agree that the Netbeans code was never going to be very efficient. The WYSIWYG side of things drew me in.
This feels like the right time to pick out the GUI code, clean things up and switch to a basic java editor going forward.
Yes. There are a few new concepts in your code but thats a good thing. Its very much appreciated and far beyond my expectations. Thank you.
I'll digest over the next couple of days and get back to you if I get stuck.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populate the 'current' JTextField via a button/label
 
Similar Threads
Netbeans 6.1 webuijsf:Addremove problems.
To get the focus around the text in a label like in a button
passing value between frames
Populate the 'current' JTextField via a button/label
Focusing Radio Button