Why are these panels not compacting?

I have 3 panels one on top of the other within a frame, and each panel contains some components. When I run this code below, I get three panels all with the same size. Ideally, I would like to have each panel compress around the components (at least vertically). Why is this not working here? Please run the code and see what I mean. Thanks.

/**

* Client's Graphical User Interface

*/

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

import java.awt.event.*;

public class ClientUI extends JFrame implements ActionListener {

private JButton btnSearch, btnSearchGet, btnSearchDelete;

private JLabel lblSearch;

private JList searchResultList;

private JTextField txtSearch;

private JTextArea txtMessageLog;

private JScrollPane scrollMessageLog, scrollSearchResultList;

public ClientUI(String title) {

super(title);

setGUI();

} // end ClientUI constructor

// Set up the Graphical User Interface of client

public void setGUI() {

TitledBorder mainTitle, searchResultTitle, messageLogTitle;

// Border color: yellow; Border width: 1

Border linedBorderYellow = BorderFactory.createLineBorder(Color.YELLOW, 2);

Border linedBorderWhite = BorderFactory.createLineBorder(Color.WHITE, 1);

JPanel pane = new JPanel();

mainTitle = BorderFactory.createTitledBorder( linedBorderYellow, "Client Controls" );

mainTitle.setTitleJustification( TitledBorder.LEFT );

pane.setBorder( mainTitle );

pane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

//pane.setPreferredSize( new Dimension(400, 500) );

pane.setLayout( new GridLayout(3, 1) ); // Three rows, one column.

// Declare panel sections

JPanel searchPane = new JPanel();// adds a GridLayout manager

JPanel searchResultPane = new JPanel();

JPanel messageLogPane = new JPanel();

// Add panel sections to the main panel

pane.add( searchPane );

pane.add( searchResultPane );

pane.add( messageLogPane );

// set search panel properties

searchPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

searchPane.setBounds(searchPane.getX(), searchPane.getY(), searchPane.getWidth(), 75);

//searchPane.setPreferredSize( new Dimension(searchPane.getWidth(), 20) );

lblSearch = new JLabel("Search for an object:");

txtSearch = new JTextField( 10 ); // 10 is the number of columns

btnSearch = new JButton("Search");

searchPane.add( lblSearch );

searchPane.add( txtSearch );

searchPane.add( btnSearch );

// set search results panel properties

searchResultPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

searchResultTitle = BorderFactory.createTitledBorder(linedBorderWhite, "Search Results");

searchResultTitle.setTitleJustification( TitledBorder.LEFT );

searchResultPane.setBorder( searchResultTitle );

searchResultPane.setLayout( new GridLayout(2, 1) ); // Two rows, one column.

searchResultPane.setLocation( searchResultPane.getX(), 25 );

// Declare panel sections for Search Results

JPanel searchResultListPane = new JPanel();// upper panel encapsulated within searchResultPane panel

JPanel searchResultButtonPane = new JPanel();// lower panel encapsulated within searchResultPane panel

searchResultPane.add( searchResultListPane );

searchResultPane.add( searchResultButtonPane );

// set search result list panel properties

searchResultListPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

String[] data = {"one", "two", "three", "four"}; // Used for Testing

searchResultList = new JList(data);

scrollSearchResultList = new JScrollPane( searchResultList );

searchResultListPane.add( scrollSearchResultList );

// set search result button's panel properties

searchResultButtonPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

btnSearchGet = new JButton("Get");

btnSearchDelete = new JButton("Delete");

searchResultButtonPane.add( btnSearchGet );

searchResultButtonPane.add( btnSearchDelete );

// set lower panel properties

messageLogTitle = BorderFactory.createTitledBorder(linedBorderWhite, "Message Log");

messageLogTitle.setTitleJustification( TitledBorder.LEFT );

messageLogPane.setBorder( messageLogTitle );

messageLogPane.setBackground( new Color(153, 153, 204) ); // new Color(204, 204, 255) ); // To change, use RGB colors

//messageLogPane.setBounds(messageLogPane.getX(), messageLogPane.getY(), messageLogPane.getWidth(), 75);

txtMessageLog = new JTextArea(5, 30);

txtMessageLog.setEditable(false);

txtMessageLog.append("Hello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\n");

scrollMessageLog = new JScrollPane(txtMessageLog);

scrollMessageLog.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );

messageLogPane.add( scrollMessageLog );// add scrollpane and textarea to the lower panel

getContentPane().add(pane);

} // end setGUI method

// Handle all actions

public void actionPerformed(ActionEvent e){

} // end actionPerformed method

/**

* Entry point of application

* @param args

*/

public static void main(String[] args) {

try {

UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );

}

catch(Exception e){}

JFrame frame = new ClientUI("Client");

// finish setting up the frame, and show it

frame.addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e){ System.exit(0); }

});

frame.pack(); // causes this window to fit layout of its components

frame.setVisible(true);

} // end main method

} // end ClientUI class

[6043 byte] By [stevebju] at [2007-9-30 3:13:20]
# 1

