2012-11-08 52 views
0

我正在整合一個小程序,我需要破解一個對話框並改變它的模態。改變現有的JDialog的模式

我的問題是我不知道Swing,而我的嘗試在實踐中沒有任何效果。

當前實現:

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

也試過

dialog.setModal(false); 

所以有我的問題。我如何動態地改變現有JDialog的模式?

+0

爲什麼? (15個字符) – kleopatra

+2

請注意,當原始開發人員將對話框設計爲模態時,更改這可能會影響應用程序的正確工作 – Robin

回答

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

我猜你沒有獲得你的小程序AWTPermission.toolkitModality權限得到的東西。您可以使用Toolkit.isModalExclusionTypeSupported(java.awt.Dialog.ModalExclusionType)來檢查此問題。

0

要更改對話框是模態還是非模態,請使用setModalityType方法。

當您更改模態時,該對話框應該不可見。否則,直到對話框被隱藏然後再次顯示它纔會生效。

此外,對話框本身必須進行編程以支持不同的模態模式。

  • 當您使用模態對話框時,與所有者窗口(以及可能與其他應用程序窗口)的交互被阻止。因此,您將顯示對話框dialog.setVisible(true),並且在對話框關閉之前此方法不會返回。然後你使用對話框中的數據。
    一個典型的模式對話框是打開文件:應用程序不能繼續,直到它知道要加載哪個文件。
  • 在無模式對話框的情況下,方法dialog.setVisible(true)立即返回(在屏幕上顯示對話框後)。按下對話框中的按鈕通常會對其他窗口和對話框產生一些影響。在顯示對話框時,您可以與應用程序的其他窗口進行交互。
    例如,典型的查找對話框會在主窗口中選擇一個搜索字符串。您可以返回到主窗口,然後更改文字,然後再次點擊查找等。

如果您需要更多的幫助,我可以告訴你有對話框,在兩種模式下工作的工作示例:模式和無模式。

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