Solved

Why the JOptionPane show 2 time?

Posted on 2002-05-07
10
603 Views
Last Modified: 2012-06-21
Hai, now i try to develope the TextFieldcomponent,
Here is my code;

My question is why after the textfield has been lostFocus,
should show the JOptionMessage, but why it show two time?
beside that , still got a lot problem need to improve....

got any example? This component is one textfield that accept the date input and validate it.


public class DateTextField extends JTextField{    
  DateTextField(int cols){
    super(cols);
    addFocusListener(new FocusAdapter(){
      public void focusLost(FocusEvent e){
        String fieldText = getText();
     SimpleDateFormat simpleDateFormat = new
                         SimpleDateFormat("dd/MM/yyyy");
        Date date = null;
     if(fieldText.equals(""))
       return;
     try{
       date = simpleDateFormat.parse(fieldText);
       fieldText = simpleDateFormat.format(date);
     } catch (ParseException pe){
       pe.printStackTrace();
       fieldText = "";
       showMessage();
       requestFocus();
     }
     setText(fieldText);
     }
     });
  }
  private void showMessage(){
    JOptionPane.showMessageDialog(get(),"Input
        Date", "Invalid Filed",JOptionPane.ERROR_MESSAGE);
  }

}
0
Comment
Question by:HSL
  • 6
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6993267
I'd sya that its appearing twice cause your field is recieving two focus lost events.
Looks like it'd get one when the text field loses focus, at which point you request a popup dialog and return focus to text field. Then another after that dialog pops up and takes focus.
0
 

Author Comment

by:HSL
ID: 6993302
Ya, i also think like this, then how i solve this problem?
Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 6993319
Try removing the requestFocus() call.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:HSL
ID: 6993323
Ya, i also think like this, then how i solve this problem?
Thanks
0
 

Author Comment

by:HSL
ID: 6993333
Hmm....This one i try before , but also cannot,
still got any solution?

is it i need to restructure ? Thanks...
0
 

Author Comment

by:HSL
ID: 6993339
Hmm....This one i try before , but also cannot,
still got any solution?

is it i need to restructure ? Thanks...
0
 
LVL 92

Expert Comment

by:objects
ID: 6993349
WHy can't you remove the requestFocus method??
0
 

Author Comment

by:HSL
ID: 6993370
Sorry.... what i mean is i remove it already...
but the result still same..
The pane still prompt two time.. =)
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6993384
Maybe cause you're it's still in the process of handling the first focusLost, when you pop up the dialog then another one gets queued.
You need to rethink your aproach I think, or track when the dialog is showing. You're current approach will also pop up new dialog every time you lose focus and the data is incorrect so you could end up with LOTS of dialogs.
0
 

Author Comment

by:HSL
ID: 7599123
hi, actually this not the actual answer, but now i solve it already , i just want to close this question !
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 43
Spring Framework HTTPSession management 1 33
xampp tool 12 48
eclipse buid path vs tomcat lib path 10 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question