From JTextField to JFormattedTextField - java

In NetBeans I've created a project with many of JTextField's. Now I understand that I should validate it. Is it possible to "cast" text fields to JFormattedTextField's?

It is possible to "cast" text fields to JFormattedTextField's?
No it's not. At least not directly. The other way around is possible though, given JFormattedTextField extends from JTextField.
So I must delete all JTexFields and add Formatted fields ?
Not necessarily but it's highly recommended. On the other hand you can let the class' variables declared as JTextField and initialize them as JFormattedTextField through custom code:
Then you can "safely" downcast them as JFormattedTextField later. However I'd like to emphasize one more time that it sounds like a dirty workaround. Also note you won't be able to change the setText(...) code to setValue(...) which is preferred for formatted text fields.

Related

JFormattedTextField and JComboBox

How can I use JFormattedTextField with an editable JComboBox?
I know it is using internally an JTextField, but I only want it to accept numeric values with one decimal.
For this reason I want to use JFormattedTextField, but I´m not able to find the function in JComboBox to establish it.
Maybe this will point you in the right direction:
https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification

Improving performances of JTextField

Title itself sounds strange but i wanted to see if there's actual way of improving performances of "setText" method of regular Java JTextField.
I have app that's used for calculating some scientific data. Results of the calculation are being presented in Swing window that contains tabs with total of 1815 different JTextField controls. I do need JTextField because results should be editable.
I noticed that a lot of "processing results" time goes to internal workings of "setText" method. Looking at JTextComponent source code i assume it's because of underlying "AbstractDocument".
As i would like to improve speed of presenting calculated results to my user, is there a way to make JTextField perform faster (most notably it's "setText" method)?
Those fields are grouped into separate frames in order to be more visual appealing and distinguishable.
Seriously. A user will not be able to view 1815 components at a single time an observe there behaviour especially if the value of all of them are changing at once.
Maybe a single table can't be used for all the text fields, but I'm sure you can structure some of the data into tables.
I do need JTextField because results should be editable.
I suspect some processing is related to the text field generating events whenever the text is changed. For example DocumentEvents and UndoableEditEvents.
So, maybe use a JLabel to display the data and then to edit you double click and use a popup JTextField to enter the data into the label.
creating a custom JTextField that's not using AbstractDocument but plain String?
Instead of using a PlainDocument as the implemtation of the AbstractDocument you could create your own simpler Document implementation without the full overhead of the PlainDocument.

Is there a good way to add a Document Listener to a Dynamically created JTextField

I am reading in information from an xml file where i have no idea how much information is there until i read it in. I dynamically create Jtextfields and can get the information to appear on them. I am trying to add a documentlistener to those text fields so that a user can change the data and it will automatically update the xml file every time a number is changed. The problem is that there is no reference to the JTextField to get the text from and it is causing a compiler error. Is there a way to do this?
Use a factory style setup.
Basically, create a method or class that will create a JTextField. Pass it the reference to the node that it will be responsible for.
Create the text field and register the DocumentListener to it...although, I'd be wary about using a DocumentListener personally, maybe a ActionListener and/or FocusListener might be more suitable, but hay.

Java SE + NetBeans - How to assign textfields to an array in UI Generator?

I've generated a UI in NetBeans that has 16 text fields positioned 4x4. To be able to iterate through them dynamically, I wanted to have those text fields in an array so their names would be something like field[0] - field[15], but i have not found a way to do so. The editor allows me only to assign a specific variable for each field, and it doesn't accept "[]" and returns an error message "The name of a component must be a valid Java Variable name.".
I would prefer not to break the UI generator display by externally modifying the code, but if it's the only way to do this, then to hell with it. :)
Thanks in advance!
There's no support for that sort of variable organization.
I have always approached it by adding something like this after the initComponents() call:
fields = new JTextField[]{ textField1, textField2, ... };

Get input values from JComboBox

How can I get the input for an editable JComboBox. When user gives an input to the combo how I can get the input text from it?
You need to get the edited text from the combobox editor via combo.getEditor().getItem().
If you need the text that is selected on a JComboBox and you are sure it's a String and not any other object, just use something like String text = (String)myCombobox.getSelectedItem().
If the thing you have in your Model is other than a String, then you need to cast it to the appropriate class, and then use the toString() method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox...
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

Categories