Java限制窗口/Jpanel的切换 [英] java restrict switching of window/Jpanel

查看:117
本文介绍了Java限制窗口/Jpanel的切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个JPanel(基本上是三个标签)的JFrame.面板之一具有文本框.文本框上有值限制.这意味着,用户只能输入1-1000号码.如果他输入的数字> 1000,则会引发警告消息. 现在,我正在使用focuslistener来保存输入的数字,以免失去焦点.但是,如果用户输入1200并单击另一个选项卡(面板),它会给我预期的警告消息,但还会转到另一个选项卡.如果有警告框,我需要保留在同一面板中.我不想从当前面板上移开焦点.

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();
}

推荐答案

问题中的代码块未提供太多详细信息,但据我所知,您需要使用VetoableChangeListener来禁止更改焦点

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.

这里是 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);
    }
  }
}

但是,我考虑得更多,也许使用InputVerifier如何使用Focus子系统"中的验证输入" "Java教程中的a>.

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.

这篇关于Java限制窗口/Jpanel的切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