vendredi 8 juillet 2016

JLayeredPane returning null on getParent()

My situation: I have a class MainScreen that extends JFrame (it really is just a JFrame with a main method to start the application) on which I added a class GameManager that extends JLayeredPane, that I'm using to display something.

public static void main(String[] args) {
    MainScreen ms = new MainScreen();
}

public MainScreen() {
    this.initScreen();
    this.gm = new GameManager();
    this.add(gm, BorderLayout.CENTER);
    this.setVisible(true);
}       

Now, what I want to do is, from the GameManager class I want to add a JButton to the main JFrame. I thought it would be easy, just do:

JButton button = new JButton("Hello");
this.getParent().add(button, BorderLayout.SOUTH);

but getParent() is returning null, so obviously it doesn't work. I don't know why though, I did something similar before (with a JComponent and a JPanel though), and I thought that every JComponent when added to a container would have the container as its parent. What did I miss?

Aucun commentaire:

Enregistrer un commentaire