java panel适应窗口,Java限制窗口/ 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();

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值