Buttons in JDialog are not clickable when on top of other two modal popups

I have a swing application. I have a window that needs to be displayed when multiple text boxes are clicked. (It looks like a virtual keyboard with multiple keys.) This window is created with

Window wdw= new JDialog();

      

It also sets the modality. ((JDialog) wdw).setModal(true);

A mouse listener has been added to keys. When pressing and holding keys, it shows another JDialog with extra buttons displayed.

Below is the method for creating this window, which is invoked by pressing and holding a key.

private void createPopupKeys(MouseEvent e) {
    JButton keyBtn = (JButton)e.getSource();
    Window parentWindow = SwingUtilities.windowForComponent(keyBtn);
    Window dialog = new JDialog(parentWindow);
    dialog.setFocusable(true);
    dialog.setFocusableWindowState(true);

    JPanel contP = (JPanel)((JDialog) dialog).getContentPane();
    contP.setPreferredSize(new Dimension(150, 40));
    JPanel pnl = new JPanel();

    String[][] popupKeys = {{"123","123","123"},
                            {"124","124","196"},
                            {"125","125","197"},
                            {"126","126","198"},
                            };
    for (int i = 0; i < popupKeys.length; i++) {
        JButton btn = new JButton(popupKeys[i][0]);
        btn.setActionCommand(popupKeys[i][2]);
        btn.setName("popupkey"+i);
        btn.addMouseListener(this);
        btn.setMinimumSize(new Dimension(30, 30));
        btn.setPreferredSize(new Dimension(30, 30));
        btn.setFocusable(false);
        btn.setFont(new Font("Dialog", Font.BOLD, 14));
        pnl.add(btn);
    }

    pnl.setPreferredSize(new Dimension(150, 40));
    contP.add(pnl);
            dialog.setAlwaysOnTop(true);
    Point loc = keyBtn.getLocationOnScreen();
    dialog.setLocation(loc.x - 20 , loc.y - 50);
    dialog.pack();

    dialog.setVisible(true);

    dialog.addWindowFocusListener(windowListener);
    dialog.addWindowListener(windowListener);
}

WindowAdapter windowListener = new WindowAdapter() {
    @Override
    public void windowLostFocus(WindowEvent e) {
        JDialog dialog = (JDialog) e.getSource();
        dialog.setVisible(false);
        dialog.dispose();
    }
};

      

In a normal window, when I click on the textbox, the modal opens, and when I press and hold the key, a new popup dialog opens and everyone is clickable.

But when I click a textbox in a modal popup, the window opens and presses and holds a key, popup keys are shown as well. But no buttons on the popup keys are available.

What could be the reason? And what's the solution?

+3


source to share





All Articles