JPanel not showing in JFrame
Well I had left GUI's for a while, and now I decided to create one. I got stuck adding my JPanel to my JFrame, and over Google all I found were tons of arguments with revalidate vs validate vs repaint vs awt vs swing. I have already looked at Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components), JPanel (Java 2 Platform SE 5.0) and JFrame (Java Platform SE 6) but that didn't help for this particular problem. I created an SSCCE, here is the code.
JFrame:
Code Java:
import javax.swing.JFrame;
import javax.swing.JComponent;
public class basicJFrame extends JFrame
{
//Constructors
/**
* Create a very simple JFrame, create a new instance of ButtonGrid and show it.
*/
public basicJFrame()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
ButtonGrid jpanel = new ButtonGrid();
getContentPane().add(jpanel);
((JComponent)getContentPane()).revalidate(); //Didn't do anything
setVisible(true);
}
//Methods
public static void main(String[] args)
{
new basicJFrame();
}
}
JPanel:
Code Java:
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
/**
* JButton Grid using JButtons. Uses GridLayout
*/
public class ButtonGrid extends JPanel
{
/**
* Creates the buttons and adds them
*/
public void ButtonGrid()
{
setLayout(new GridLayout(3,2));
add(new JButton("1"));
add(new JButton("2"));
add(new JButton("3"));
add(new JButton("4"));
add(new JButton("5"));
add(new JButton("6"));
}
}
My problem is that the JPanel just will not show up in my JFrame. Thanks for any help :)
**EDIT**
This should probably be in AWT / Swing forums.. I can't believe I missed that.
Re: JPanel not showing in JFrame
Hi Tjstretch,
I have moved this into the correct forum for you, please have a broswe of all forum title for future refence.
In regards to your problem, you are over complicating it. Your ButtonGrid class doesn't have a constructor, try removing void ;)
Everyone hates being caught by silly things like that haha!
Regards,
Chris
Re: JPanel not showing in JFrame
Thank you for moving it, and as to the constructor, I can't believe I missed that. Sigh, obviously it was to early when I did that >.>
It works! Setting the thread to solved.