Hello,

I currently have 2 JFrames for my "game" (Not really a good one), one for the actual game and one for a minimap.

What I can't figure out is that when you click off the game (for example to a browser or something) the game and minimap loose focus, but when you click on the game again, the minimap may not show or is still not on top.

What I want is so that once the game is brought into focus (they are playing the game) the minimap is shown next to it, on top.

I hope that makes sense.

Any help is appreciated.

-- Turt2Live

Dani AI

Generated

Two independent JFrames will fight with the OS for focus and stacking. As hinted, treat the minimap as an owned window or as a component inside the main frame instead of a second top-level JFrame. Owned windows (JDialog/JWindow) are tied to their owner: when the owner is raised the owned window is raised with it, and when the owner moves the owned window can be kept next to it. That is a much more reliable pattern than trying to force focus with Robot or repeatedly calling setVisible(true).

A simple, reliable approach is to create the minimap as a JWindow owned by the game JFrame and keep it in sync with the game window. Make it non-focusable if you don’t want it to steal keyboard input:

// run on the EDT
JWindow minimap = new JWindow(gameFrame);
minimap.setFocusableWindowState(false); // avoid stealing focus
minimap.getContentPane().add(minimapPanel);
minimap.pack();
minimap.setLocation(gameFrame.getX() + gameFrame.getWidth(), gameFrame.getY());
minimap.setVisible(true);

gameFrame.addComponentListener(new ComponentAdapter() {
    public void componentMoved(ComponentEvent e) {
        minimap.setLocation(gameFrame.getX() + gameFrame.getWidth(), gameFrame.getY());
    }
    public void componentResized(ComponentEvent e) {
        minimap.setLocation(gameFrame.getX() + gameFrame.getWidth(), gameFrame.getY());
    }
});

Alternatives: put the minimap inside the main frame (JLayeredPane, GlassPane, or a lightweight overlay) so there is only one top-level window — this removes almost all cross-platform focus/stacking issues. JInternalFrame inside a JDesktopPane is another option if you want window-like behavior but inside a single frame.

Troubleshooting notes: use the EDT for all Swing calls, avoid setVisible(true) as a focus hack, and avoid setAlwaysOnTop(true) unless you want the minimap above all other apps (it can be intrusive). Also remember some platforms prevent programmatic “focus stealing” (toFront/requestFocus) for user-friendly reasons, so owned windows or in-frame overlays are the safest solution.

Recommended Answers

All 2 Replies

standard is that you using just one JFrame and other TopLayoutContainers would be JDialog(s)

1/ it hard to manage Focus between two JFrames, nor to move one of then toFront() - sure its possible but code needed for that will be longer than your "game"

2/ JDialog#Modality - but chaning with JFrame Focus

3/ anyway is possible (search for that) set JFrame as parent for JDialog

Thank you very much :) I was trying to use a combination of WindowListners, Robots, and KeyEvents to hide the minimap, show it, then show the game.

Although I did learn that setting a JFrame to visible while it is already visible gives it focus :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.