Question

I have that piece of code:

public WaitingDiag(Window mainWindow, String string, JFrame stFrame) {
        dialog = new JDialog(stFrame);
        // dialog.setForeground(new Color(100, 100, 100));
        dialog.setUndecorated(true);
        dialog.setSize(WAITING_DIALOG_WIDTH, WAITING_DIALOG_HEIGHT);
        int x = mainWindow.getX() + (mainWindow.getWidth() - dialog.getWidth())
                / 2;
        int y = mainWindow.getY()
                + (mainWindow.getHeight() - dialog.getHeight()) / 2;
        progressBar.setForeground(new Color(0, 128, 0));
        progressBar.setString("Loading data...");
        Font font = new Font("Serif", Font.BOLD, 12);
        progressBar.setFont(font);
        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        dialog.getContentPane().add(progressBar);
        dialog.setLocation(x, y);
        dialog.setModal(true);
    }

My problem is that, when dialog appears, it blocks all the application and it is contained in a JFrame stFrame (which is in fact a Tab), I want only that that Tab (stFrame) would be blocked, how that?

UPDATE:

The code where I careated the tabs is:

maingui.getContentPane().add(mainTabs);   //maingui is a JFrame
mainTabs.addTab("Editor", editorPanel);
Statistics st;  //st extends JFrame
mainTabs.addTab("Statistics", null, st.getContentPane(), null);
Was it helpful?

Solution

Modality works only for windows, so you can't use it to block tabs unless you overhaul the mechanism (and it's a non-trivial one).

One option is to call setEnabled(false) on the tab and component in its view - the content pane of the frame in your case. The implementations depends on if the dialog pops when you do something in a tab, if it pops regardless of what tab you are in or some other way. You can call setEnabled in an override of setVisible of the dialog - which should give you the desired results (this is not where modality is called, so it might behave slightly differently in certain circumstances).

Another option is to add some mouse listener to manually block clicks on the tab and \ or its component-in-view. This is more messy but resembles more how modality acts.

As to the modality of the dialog, it depends if you want it to block its parent frame - the one whose content pane is displayed in the tabs. Note that there is no relation between the tab and the frame the way you present it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top