Null
Null

Reputation: 1

Extension of JDialog (hidden?) not showing up in front of parent JFrame?

I have an application I'm making for a game to automatically update a game client.

Once you press Launch, it will open up my DownloadFrame (extends JDialog), and will look like this:

screenshot1

If you click the icon for the application in the taskbar, (maybe Windows 8 is the problem?) it will minimize the application like usual. However when you go to maximise the application again, the JDialog will be hidden, I'm assuming, behind the parent. It looks like this:

screenshot2

Here's my code for my extension of JDialog. Apologies in advance for it being messy.

public class DownloadFrame extends JDialog implements Runnable {

private static final long serialVersionUID = -8764984599528942303L;

private Background frame;
private ImageIcon[] gifs;

private JLabel spinner;

public DownloadFrame() {
    super(Loader.application, false);
    setLayout(null);
    setUndecorated(true);
    setAutoRequestFocus(true);
    new Thread(this).start();
    generateBackground();
    generateButton();
    generateGif();
}

private void generateBackground() {
    frame = new Background("sub_background.png");
    setSize(frame.getWidth(), frame.getHeight());
    setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
    setLocationRelativeTo(null);
    setLocation(this.getX(), this.getY() + 5);
    setLayout(null);
    setContentPane(frame);
}

private void generateGif() {
    gifs = Utils.generateGifImages();
    spinner = new JLabel(gifs[0]);
    spinner.setBounds(70, 30, gifs[0].getIconWidth(), gifs[0].getIconHeight());
    add(spinner);
}

private HoverableButton cancel;

public HoverableButton getCancelButton() {
    return cancel;
}

private void generateButton() {
    cancel = new HoverableButton(Settings.CANCEL_BUTTON, 75, 145);
    cancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            /*
             * TODO -
             * stop the download in progress
             */
            for (HoverableButton button : Loader.application.getPrimaryButtons()) {
                button.setActive(true);
                button.setVisible(true);
            }
            dispose();
        }
    });
    add(cancel);
}

private int cycleCount;

private void cycleGif() {
    if (spinner == null) {
        return;
    }
    cycleCount++;
    if (cycleCount > 7) {
        cycleCount = 0;
    }
    spinner.setIcon(gifs[cycleCount]);
}

@Override
public void run() {
    while (true) {
        cycleGif();
        try {
            Thread.sleep(100L);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }
}

In case it's needed, here's my usage of it. Most of the stuff can be ignored I'm sure, it's simply there to hide the four buttons while the download is in progress.

((HoverableButton) components[2]).addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            HoverableButton source = (HoverableButton) components[2];
            if (source.isActive()) {
                try {
                    Thread.sleep(500L);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if (panel == null) {
                    panel = new DownloadFrame();
                    panel.setVisible(true);
                } else {
                    panel.setVisible(true);
                    panel.getCancelButton().removeHighlight();
                }
                for (HoverableButton button : getPrimaryButtons()) {
                    button.setActive(false);
                    button.setVisible(false);
                    button.removeHighlight();
                }
                /*
                 * TODO -
                 * handle checking for updates / downloading updates
                 */
            }
        }
    });

Upvotes: 0

Views: 675

Answers (1)

camickr
camickr

Reputation: 324197

However when you go to maximise the application again, the JDialog will be hidden, I'm assuming, behind the parent

Yes. When you create the JDialog, you need to specify the "owner" JFrame of the dialog in the constructor.

So you must create and make the JFrame and make the frame visible before you create the dialog.

Upvotes: 3

Related Questions