When I instantiate a textField, I have a number in it that I want to be the default text. The problem is, I can't seem to be able to place that value into the textfield without getting an error. The strange thing about it is that the same TextField is what I use to set the value of the variable containing the number.
TextField myTF = new TextField("Number", value, 10, TextField.NUMERIC);
When I run this code, I receive an exception stating that value doesn't match the constraints of TextField.NUMERIC. However, when I check the vale of the value of the variable, I get the following output:
value = 1234567890
value.length() = 10
The value is set by the same TextField, saved to the phone that I am working on, and when loaded from the phone's settings, throws an exception.
This is definitely a JVM bug. If a TextField returned a string, it must be able to accept it. The only thing I can advice is to play a bit with the size of the field or the constraints. You haven't specified the device you are using, there could be some new firmwares for it with bugfixes.
a potential workaround to your problem could be to instantiate the field with a null value and then set the text afterwards.
TextField myTF = new TextField("Number", null, 10, TextField.NUMERIC);
myTF.setString(value);
I have the same problem. The cellphone is trying to store the field value as an int, and the maximum int value is (2^31) - 1 = 2,147,483,647, which is one digit short of what you (and me) need. Workaround, make your field of type text and set a charset of IS_LATIN_DIGITS. Cheers.
My idea is you try it
String str=""+value;
TextField myTF = new TextField("Number",str,10,TextField.NUMERIC);
Related
I'm a beginner who's been desperately trying to understand how to achieve the following thing: I have 3 TextView displaying 3 numeric values (always integers) on screen as text, such as: 50, 100, 200. Then, after some event, I want these 3 strings to be put as values in my PieEntries
in the ArrayList. I can't just get the text from the TextView and put it as a value for the entry, so I tried to use Integer.parseInt to read the Strings as an Ints (since they are actually made of all numbers), or also Integer.valueOf but I only get java.lang.NumberFormatException: For input string: "First" error after the crash. This is the part I'm talking about:
ArrayList<PieEntry> myData(){
ArrayList<PieEntry> myArray = new ArrayList<PieEntry>();
arrayValue.add(new PieEntry(Integer.parseInt(TextView1.getText().toString()), "First"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView2.getText().toString()), "Second"));
arrayValue.add(new PieEntry(Integer.parseInt(TextView3.getText().toString()), "Third"));
return graphValues;
}
I won't paste the whole code here since the graph actually works fine if I manually put values instead of trying to parse the integers from the TextViews.
Also: when I first start to type "new PieEntry", the hint clearly says that it's gonna require a "float value" (then a drawable or a string and so on). Is this where the troubles begin? Should I convert Strings into Integers (or into Floats?) in some way before? I guess there's just like some basic Java rule I'm ignoring, so I'd like to understand what is happening here and how to face this. Many thanks!
As you Explained, you are not getting the TextView text, it is because maybe you have written it somewhere you should not, i.e. provide a Button, when the user types and is done he will hit the button say submit and do this above code on button click.
Solved: I used SharedPreferences to store the new typed data as Integers and retrieve them whenever I need them:
Setting up my Preferences:
SharedPreferences myPreferences;
savedData = getSharedPreferences("myStoredData", Context.MODE_PRIVATE);
Saving an Integer for later:
SharedPreferences.Editor myEditor = savedData.edit();
myEditor.putInt("Label", int);
myEditor.apply();
myEditor.commit();
Retrieving and using the Integer:
savedData.getInt("Label", Default);
How do I put a comma on a textfield if what is used to initialize the textfield is a double?
So, the declared double variable is equal to, lets say, 10,000.
I use it to put a "10,000" on a textfield. So, this is what I want to happen...
This kind of result:
However, what I get is something like this:
How do I make the output in the textfield into "24,508.0" (with a comma) instead of just "24508.0"? I was thinking, maybe, I could turn it into an array. But how?
You have to use a formatted text field. Check out this link for more information. I would also like to note that a Boolean is either true or false. Not 10,000.
double d = 10000000.5
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(d));
Not sure how to write this in JAVA...
I need to code that will insert a default value (say 50) if user keys in a value outside of a given range of 10-100 feet. I have it working for errors if blank or non integer is entered or value is outside the range but cannot figure out how to integrate a default.
What I have that works is
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
poolLength = new JTextField(10);
panel1.add(new JLabel("Enter the pool's length (ft):"));
panel1.add(poolLength);
What I want to add is something like this
If poolLength <10 or >200 then poolLength = 100
Else this.add(panel1);
The simplest way is to get the text during e.g. an ActionEvent, parse it and reset the text if it's outside the range.
jTextField.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean isValid = true;
try {
int intVal = Integer.parseInt(jTextField.getText());
if (intVal < 10 || intVal > 200) {
isValid = false;
}
} catch (NumberFormatException x) {
// user entered something which is not an int
isValid = false;
}
if (!isValid) {
jTextField.setText("100");
}
}
});
Also see How to Use Text Fields.
Another way would be to use a spinner which does something like this by default.
disclaimer: I do not know Swing well, actually, I dont remember much at all..
Maybe you simply need to attach a validator to your JTextField?
Here you have an example, however please notice that in that post they check for the length of the text, not the contents. But having the text, you can easily parse it as a number and check if it is greater/lower than the bounds.
As you see in that post, by returning true/false you will prevent the user from entering a wrong value - ie. too small. Now, when wrong value is detected, then, instead of returning 'false', maybe it is possible to simply myjtextfield.setText("50.0") on that input? That may do exactly what you wanted.
really, be careful with those "maybe"s: I do not know Swing, but from general UI framework design, trying to "setText" may:
throw an exception: many frameworks think it's quite evil to change the field's value during validation and defend themselves against it
cause strange issues when editing: if that JTextEdit calls validation upon every single change of the text, you will notice odd things when trying to, i.e. select-all, delete, write 123. You'll end up with 50.0123 or 12350.0 as the JTextEdit might try to validate the intermediate empty text after deletion and coerce it to "50.0"..
or, it may just work.
So, if your time is critical, just try it. Maybe I guessed well. But, if you have some time to spare, wait until someone checks that I didn't write any nonsense.
Use a JSpinner instead.
It will not behave exactly as described, but provide a better user experience. The user can adjust the value with the ↑/↓ buttons. If they type a value outside the specified range, it will revert to the last valid value when the field loses focus.
I am using a JFormattedTextField, when i try to delete the value in the field, it automatically restores the previous value.
What is the reason for this behaviour and how can i stop it restoring the value?
This is the code for JFormattedtextField.
jFormattedTextField2 = new javax.swing.JFormattedTextField(new DecimalFormat("###,###.##"));
It happens because JFormattedTextField allows configuring what action should be taken when focus is lost using the setFocusLostBehavior method.
These are the actions.
Description
JFormattedTextField.REVERT - Revert the display to match that of getValue, possibly losing the current edit.
JFormattedTextField.COMMIT - Commits the current value. If the value being edited isn't considered a legal value by the AbstractFormatter
that is, a ParseException is thrown, then the value will not change,
and then edited value will persist.
JFormattedTextField.COMMIT_OR_REVERT - Similar to COMMIT, but if the value isn't legal, behave like REVERT.
JFormattedTextField.PERSIST - Do nothing, don't obtain a new AbstractFormatter, and don't update the value.
**** The default is JFormattedTextField.COMMIT_OR_REVERT so when you enter an invalid value, it's reverted and you get the previous
consistent state**
...
float value = Float.parseFloat((String)model.getValueAt(e.getLastRow(), 1));
DecimalFormat dec = new DecimalFormat("#.###");
model.setValueAt(dec.format(value), e.getLastRow(), 1);
...
at the third line i'm getting the stackOverflowError exception. What I'm intending to do is getting a JTable cell value from an Object, converting it to a float, limiting it to 3 decimal places, and finally convert to String and set the value with 3 decimal places at the cell.
I guess the problem is I'm changing the value, and entering the function again and again. So the StackOverflow is due to that. Question is, how can i fix this?
Complete function at: Java: Converting data types
(Sorry for posting twice... It was a different question, and the solution drove me to a different problem)
The problem is that setValueAt() will, as part of its implementation call tableChanged() on all registered listeners, including this one.
In order to avoid this, simply check whether the value in the cell is already in the desired format as the first thing in your method, and don't do anything if it is.
Just don't call model.setValueAt() if value of the cell is not changed.
It should stop the recursion.
I think this task is usually accomplished by setting a custom editor to the table. So that it formats all input data to a desired form. See this answer.
Perhaps you need something like
String text = (String) model.getValueAt(e.getLastRow(), 1);
String text2 = new DecimalFormat("#.###").format(Float.parseFloat(text));
if (!text.equals(text2))
model.setValueAt(dec.format(value), e.getLastRow(), 1);