更改现有JDialog的模态

时间:2012-11-08 16:55:54

标签: java swing jdialog modality

我正在集成一个applet,我需要破解其中一个对话框并改变它的形态。

我的问题是我不知道Swing,我的尝试在实践中没有效果。

目前的实施:

dialog.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
dialog.repaint();

也尝试了

dialog.setModal(false);

所以有我的问题。如何动态更改现有JDialog的模态?

4 个答案:

答案 0 :(得分:1)

不知道你要做什么...... 但也许你可以从这里得到一些东西

   public class Mainz extends JFrame implements ActionListener{
                JButton showDialog = new JButton("show dialog");

                public Mainz() {
                    setLayout(new FlowLayout());
                    showDialog.addActionListener(this);
                    add(showDialog);
                    setSize(200, 300);
                    setVisible(true);   
                }
                @Override
                public void actionPerformed(ActionEvent e) {
                    new Dialogz(this, false);
                    setEnabled(false);
                }

                public static void main(String[]args){
                    new Mainz();
            }
            }
            class Dialogz extends JDialog{
                JButton close = new JButton("close");


                public Dialogz(JFrame owner,boolean modal) {
                    super(owner, modal);
                    setSize(100, 200);
                    add(close);
                    setLocationRelativeTo(owner);
                    setVisible(true);

                    close.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae){
                            closez();
                        }
                    });
                } 

                void closez(){
                    setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
                    System.out.println("modal exclusion befor = "+getModalExclusionType());
                    setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
                    System.out.println("modal exclusion after = "+getModalExclusionType());

                    System.out.println("modality before ="+getModalityType());
                    setModal(true);
                    System.out.println("modality  after ="+getModalityType());
                    getOwner().setEnabled(true);
                    Dialogz.this.dispose();
                }
            }

答案 1 :(得分:1)

黑客攻击:

您可以通过调用私有方法来更改现有对话框的模态:

java.awt.Dialog.hideAndDisposePreHandler();

要调用此私有方法 - 例如:

private void executeMethod(final Class<?> clazz, final String methodName, final Object instance)
{
    final Method method =
        Iterables.getOnlyElement(Iterables.filter(
            Arrays.asList(clazz.getDeclaredMethods()), new Predicate<Method>()
            {
                public boolean apply(final Method method)
                {
                    return method.getName().equals(methodName);
                }
            }));

    method.setAccessible(true);
    try
    {
        method.invoke(instance);
    }
    catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
    {
        throw Throwables.propagate(e);
    }
}

(此代码需要番石榴)

最后称之为:

final Dialog myDialog = ...;

executeMethod(Dialog.class, "hideAndDisposePreHandler", myDialog);

答案 2 :(得分:0)

我猜你没有获得applet的AWTPermission.toolkitModality权限。

另一个问题可能是您的平台不支持排除类型 - 您可以使用Toolkit.isModalExclusionTypeSupported(java.awt.Dialog.ModalExclusionType)进行检查。

答案 3 :(得分:0)

要更改对话框是模态还是无模式,请使用setModalityType方法。

更改模态时,对话框应该不可见。否则,只有在隐藏对话框然后再次显示对话框后它才会生效。

此外,必须对对话本身进行编程以支持不同的模态模式。

  • 使用模式对话框时,将阻止与所有者窗口(可能还有其他应用程序窗口)的交互。因此,您将显示对话框dialog.setVisible(true),并且在关闭对话框之前,此方法不会返回。然后使用对话框中的数据 典型的模式对话框是打开文件:在知道要加载哪个文件之前,应用程序无法继续。
  • 如果是无模式对话框,方法dialog.setVisible(true)会立即返回(在屏幕上显示对话框后)。按对话框中的按钮通常会对其他窗口和对话框产生一些影响。在显示对话框时,您可以与应用程序的其他窗口进行交互 例如,典型的“查找”对话框在主窗口中选择搜索字符串。您可以返回主窗口,更改文本,然后再次单击“查找”,依此类推。

如果您需要更多帮助,我可以向您展示一个带有对话框的工作样本,该对话框在两种模式下都有效:模态和无模式。

相关问题