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

