2013-05-07 2 views
1

다음 코드 유무 : JInternalFrame를 같이 모달

import java.awt.AWTEvent; 
import java.awt.ActiveEvent; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.MenuComponent; 
import java.awt.event.MouseEvent; 
import javax.swing.JInternalFrame; 
import javax.swing.SwingUtilities; 

public class modalInternalFrame extends JInternalFrame { 

// indica si aquest es modal o no. 
    boolean modal = false; 

    @Override 
    public void show() { 
     super.show(); 
     if (this.modal) { 
      startModal(); 
     } 
    } 

    @Override 
    public void setVisible(boolean value) { 
     super.setVisible(value); 
     if (modal) { 
      if (value) { 
       startModal(); 
      } else { 
       stopModal(); 
      } 
     } 
    } 

    private synchronized void startModal() { 

     try { 
      if (SwingUtilities.isEventDispatchThread()) { 
       EventQueue theQueue = 
         getToolkit().getSystemEventQueue(); 
       while (isVisible()) { 
        AWTEvent event = theQueue.getNextEvent(); 
        Object source = event.getSource(); 
        boolean dispatch = true; 

        if (event instanceof MouseEvent) { 
         MouseEvent e = (MouseEvent) event; 
         MouseEvent m = 
           SwingUtilities.convertMouseEvent((Component) e.getSource(), e, this); 
         if (!this.contains(m.getPoint()) && e.getID() != MouseEvent.MOUSE_DRAGGED) { 
          dispatch = false; 
         } 
        } 

        if (dispatch) { 
         if (event instanceof ActiveEvent) { 
          ((ActiveEvent) event).dispatch(); 
         } else if (source instanceof Component) { 
          ((Component) source).dispatchEvent(
            event); 
         } else if (source instanceof MenuComponent) { 
          ((MenuComponent) source).dispatchEvent(
            event); 
         } else { 
          System.err.println(
            "Unable to dispatch: " + event); 
         } 
        } 
       } 
      } else { 
       while (isVisible()) { 
        wait(); 
       } 
      } 
     } catch (InterruptedException ignored) { 
     } 

    } 

    private synchronized void stopModal() { 
     notifyAll(); 
    } 

    public void setModal(boolean modal) { 
     this.modal = modal; 
    } 

    public boolean isModal() { 
     return this.modal; 
    } 
} 

가 그럼 난 내 JInternalFrame를 그릴 넷빈즈 GUI를 사용하지만, 단지 대신 JInternalFrame의의 modalInternalFrame를 확장하는 클래스 선언의 코드를 변경 :

public class myDialog extends modalInternalFrame { 
.... 

myDialog d = new myDialog(); 
d.setModal(true); 
d.setBounds(160, 180, 550, 450); 
jDesktopPane1.add(d); 
d.setVisible(true); 
012 :

다음은 실제로 "바탕 화면"JFrame의이 (jDesktopPane1 포함) 내 최상위에서 표시하려면이 사용

내 문제는 : 내부 프레임에 JComboBox 또는 PopupMenu가있는 경우 PopupMenu의 일부가 내부 프레임의 경계에서 벗어난 경우 해당 부분이 마우스 이벤트를 처리하지 않습니다 (해당 부분을 스크롤 할 수 없음).

어떤 아이디어?

+0

d.setModal을; 부모가 정의 된 경우에만 작동합니다. JDialog API를 참조하십시오. 런타임에 변경해야하는 경우 대신 ModalityTypes를 사용하십시오. – mKorbel

+0

OK, 올바르게 작동하지만 내 문제가 있음을 알고 있습니다. 내부 프레임에 JComboBox 또는 PopupMenu가있는 경우, PopupMenu의 일부가 내부 프레임의 경계에서 벗어난 경우 해당 부분이 마우스 이벤트를 처리하지 않습니다 (해당 부분을 스크롤 할 수 없음). –

+1

의견이 게시되지 않아서 [SSCCE] (http://sscce.org/), 짧은, 실행 가능한, 편집 가능한 의견이 게시되지 않았습니다. – mKorbel

답변

0

어떻게 JOptionPane.showInternalMessageDialog(...) 사용에 대한 :

  • 나는 윈도우 7의 x64에 JDK 1.7.0_21을 실행하고 (사실)
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class ModalInternalFrameTest { 
    private final JDesktopPane desktop = new JDesktopPane(); 
    private final String[] items = new String[] { 
    "bananas", "pizza", "hot dogs", "ravioli" 
    }; 
    private final Action openAction = new AbstractAction("open") { 
    @Override public void actionPerformed(ActionEvent e) { 
     JComboBox<String> combo = new JComboBox<String>(items); 
     combo.setEditable(true); 
     JOptionPane.showInternalMessageDialog(desktop, combo); 
     System.out.println(combo.getSelectedItem()); 
    } 
    }; 
    public JComponent makeUI(JFrame frame) { 
    frame.setJMenuBar(createMenuBar()); 

    JButton button = new JButton(openAction); 
    button.setMnemonic(KeyEvent.VK_S); 
    JInternalFrame internal = new JInternalFrame("Button"); 
    internal.getContentPane().add(button); 
    internal.setBounds(20, 20, 100, 100); 
    desktop.add(internal); 
    internal.setVisible(true); 

    JButton b = new JButton(new AbstractAction("beep") { 
     @Override public void actionPerformed(ActionEvent e) { 
     Toolkit.getDefaultToolkit().beep(); 
     } 
    }); 
    b.setMnemonic(KeyEvent.VK_B); 

    JPanel p = new JPanel(new BorderLayout()); 
    p.add(b, BorderLayout.SOUTH); 
    p.add(desktop); 
    return p; 
    } 
    private JMenuBar createMenuBar() { 
    JMenuBar menuBar = new JMenuBar(); 
    JMenu menu = new JMenu("Frame"); 
    menu.setMnemonic(KeyEvent.VK_F); 
    menuBar.add(menu); 

    JMenuItem menuItem = new JMenuItem(openAction); 
    menuItem.setMnemonic(KeyEvent.VK_1); 
    menuItem.setAccelerator(
     KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); 
    menu.add(menuItem); 

    return menuBar; 
    } 
    public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override public void run() { 
     createAndShowGUI(); 
     } 
    }); 
    } 
    public static void createAndShowGUI() { 
    JFrame f = new JFrame(); 
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    f.getContentPane().add(new ModalInternalFrameTest().makeUI(f)); 
    f.setSize(640, 480); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    } 
} 
관련 문제