Forum Stats

  • 3,769,482 Users
  • 2,252,969 Discussions
  • 7,875,047 Comments

Discussions

[Swing] How to expose processFocusEvent when JFormattedTextField is already

936354
936354 Member Posts: 63
edited Feb 23, 2013 5:58AM in Java Programming
I am using the Netbeans IDE, the JFormattedTextfield has been already created in the method called initComponents();
Therefore I cannot change it. I want to expose and override the method called processFocusEvent.
Any ideas I can do this after the control is created by the IDE wizard.
Thanks
Jack

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    Why not simply adding another </tt>FocusListener</tt> to the component?

    bye
    TPD
  • 936354
    936354 Member Posts: 63
    Hi,
    I want to override it since there is a bug, as I was told, that the JFormattedTextField, when it is set to COMMIT_OR_REVERT, it can't accept a blank value, it would return the previous value which it is not desired. But I can't set it to COMMIT because I'd like to revert the values if the user enters the wrong value.
    Could you please provide an example of FocusListener that does the same thing as this?
    final JFormattedTextField field = new JFormattedTextField(NumberFormat.getInstance()) {  
        @Override  
        protected void processFocusEvent(final FocusEvent e) {  
            if (e.isTemporary()) {  
                return;  
            }  
      
            if (e.getID() == FocusEvent.FOCUS_LOST) {  
                if (getText() == null || getText().isEmpty()) {  
                    setValue(null);  
                }  
            }  
            super.processFocusEvent(e);  
        }  
    }; 
    Thanks
    Jack
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Feb 22, 2013 4:34AM
    Better approach to this would be an <tt>InputVerifier</tt> or a <tt>DocumentListener</tt>.

    bye
    TPD
  • darrylburke
    darrylburke Member Posts: 18,007
    Cross posted
    http://www.coderanch.com/t/605326/GUI/java/expose-processFocusEvent-JFormattedTextField-opened

    db
This discussion has been closed.