javaswingfocuslistener

java restrict switching of window/Jpanel


I have JFrame with 3 JPanel(basically three tabs). one of the panel has a textbox. there is value restriction on textbox. it means, user can enter only 1-1000 number in it. If he enters number >1000, it throws the warning message. Now I am using focuslistener to save the entered number as soon as it looses the focus. But if the user enters 1200 and click on another tab(panel), it gives me expected warning message but also goes to the another tab. I need to remain in same panel if there is warning box. I don't want to loose the focus from the current panel.

mMaxLabelLength = new JTextField();
mMaxLabelLength.addActionListener(this);

public void focusGained(FocusEvent fe)
{
    // do NOTHING
}

@Override
public void focusLost(FocusEvent fe)
{
    saveActions();
}

public void actionPerformed(ActionEvent e)
{
    //Do something
}

private void saveActions()
{
    // error message
    JOptionPane.showMessageDialog(this, 
        "Please enter an integer value between 1 and 1000.", 
        "Invalid Entry", JOptionPane.INFORMATION_MESSAGE);
    SwiftApplication APP = SwiftApplication.getInstance();
    int nMaxLabel = APP.getMaxPieLabel();
    mMaxLabelLength.setText(new Integer(nMaxLabel).toString());
    mMaxLabelLength.requestFocus();
}

Solution

  • The code block in the question does not offer too many details, but as far as I understand it, you need to use a VetoableChangeListener to prohibit focus change.

    Here an example from Java2s:

    import java.awt.Component;
    import java.awt.KeyboardFocusManager;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyVetoException;
    import java.beans.VetoableChangeListener;
    
    public class Main {
      public static void main(String[] argv) {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener(
            new FocusVetoableChangeListener());
      }
    }
    
    class FocusVetoableChangeListener implements VetoableChangeListener {
      public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
        Component oldComp = (Component) evt.getOldValue();
        Component newComp = (Component) evt.getNewValue();
    
        if ("focusOwner".equals(evt.getPropertyName())) {
          if (oldComp == null) {
            System.out.println(newComp.getName());
          } else {
            System.out.println(oldComp.getName());
          }
        } else if ("focusedWindow".equals(evt.getPropertyName())) {
          if (oldComp == null) {
            System.out.println(newComp.getName());
          } else {
            System.out.println(oldComp.getName());
          }
        }
    
        boolean vetoFocusChange = false;
        if (vetoFocusChange) {
          throw new PropertyVetoException("message", evt);
        }
      }
    }
    

    But, the more I think about it, maybe using InputVerifier and public boolean shouldYieldFocus(JComponent input) is more appropriate. See "Validating Input" in the "How to Use the Focus Subsystem" of the Java Tutorial.