• Advertisement
Sign in to follow this  

can't get swing gui to render...

This topic is 3232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm taking a stab at a simplistic Java Swing app:
	private JLabel label;
	private JTextField txtField;
	private JButton button;

	public static void main(String[] args)
	{
		MyApp window = new MyApp();
		window.setVisible(true);
	}

	public MyApp()
	{
		// Set JFrame properties.
		setSize(400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("My GUI");
		setLayout(null);
		bg = new JPanel();
		bg.setBackground(Color.CYAN);
		
		// Initialize child controls.
		label = new JLabel("A label.");
		txtField = new JTextField(100);
		button = new JButton("Push!");
		
		// Add controls to the 'bg' panel.
		bg.add(label);
		bg.add(txtField);
		bg.add(button);
		add(bg);
		
		// Set preliminary states for all child controls.
		// 1. Locations.
		label.setLocation(10, 10);
		txtField.setLocation(10, 30);
		button.setLocation(10, 50);

		// 2. Enable states.
		setInitEnableStates();
	}




The program compiles just fine and runs, but when it does, I get the following: (1) None of the buttons are visible (seeable) (2) The background "canvas" is default gray and empty (3) When I resize the window to make it larger, the expanded area is shaded Cyan (as per my desired background) until I release the window borders Obviously the repaint() is revealing a cyan background; but it seems to be getting "drawn over" by some mechanism. I also think my null LayoutManager has something to do with it. I do not want to use a manager for this app; I want to custom define *exactly* what controls go where. Any ideas or suggestions? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You seem to be missing some code. Please post the complete code.

I assume your class inherits from JFrame. In which case, it says here that setting the layout manager will result in a runtime exception.

Perhaps that is happening?

Share this post


Link to post
Share on other sites
It could be that your bg JPanel maintains its default width and height of 0. Since there is no layout manager for the frame, there is nothing that tells the panel how big it should be. And don't set the layout manager of the frame to null - set the panel's layout to null, and place all components on the panel where you want them.

Rip-off: in more recent versions of Java, no runtime error is thrown. Java has changed quite a lot since 1.4. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Oysterman
Rip-off: in more recent versions of Java, no runtime error is thrown. Java has changed quite a lot since 1.4. :)


Hey, its not my fault that any Google search for the Java API tends to give results for 1.4 [grin]

Share this post


Link to post
Share on other sites
Oysterman,

Thanks for the tip. I did what you said and now I'm at least getting a full cyan background when the app loads up. But the child control (label, text field and button) are still M.I.A. Here is the complete code:


public class MyApp extends JFrame
{
// Child controls.
private JLabel label;
private JTextField txtField;
private JButton button;

// Main.
public static void main(String[] args)
{
MyApp window = new MyApp();
window.setVisible(true);
}

public MyApp()
{
// Set JFrame properties.
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("My GUI");
bg = new JPanel();
bg.setBackground(Color.CYAN);
bg.setLayout(null);

// Initialize child controls.
label = new JLabel("A label.");
txtField = new JTextField(100);
button = new JButton("Push!");

// Add controls to the 'bg' panel.
bg.add(label);
bg.add(txtField);
bg.add(button);
add(bg);

// Set preliminary states for all child controls.
// 1. Locations.
label.setLocation(10, 10);
txtField.setLocation(10, 30);
button.setLocation(10, 50);

// 2. Enable states.
setInitEnableStates();
}

// Other functions used by the app, such as event handlers and
// private helper routines, etc.
}



I tried experimenting around with the JPanel's dimensions and changing them to be both larger and smaller than the JFrame - interestingly enough it whatever dimensions are set for the JFrame override the bg JPanel. Also, if the bg layout is set to null, I can still set the JFrame's layout manager and get different(weird) results when I mess around with it.

I guess I just don't understand why I'm not seeing the child controls on screen...

Share this post


Link to post
Share on other sites
Had a thought - have Null Layouts been deprecated in modern releases? This might explain some quirky behaviors I am seeing.

Share this post


Link to post
Share on other sites
AFAIK all Swing components get the default width and height of 0, so you'd have to manually set the sizes of buttons, textfields and what have you. Normally the layout manager does this for you, but since the container (the panel) has none... :-)

I believe that the default layout manager is a horizontally-aligned FlowLayout. This means that manually changing your bg panel's size does nothing, since the frame's layout manager has the final word anyway.

I don't think that null layouts are deprecated. They're not recommended, but that's not the same thing. :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement