I'm working with Java Application with JFormattedTextField. I have some problems that:
How to set JFormattedTextField with can accept "/" and "-"
I want to set jformattedtextfield value and text to null or "" after entering wrong format.
Example:
2.1.JFormattedTextField ftf .... with format ("yyyy-MM-dd")
2.2. Input right format: 1990-5-6
2.3. Leave ftf
2.4. Then focus ftf and input wrong format: 5-6-1990
2.5. Leave ftf then it recognize wrong format but return something else
But what I want is that I must be null or empty.
How can I do it?
Any suggestions would be appreciated!
This is my first answer on this forum and I hope I don't mess up. For the first question, you can use MaskFormatter. This will also ensure that the value is never entered wrong.
new JFormattedTextField(createFormatter("####-##-##"));
private static MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
formatter.setPlaceholderCharacter('0');
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
Related
I'm getting data from Microsoft Access and reading it into an object array and then displaying it on a list. I'm trying to set the date of a jDateChooser using the String from the object. The first time I try to set the date, nothing is set/selected. If I change the date on my GUI, save it to access and try to set the date again, then it works? Not sure why this is the case. Please help (:
String apUpdateDate = appointments[appointmentPos].getAppointmentDate();
try
{
Date updateDate = new SimpleDateFormat("dd/MM/yyyy").parse(apUpdateDate);
}
catch (ParseException ex)
{
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
With the following code I only want to allow positive numbers. For some reason i am not even able to parse the strings correctly:
DecimalFormat dfNoNegative = new DecimalFormat("#,##0.00");
dfNoNegative.setNegativePrefix("");
try {
System.out.println(dfNoNegative.parse("123.00"));
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println(e.getErrorOffset());
e.printStackTrace();
}
Error message and ErrorOffset:
Unparseable number: "123.00"
6
Can anyone guide me where I am mistaken? An example for a working String would be good as well
My mistake was to dfNoNegative.setNegativePrefix(""); to nothing (""). This doesn't work, because the String started directly with the number and 123 is not "", and therefore it fails. Basically this method overwrites what should be used as negative prefix (default is -). If you set it to ! as example, System.out.println(dfNoNegative.parse("!123.00")); would print -123.
I have the following exception trying to manipulate a value to be added;
thank you very much for your help
java.lang.NumberFormatException: Invalid double: "20,000"
java.lang.StringToReal.invalidReal(StringToReal.java:63)
java.lang.StringToReal.parseDouble(StringToReal.java:269)
java.lang.Double.parseDouble(Double.java:295)
java.lang.Double.valueOf(Double.java:332)
You can use a NumberFormat to parse your String1. Something like,
String str = "20,000";
NumberFormat nf = NumberFormat.getNumberInstance(new Locale("en_US"));
NumberFormat nfIT = NumberFormat.getNumberInstance(Locale.ITALIAN);
try {
System.out.println(nf.parse(str)); // <-- 20000
System.out.println(nfIT.parse(str)); // <-- 20
} catch (ParseException e) {
e.printStackTrace();
}
For more options see Customizing Formats (The Java Tutorials).
1Being sure to pass the appropriate Locale to match your expected output.
You don't use comma as a separator in numbers.
You have to use 20000 instead of 20,000.
EDIT:
as #MitchWeaver mentioned, you can also substitute comma to underescore, making it 20_000
I am using JDateChooser with JCalendar in application. User should choose the date in date chooser and date should be updated in JTextFieldDateEditor. However, I can't seem to get the PropertyChangeListener to work.
I have checked it on stackoverflow as well, found Is it possible to detect a date change on a JCalendar JDateChooser field? which seems to be the exact same problem as I am having. I copied the code through from there, but I still get 'cannot find symbol - class PropertyChangeListener' error. I imported whole awt.event and JCalendar libraries. I am absolutely clueless as to what is wrong with my code.
Here's what I have written so far
datum = new Date ();
klik = new JDateChooser (datum)
klik.getDateEditor().addPropertyChangeListener( new PropertyChangeListener()
{
#Override
public void propertyChange(PropertyChangeEvent e)
{
if ("date".equals(e.getPropertyName()))
{
System.out.println(e.getPropertyName()
+ ": " + (Date) e.getNewValue());
}
}
});
which is basically copy-paste from the link above, but it seems reasonable to me
Thanks for your help :)
I use java 1.7.25
but found this error. what should I do?
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Unknown pattern character 'u'
at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:264)
at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:319)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:365)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:249)
Here is my code
public static int getDayNumberOfWeek(int day, String monthString, int yyyy) {
//http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
int dayNumberOfWeek = 1;
final String inputFormat = "MMM/dd/yyyy";
final String outputFormat = "u";
String dayString2Digit = DateTimeHelper.getTwoDigit(day);
String inputTimeStamp = monthString + "/" + dayString2Digit + "/" + String.valueOf(yyyy);
try {
dayNumberOfWeek =Integer.valueOf(TimeStampConverter(inputFormat, inputTimeStamp,
outputFormat));
}
catch (ParseException e) {
e.printStackTrace();
}
return dayNumberOfWeek;
}
I use java 1.7.25
No, you don't - not if you're running on Android. You need to look at the Android documentation, not the Java 7 docs.
If you look at the Android SimpleDateFormat documentation you'll see that u isn't listed there. I don't believe there's a format pattern character for "day of week as a number" in Android.
Were you really looking for that though? If you just want the day of the week as a number (without anything else) you can always use
String text = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
If you're using android, then you're not using Java 1.7.25. See the android documentation: there's no support for u in SimpleDateFormat.
I'm guessing your problem is going to be in your TimeStampConverter class where you're passing in that "u" as the outputFormat. "u" is not a valid format character in SimpleDateFormat and you must be constructing a format string that contains it.
If you need to use the "u" as a literal, you'll need to enclose it in single quotes.