Solved

Why the JOptionPane show 2 time?

Posted on 2002-05-07
10
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

691 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