GridLayout ignores the preferred size of its components and make everything the same size to just fill the given space. GridBagLayout respects the preferred size of the components it lays out. Have a look at what your ClientUI does now (// ******** == changes):

import java.awt.*;

import java.awt.event.*;import javax.swing.*;

import javax.swing.border.*;

public class ClientUI extends JFrame implements ActionListener {

private JButton btnSearch, btnSearchGet, btnSearchDelete;

private JLabel lblSearch;

private JList searchResultList;

private JTextField txtSearch;

private JTextArea txtMessageLog;

private JScrollPane scrollMessageLog, scrollSearchResultList;

public ClientUI(String title) {

super(title);

setGUI();

} // end ClientUI constructor

// Set up the Graphical User Interface of client

public void setGUI() {

TitledBorder mainTitle, searchResultTitle, messageLogTitle;

// Border color: yellow; Border width: 1

Border linedBorderYellow = BorderFactory.createLineBorder(Color.YELLOW, 2);

Border linedBorderWhite = BorderFactory.createLineBorder(Color.WHITE, 1);

JPanel pane = new JPanel();

mainTitle = BorderFactory.createTitledBorder( linedBorderYellow,

"Client Controls" );

mainTitle.setTitleJustification( TitledBorder.LEFT );

pane.setBorder( mainTitle );

pane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

//pane.setPreferredSize( new Dimension(400, 500) );

/******* !!! *******/

GridBagLayout gridbag = new GridBagLayout();// *******

GridBagConstraints gbc= new GridBagConstraints(); // *******

gbc.gridwidth = gbc.REMAINDER; // *******

pane.setLayout( gridbag ); // *******

// Declare panel sections

JPanel searchPane = new JPanel(); // adds a GridLayout manager

JPanel searchResultPane = new JPanel();

JPanel messageLogPane = new JPanel();

// Add panel sections to the main panel

pane.add( searchPane, gbc );// ********

pane.add( searchResultPane, gbc ); // ********

pane.add( messageLogPane, gbc );// ********

// set search panel properties

searchPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

// *******searchPane.setBounds(searchPane.getX(), searchPane.getY(), searchPane.getWidth(), 75);

//searchPane.setPreferredSize( new Dimension(searchPane.getWidth(), 20) );

lblSearch = new JLabel("Search for an object:");

txtSearch = new JTextField( 10 ); // 10 is the number of columns

btnSearch = new JButton("Search");

searchPane.add( lblSearch );

searchPane.add( txtSearch );

searchPane.add( btnSearch );

// set search results panel properties

searchResultPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

searchResultTitle = BorderFactory.createTitledBorder(linedBorderWhite, "Search Results");

searchResultTitle.setTitleJustification( TitledBorder.LEFT );

searchResultPane.setBorder( searchResultTitle );

/******** !!! ********/

searchResultPane.setLayout( gridbag ); // ********

// ********searchResultPane.setLocation( searchResultPane.getX(), 25 );

// Declare panel sections for Search Results

JPanel searchResultListPane = new JPanel(); // upper panel encapsulated within

JPanel searchResultButtonPane = new JPanel(); // lower panel encapsulated within

searchResultPane.add( searchResultListPane, gbc ); // *********

searchResultPane.add( searchResultButtonPane, gbc );// *********

// set search result list panel properties

searchResultListPane.setBackground( new Color(153, 153, 204) ); // To change, use RGB colors

String[] data = {"one", "two", "three", "four"}; // Used for Testing

searchResultList = new JList(data);

searchResultList.setPreferredSize(new Dimension(175,

searchResultList.getPreferredSize().height));

scrollSearchResultList = new JScrollPane( searchResultList );

searchResultListPane.add( scrollSearchResultList );

// set search result button's panel properties

searchResultButtonPane.setBackground( new Color(153, 153, 204) );

btnSearchGet = new JButton("Get");

btnSearchDelete = new JButton("Delete");

searchResultButtonPane.add( btnSearchGet );

searchResultButtonPane.add( btnSearchDelete );

// set lower panel properties

messageLogTitle = BorderFactory.createTitledBorder(linedBorderWhite, "Message Log");

messageLogTitle.setTitleJustification( TitledBorder.LEFT );

messageLogPane.setBorder( messageLogTitle );

messageLogPane.setBackground( new Color(153, 153, 204) );

//messageLogPane.setBounds(messageLogPane.getX(), messageLogPane.getY(),messageLogPane.getWidth(), 75);

txtMessageLog = new JTextArea(5, 30);

txtMessageLog.setEditable(false);

txtMessageLog.append("Hello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\n");

scrollMessageLog = new JScrollPane(txtMessageLog);

scrollMessageLog.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );

messageLogPane.add( scrollMessageLog ); // add scrollpane and textarea to the lower panel

getContentPane().add(pane);

} // end setGUI method

// Handle all actions

public void actionPerformed(ActionEvent e) {

} // end actionPerformed method

/**

* Entry point of application

* @param args

*/

public static void main(String[] args) {

try {

UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );

}

catch(Exception e){}

JFrame frame = new ClientUI("Client");

// finish setting up the frame, and show it

frame.addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e){ System.exit(0); }

});

frame.pack(); // causes this window to fit layout of its components

frame.setVisible(true);

} // end main method

} // end ClientUI class

74philip at 2007-6-29 13:23:36 > top of Java-index,Archived Forums,Socket Programming...