I am creating a Swing GUI, I am using a series of JTextField. I want to be able to add a custom property to the text field. This is going to be used to complete a calculation when focus is lost, for instance.
JTextField1.getCustomePram() * user input
I want a text field that will have a predefined value, it is for multiplying £20 * 5. So the default param will be 20 and the user will put the qty in, and on loss of focus the calculation will be done.
I am using Netbeans to build the GUI, I have tried extending the JTextField but to be honest I am not sure what to do, any advice or if I am going about this completely wrong please let me know.
You’re doing this completely wrong.
For this I might use either a disabled text field or label for the unit price (£20), and a
JSpinnerfor the quantity (5). Instead of doing the calculation on focus lost, add a change listener and show the user the new sub-total (and total if relevant) of the ordered item(s) in ‘real time’ – when the quantity is changed.A good place to look is the Visual Guide to Swing Components. Combine them in a layout for complex groups of components.