Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向调用者返回数据的模态JInternalFrame_Java_Swing_Joptionpane_Jinternalframe - Fatal编程技术网

Java 向调用者返回数据的模态JInternalFrame

Java 向调用者返回数据的模态JInternalFrame,java,swing,joptionpane,jinternalframe,Java,Swing,Joptionpane,Jinternalframe,我有一个Java Swing应用程序和一个JInternalFrame 在JInternalFrame中,我有一些输入字段,当我按下热键时,我希望发生以下情况: 当前JInternalFrame中的所有执行都被挂起,并且任何输入字段都不能接收焦点 一个新的框架(inputFrame)将以某种模式打开,并为用户填写正确的值提供帮助。(数据从EJB获取,并根据用户选择进行过滤) 当用户单击OK时,inputFrame关闭,数据返回到大型机 大型机接受数据并继续处理 如果可能,我希望inputFram

我有一个Java Swing应用程序和一个JInternalFrame

在JInternalFrame中,我有一些输入字段,当我按下热键时,我希望发生以下情况:

  • 当前JInternalFrame中的所有执行都被挂起,并且任何输入字段都不能接收焦点
  • 一个新的框架(inputFrame)将以某种模式打开,并为用户填写正确的值提供帮助。(数据从EJB获取,并根据用户选择进行过滤)
  • 当用户单击OK时,inputFrame关闭,数据返回到大型机
  • 大型机接受数据并继续处理
  • 如果可能,我希望inputFrame成为JInternalFrame,但这不是主要问题


    有人知道实现这一点的方法吗?

    请查看并在
    showInternal
    上搜索。答案就在这里。

    我在JOptionPane的源代码中找到了答案,在那里我复制了两个静态方法createInternalFrame和StartModel(只做了一些小改动),并在下面创建了我自己的类:

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

    或者定制的JOptionPane。这不完全是我需要的功能,但它为我指明了正确的方向。谢谢