1

I have a class that creates a frame.

public class GameDisplay{

....

public void createDisplay(){
    frame=new JFrame(title);
    canvas=new Canvas();
    canvas.setPreferredSize(new Dimension(width,height));
    canvas.setFocusable(false);
    frame.setSize(width,height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.add(canvas);
    frame.pack();
}
public Canvas getCanvas(){
    return this.canvas;
}

public JFrame getFrame(){
    return frame;
}

If I have another class that would add Panels and Buttons to the frame, how can I add them? I have tried:

GameDisplay g;
Container c;
c = g.getFrame().getContentPane();

But it returns NullPointer Error. Thus, I can't seem to add panels to it.

kahjt
  • 13
  • 1
  • 1
  • 6

1 Answers1

-1

Attach your JFrame made in createDisplay() to a static variable. Then access that static variable from another class.

Like this

public static JFrame frame1;

Then in createDisplay()

GameDisplay.frame1 = frame;

In another class to get the content pane just do

c = GameDisplay.frame1.getContentPane();

Hope this helped!

RoccoDev
  • 518
  • 4
  • 14
  • Thank youuuu! It worked! I mean, the Exception Error is now gone! =) But now, my problem is I tried adding button to the container, but it still doesn't show up in the frame. How can I refresh the frame to make the container appear? Sorry, I'm still learning the curves of GUI. – kahjt May 29 '16 at 11:50
  • Tick the answer (the green tick) to mark as solved. Also leave a vote if helped! – RoccoDev May 29 '16 at 11:52
  • You are neither declaring nor assigning a button variable (from the code above)... You should make a JButton variable and call frame.add(button) – RoccoDev May 29 '16 at 11:53
  • 2
    *"Attach your JFrame made in createDisplay() to a static variable."* The need to make GUI elements static is a sign of a poor design and will likely cause more problems that it fixes. **Don't do this at home!** – Andrew Thompson May 29 '16 at 11:57
  • I know, but for the needs of kahjt it's ok – RoccoDev May 29 '16 at 11:58
  • Yup, I did try making a Jbutton, adding that button to the c(container code above). And then GameDisplay.frame1.add(c). But then it resulted into an IllegalArgumentException: adding container's parent to itself error. So I tried adding the button to a panel and adding that panel to frame1, instead. But still, the frame displays nothing. Can you still help? Thank you! – kahjt May 29 '16 at 12:03
  • then, you should do frame.setVisible(true) – RoccoDev May 29 '16 at 12:06
  • I already have frame.setVisible(true) in the GameDisplay class. I'm thinking that it has something to do with adding new components after the frame is packed. Because my program does this: createDisplay() first(so the frame is already packed), then add new components. Do you know how could I refresh the frame to add those new components and pack again? I'm really confused. – kahjt May 29 '16 at 12:12
  • You should add components before the frame is getting packed – RoccoDev May 29 '16 at 12:14