Java applet JTextField inaccesible after JDialog o

2019-07-13 19:12发布

问题:

I have 2 problems with java on Ubuntu:

  1. JTextField gets inaccessible and you cant type anything in it. To reproduce you have to click on label ('Click this label') it will open my extended JDialog. After closing it with Cancel button JTextField gets inaccessible. The problem is that it doesen't happen all the time. I think around 1 on 10 tries. When it happens you have to click somewhere else on browser window or open dialog one more time.

  2. Second problem is that when ubuntu opens JDialog it creates other process which is shown on left side app bar. You can than click somewhere on the applet under the dialog and this dialog will go under the browser even thought it is modal and should be on top.

Did anyone get similar errors with ubuntu and know how to fix it. On windows everything works fine. We use ubuntu-12.04-desktop and java 1.6.0_34-b04. It was tested in firefox 11.0 and Google chrome (newest I think)

Here is my code TestApplet.java class:

    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    import javax.swing.JApplet;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;

    import client.utilities.GUIUtilities;


    @SuppressWarnings("serial")
    public class TestApplet extends JApplet {
        public void init() {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        JApplet applet = TestApplet.this;

                        applet.setLayout(new FlowLayout());

                        JTextField ts = new JTextField("Test text");
                        ts.setColumns(10);
                        applet.add(ts);

                        applet.add(getCallCalendarButton(ts));
                    }
                });
            } catch (Exception e) {
                System.err.println(e.getCause());
            }
        }

        private JLabel callCalendarButton;

        private MyDialog aDialog;

        protected JLabel getCallCalendarButton(final JComponent cmp) {
            if (callCalendarButton == null) {
                callCalendarButton = new JLabel("Click this label!!");

                callCalendarButton.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        if (callCalendarButton.isEnabled()) {
                            Frame parentFrame = null;
                            if (parentFrame == null)
                                parentFrame = GUIUtilities.getParentFrame(cmp);

                            System.out.println(parentFrame);

                            aDialog = new MyDialog(parentFrame, cmp);
                            aDialog.setVisible(true);

                            System.out.println("qwewqe");
                            cmp.requestFocusInWindow();
                        }
                    }
                });
            }
            return callCalendarButton;
        }


    }

And here is extended JDialog class (MyDialog.java):

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;

    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;


    @SuppressWarnings("serial")
    public class MyDialog extends JDialog {

        private JButton okButton;

        private JButton cancelButton;

        private JComponent owner;

        private int WIDTH = 230;
        private int HEIGHT = 230;

        Frame parent;

        public MyDialog(Frame parent, JComponent owner) {
            super(parent);

            this.parent = parent;
            this.owner = owner;


            okButton = new JButton("OK");
            okButton.setMnemonic('O');
            okButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    MyDialog.this.setVisible(false);
                    MyDialog.this.dispose();
                }
            });

            cancelButton = new JButton("Cancel");
            cancelButton.setMnemonic('C');
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    MyDialog.this.setVisible(false);
                }
            });

            this.setLayout(new BorderLayout());
            JPanel bottomPanel = new JPanel();
            bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
            bottomPanel.add(okButton);
            bottomPanel.add(cancelButton);
            this.add(bottomPanel, BorderLayout.SOUTH);
            this.setModal(true);
            this.setBounds(100, 100, WIDTH, HEIGHT);
            //

            this.addComponentListener(new ComponentAdapter(){
                  @Override
                  public void componentHidden(ComponentEvent e){
                    SwingUtilities.invokeLater(new Runnable(){
                      @Override
                      public void run(){
                            MyDialog.this.owner.requestFocusInWindow();
                            //MyDialog.this.parent.toFront();
                            //MyDialog.this.parent.requestFocusInWindow();

                      }
                    });
                  }
                });
        }



    }

To use this html to run applet:

    <html>
    <body>
    <Applet Code="TestApplet.class" width="200" height="100" >
    </Applet>
    </body>
    </html>