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 ?