plus-
plus-

Reputation: 46543

Changing the modality of an existing JDialog

I'm integrating an applet and I need to hack one of the dialog and change its modality.

My problem is I don't know Swing, and my attempts have no effect in practice.

Current implementation:

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

also tried

dialog.setModal(false);

So there is my question. How can I dynamically change the modality of an existing JDialog ?

Upvotes: 0

Views: 4477

Answers (4)

Alexey Ivanov
Alexey Ivanov

Reputation: 11838

To change whether your dialog is modal or modeless, use setModalityType method.

The dialog should be not visible when you change its modality. Otherwise, it will not take effect until the dialog is hidden and then shown again.

Moreover the dialog itself has to be programmed to support different modality modes.

  • When you use a modal dialog, interaction with the owner window (and possibly with other application windows) is blocked. So you show the dialog, dialog.setVisible(true) and this method does not return until the dialog is closed. Then you use the data from the dialog.
    A typical modal dialog is Open File: the application can't proceed until it knows which file to load.
  • In case of modeless dialog, the method dialog.setVisible(true) returns immediately (after showing the dialog on the screen). Pressing buttons in the dialog usually has some effect on other windows and dialogs. And you can interact with other windows of the application while the dialog is shown.
    For example, a typical Find dialog selects a search string in the main window. You can return to the main window, and change text, then click Find again and so on.

If you need more help, I can show you a working sample with dialog which works in both modes: modal and modeless.

Upvotes: 1

Illyr
Illyr

Reputation: 155

A hack for a hack:

you can change the modality of an existing dialog by calling the private method:

java.awt.Dialog.hideAndDisposePreHandler();

To call this private method - as an example:

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

(This code requires Guava)

And finally call it:

final Dialog myDialog = ...;

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

Upvotes: 1

Tom
Tom

Reputation: 830

I guess that you haven't acquired the AWTPermission.toolkitModality permission for your applet.

Another problem could be that the exclusion type isn't supported on your platform -- you can check this with Toolkit.isModalExclusionTypeSupported(java.awt.Dialog.ModalExclusionType).

Upvotes: 0

mopr mopr
mopr mopr

Reputation: 193

don't know what you try to do ... but maybe you can get something from here

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

Upvotes: 1

Related Questions