BlackEye
BlackEye

Reputation: 777

Swing - Swapping and hiding glass pane does not work - glass pane remains visible

I am trying to build a Swing application that shows a login panel as a glasspane if no user is loggeg in. If i try to hide to login glass pane it remains visible, but won't react to any user interactions.

Do you see any problems here?

public class HauptFrame implements SessionListener {

private static final long serialVersionUID = 7985854311368619704L;

public HauptFrame() {
    initialize();
}

public void initialize() {
    Session.get().addSessionListener(this);
    setSize(1024, 768);
    setVisible(true);
    startAndCheck();
}

public void startAndCheck() {
    if (!DatabaseManager.doesConfigExist()) {
        setNewGlassPane(new SetupGlassPanel(this));
    }
    else if (new UserDAO().getAllUser().size() == 0) {
        setNewGlassPane(new FirstUserGlassPane(this));
    }
    else if (Session.get().getUser() == null) {
        setNewGlassPane(new LoginGlassPanel());
    } else {
        setNewGlassPane(null);
    }
}

public void setNewGlassPane(JPanel glassPane) {
    if (glassPane != null) {
        getGlassPane().setVisible(false);
        setGlassPane(glassPane);
        getGlassPane().setVisible(true);
    }
    else {
        if (getGlassPane().isVisible()) {
            getGlassPane().setVisible(false);
        }
    }
}

@Override
public void userSignedIn(User user) {
    removeAll();
    startAndCheck();
}

@Override
public void userSignedOff() {
    startAndCheck();
}

Upvotes: 0

Views: 378

Answers (1)

mKorbel
mKorbel

Reputation: 109823

Do you see any problems here?

  • GlassPane doesn't consume KeyEvents
  • have to add KeyListener to JComponent added to GlassPane, and to override consume()
  • MouseEvents are consumed only in the area that covering JPanel or another JComponent, top component on hierarchy
  • add JLabel (transparent by default) that covering whore RootPane area, add proper LayoutManager to JLabel, then there put JPanel or ....
  • invoke GlassPane from JFrame.getRootPane

Upvotes: 1

Related Questions