?
Solved

Why the JOptionPane show 2 time?

Posted on 2002-05-07
10
Medium Priority
?
633 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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