Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot resize swing components

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
US
I am having a problem resizing swing components on my GUI. I want to have a "status box" at the bottom of my JFrame to display what is going on and the top of the frame is going to hold panels that will be switched out depending on what menu is showing. However, using the setSize() or setLocation() methods does nothing. It's been a while since I've worked with GUIs and all of my code samples are at home. Thanks for any help:

public class TestGUI extends JFrame
{
private Container c;
private JPanel dispPanel, statusPanel;
private JComboBox userCombo;
private JTextArea status;

public TestGUI()
{
super();

Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

double screenX = dim.getWidth();
double screenY = dim.getHeight();
System.out.println(screenX);
System.out.println(screenX / 2);
super.setLocation((int)(screenX / 2) - 300, (int)(screenY / 2) - 300);
super.setSize(500, 500);
super.getContentPane().setLayout(new BoxLayout(super.getContentPane(), BoxLayout.Y_AXIS));
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

status = new JTextArea();
userCombo = new JComboBox();

status.setBackground(Color.black);
status.setForeground(Color.green);
status.setSize(100, 100);
status.setText("test");

dispPanel = new JPanel();
dispPanel.setLayout(new FlowLayout());
dispPanel.setSize(500, 250);
dispPanel.add(userCombo);

statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout());
statusPanel.setSize(500, 250);
statusPanel.add(status);

dispPanel.add(userCombo);
statusPanel.add(status);

c = super.getContentPane();

c.add(dispPanel);
c.add(statusPanel);

super.setVisible(true);
}

-Greg :-Q

flaga.gif
 
//Don't use any layoutmanager
public TestGUI()
{
super();
super.getContentPane().setLayout(null);
...
status.setPreferredSize(New Dimension(100, 100));
// use setPreferredSize
}

// it is better for you to use only layoutmanager or absolute positioning like setSize, setLocation and setBounds
 
Using a null layout and positioning stuff yourself is not recommended if the application window could be run on machines with different resolution capabilities.

The whole point of using the LayoutManagers is to get controlled layouts (of various kinds) while allowing the API to decide how best to fit everything to the available space. If you do use the LayoutManagers, you'll usually need to set the preferredSize of components to indicate how big the things would like to be and let the LayoutManagers do the rest. The setSize, setLocation and setBounds are controlled for you. Setting them will have no effect.

Tim
 
This program will only be used on one computer...however for future reference, I should use the preferedSize() method?

-Greg :-Q

flaga.gif
 
If you want/need to use the LayoutManagers, yes.

A well layed-out panel will resize nicely (most of the time) for different screen sizes, and when the user adjusts the window size manually.

It can get quite tricky sometimes, though, to get things looking right with this approach. You can get unexpected results if you're not explicitly setting the preferredSize() on the correct component(s) as far as the LayoutManagers see it. It can eat up precious time twiddling around until you find the culprit. Like anything, this gets easier with experience and the results are rewarding (IMO).

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top