Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why the JOptionPane show 2 time?

Posted on 2002-05-07
10
Medium Priority
?
624 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
Technology Partners: 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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