Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Swing Problem. Panel not showing up

I am adding a JPanel to the content pane of my app, but its not showing up when i run the application. Its just a grey blank box with a title bar. I dont understand why, i think ive done it like this before and its worked. :mad:

import swing and awt...
declare public class called PokerGUI {...
declare JButtons and such....

// constructor
void PokerGUI() {

infoPanel = new JPanel();
infoPanel.setLayout( new BoxLayout(infoPanel, BoxLayout.Y_AXIS) );

// add the account name information
acntNameLabel = new JLabel("Account Name");
accountNameTF = new JTextField("Jon Anthony");
infoPanel.add( acntNameLabel );
infoPanel.add( accountNameTF );

// add the email address information
emailLabel = new JLabel("Email");
emailTF = new JTextField("MillionDollorMan@microsoft.net");
infoPanel.add( emailLabel );
infoPanel.add( emailTF );

// add the street address information
addressLabel = new JLabel("Address");
addressTF = new JTextField("142 Wood St, Sunburn CT, 09432");
infoPanel.add( addressLabel );
infoPanel.add( addressTF );


// add to contentPane
Container contentPane = getContentPane();
contentPane.setLayout( new FlowLayout() );
contentPane.add(infoPanel);
}

// main method
public static void main(String[] args) {
PokerGUI pk = new PokerGUI();
pk.setVisible(true);
pk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

someone please tell me what i forgot lol, im coming back to java from a long C++ project, and i guess i forgot how to use swing. :/
[1720 byte] By [ZeroFear] at [2007-11-11 7:55:06]
# 1 Re: Swing Problem. Panel not showing up
this won't solve your problem, but just so you know...constructors don't return void, they don't return anything.

I have the same problem sometimes, try changing your layout manager for the form from FlowLayout to GridLayout(1,0), it always works for me.
Nathan87 at 2007-11-11 22:36:58 >
# 2 Re: Swing Problem. Panel not showing up
actualy it did solve my problem. By puting void on my construtor it turned it into a normal method, so when i called new PokerGUI in the main, it was calling a empty constructor.
ZeroFear at 2007-11-11 22:37:59 >
# 3 Re: Swing Problem. Panel not showing up
cool, I didn't think that would fix it, I didn't even think it would compile like that. But hey what works works!
Nathan87 at 2007-11-11 22:39:02 >
# 4 Re: Swing Problem. Panel not showing up
See wether this is want u want to do.

import javax.swing.*;
import java.awt.*;
public class Poker
{
public Component compo()
{

JPanel infoPanel = new JPanel();
infoPanel.setLayout( new BoxLayout(infoPanel, BoxLayout.Y_AXIS) );

// add the account name information
JLabel acntNameLabel = new JLabel("Account Name");
JTextField accountNameTF = new JTextField("Jon Anthony");
infoPanel.add( acntNameLabel );
infoPanel.add( accountNameTF );

// add the email address information
JLabel emailLabel = new JLabel("Email");
JTextField emailTF = new JTextField("MillionDollorMan@microsoft.net");
infoPanel.add( emailLabel );
infoPanel.add( emailTF );

// add the street address information
JLabel addressLabel = new JLabel("Address");
JTextField addressTF = new JTextField("142 Wood St, Sunburn CT, 09432");
infoPanel.add( addressLabel );
infoPanel.add( addressTF );

return infoPanel;
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame("");
Poker pk = new Poker();
Component pg = pk.compo();
f.getContentPane().add(pg);
f.setSize(800,600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
subhakar_nani at 2007-11-11 22:40:08 >