Implements a semi-modal JInternalFrame

本文介绍了一种实现模态JInternalFrame的方法,通过覆盖JInternalFrame的行为,使其能够在Swing应用中作为模态对话框使用。该实现允许内部窗格在打开时阻止用户与其他非模态内部窗格交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考下面的 links,做了一个 semi-modal JInternalFrame:
[url=http://java.sun.com/developer/JDCTechTips/2001/tt1220.html]Creating Modal Internal Frames -- Approach 1 and Approach 2[/url]
[url=http://www.codecomments.com/archive250-2005-2-399679.html]Java GUI - Make JInternalFrame behave like modal dialog[/url]
[url=http://forum.java.sun.com/thread.jspa?threadID=271179&messageID=1039079]Java Technology Forums - Modal Internal Frames and JCombos[/url]

import java.beans.*;

import javax.swing.*;

public class JXFrame extends JFrame implements VetoableChangeListener {
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
if (evt.getPropertyName().equals(JXInternalFrame.IS_SELECTED_PROPERTY)) {
if(evt.getSource() instanceof JXInternalFrame) {
JXInternalFrame frame = (JXInternalFrame)evt.getSource();
frame.selectChild();
}
throw new PropertyVetoException("Selected", null);
}
}
}


import java.awt.*;
import java.awt.event.*;
import java.beans.*;

import javax.swing.*;
import javax.swing.event.*;

public class JXInternalFrame extends JInternalFrame {
private boolean modal = false;

private JXInternalFrame parent = null;

private JXInternalFrame child = null;

private static JXFrame desktopFrame = null;

private Component originalGlassPane;

private GalssPaneForModal modalGlassPane;

private Integer defaultCloseOperation = null;

public JXInternalFrame() {
super();
}

public JXInternalFrame(String title) {
super(title, true, true, true, true);
}

public JXInternalFrame(JXInternalFrame parent, boolean modal, String title) {
super(title, true, true, true, true);
this.parent = parent;
if (modal) {
startModal();
}
}

public JXInternalFrame(String title, boolean resizable) {
super(title, resizable);
}

public JXInternalFrame(String title, boolean resizable, boolean closable) {
super(title, resizable, closable);
}

public JXInternalFrame(String title, boolean resizable, boolean closable,
boolean maximizable) {
super(title, resizable, closable, maximizable);
}

public JXInternalFrame(String title, boolean resizable, boolean closable,
boolean maximizable, boolean iconifiable) {
super(title, resizable, closable, maximizable, iconifiable);
}

public boolean isModal() {
return modal;
}

public void startModal() {
if (!modal && parent != null && desktopFrame != null) {
parent.setChild(this);
originalGlassPane = parent.getGlassPane();
modalGlassPane = new GalssPaneForModal();
modalGlassPane.containerFrame = parent;
parent.setGlassPane(modalGlassPane);
parent.addVetoableChangeListener(desktopFrame);
defaultCloseOperation = parent.getDefaultCloseOperation();
parent.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
modal = true;
}
}

public void stopModal() {
if (modal && parent != null && desktopFrame != null) {
parent.setGlassPane(originalGlassPane);
parent.removeVetoableChangeListener(desktopFrame);
if (defaultCloseOperation != null) {
parent.setDefaultCloseOperation(defaultCloseOperation);
}
try {
parent.setSelected(true);
if(parent.isIcon)
parent.setIcon(false);
} catch (PropertyVetoException e) {
// TODO: handle exception
}
modal = false;
}
}

public void setVisible(boolean visible) {
super.setVisible(visible);
if (!visible) {
stopModal();
}
}

public static void setDesktopFrame(JXFrame desktopFrame) {
JXInternalFrame.desktopFrame = desktopFrame;
}

public JXInternalFrame getChild() {
return child;
}

public void setChild(JXInternalFrame child) {
this.child = child;
}

public void selectChild() {
JXInternalFrame child = this;
while(child.getChild() != null)
child = child.getChild();
try {
child.setSelected(true);
} catch (PropertyVetoException exception) {
// TODO: handle exception
}
}
}

class GalssPaneForModal extends JComponent {
JXInternalFrame containerFrame = null;

public void setVisible(boolean visible) {
super.setVisible(true);
}

public GalssPaneForModal() {

this.setOpaque(false);
MouseInputAdapter listener = new MouseInputAdapter() {
@Override
public void mousePressed(MouseEvent e) {
containerFrame.selectChild();
}
};
addMouseListener(listener);
addMouseMotionListener(listener);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值