2013-05-16 19 views
1

我有一個Java Swing應用程序和JInternalFrame的..模態JInternalFrame時將數據返回給呼叫方

在JInternalFrame的我有一些輸入字段,當我按一個熱鍵我想下面的情況發生:

  1. 當前JInternalFrame中的所有執行都被暫停,並且沒有任何輸入字段可以重新獲得焦點。
  2. 新框架(inputFrame)將以某種模式模式打開,並向用戶提供填充正確值的幫助。 (數據從EJB中獲取,並根據用戶的選擇進行過濾)
  3. 當用戶單擊確定時,inputFrame被關閉並且數據返回到mainFrame。
  4. mainFrame接受數據並繼續處理。

如果可能,我想inputFrame是一個JInternalFrame,但它不是一個主要問題。

有沒有人知道一種方法來實現這一目標?

+0

[的JDialog(HTTP:/ /docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#api)或自定義JOptionPane。 –

回答

0

我發現在JOptionPane的源代碼中的答案有我複製了兩個靜態方法createInternalFrame和startModal(只有一些小的修改),並創建下面我自己的類:

public class ModalInternalPanel { 

    public static Object showInternalDialog(Component parentComponent, Container container, HasReturnValue hasReturnValue, String title) { 
     JInternalFrame frame = createInternalFrame(parentComponent, title, container); 
     startModal(frame); 
     if (hasReturnValue!=null) return hasReturnValue.getReturnValue(); else return null; 
    } 


    public static JInternalFrame createInternalFrame(Component parentComponent, String title, Container container) throws RuntimeException { 
     // Try to find a JDesktopPane. 
     JLayeredPane toUse = JOptionPane.getDesktopPaneForComponent(parentComponent); 
     // If we don't have a JDesktopPane, we try to find a JLayeredPane. 
     if (toUse == null) toUse = JLayeredPane.getLayeredPaneAbove(parentComponent); 
     // If this still fails, we throw a RuntimeException. 
     if (toUse == null) throw new RuntimeException ("parentComponent does not have a valid parent"); 

     JInternalFrame frame = new JInternalFrame(title); 


     frame.setContentPane(container); 
     frame.setClosable(true); 

     toUse.add(frame); 
     frame.setLayer(JLayeredPane.MODAL_LAYER); 

     frame.pack(); 
     frame.setVisible(true); 

     return frame; 
    } 



    private static void startModal(JInternalFrame f) { 
     // We need to add an additional glasspane-like component directly 
     // below the frame, which intercepts all mouse events that are not 
     // directed at the frame itself. 
     JPanel modalInterceptor = new JPanel(); 
     modalInterceptor.setOpaque(false); 
     JLayeredPane lp = JLayeredPane.getLayeredPaneAbove(f); 
     lp.setLayer(modalInterceptor, JLayeredPane.MODAL_LAYER.intValue()); 
     modalInterceptor.setBounds(0, 0, lp.getWidth(), lp.getHeight()); 
     modalInterceptor.addMouseListener(new MouseAdapter(){}); 
     modalInterceptor.addMouseMotionListener(new MouseMotionAdapter(){}); 
     lp.add(modalInterceptor); 
     f.toFront(); 

     // We need to explicitly dispatch events when we are blocking the event 
     // dispatch thread. 
     EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); 
     try { 
      while (! f.isClosed())  { 
       if (EventQueue.isDispatchThread()) { 
        // The getNextEventMethod() issues wait() when no 
        // event is available, so we don't need do explicitly wait(). 
        AWTEvent ev = queue.getNextEvent(); 
        // This mimics EventQueue.dispatchEvent(). We can't use 
        // EventQueue.dispatchEvent() directly, because it is 
        // protected, unfortunately. 
        if (ev instanceof ActiveEvent) ((ActiveEvent) ev).dispatch(); 
        else if (ev.getSource() instanceof Component) ((Component) ev.getSource()).dispatchEvent(ev); 
        else if (ev.getSource() instanceof MenuComponent) ((MenuComponent) ev.getSource()).dispatchEvent(ev); 
        // Other events are ignored as per spec in 
        // EventQueue.dispatchEvent 
       } else { 
        // Give other threads a chance to become active. 
        Thread.yield(); 
       } 
      } 
     } 
     catch (InterruptedException ex) { 
      // If we get interrupted, then leave the modal state. 
     } 
     finally { 
      // Clean up the modal interceptor. 
      lp.remove(modalInterceptor); 

      // Remove the internal frame from its parent, so it is no longer 
      // lurking around and clogging memory. 
      Container parent = f.getParent(); 
      if (parent != null) parent.remove(f); 
     } 
    } 
} 
2

看看How to Use Internal Frames並在showInternal上搜索。這就是答案。

+0

我現在記得。 ;) –

+0

不完全是我需要的功能,但它指出了我正確的方向。謝謝! – Ulf