Layout Managers
Layout Managers
This section shows example GUIs that use these layout managers, and tells you where to find the how-to page for each layout manager. You can find links for running the examples in the how-to pages and in the example index.
BorderLayout
Every content pane is initialized to use a BorderLayout. (As Using Top-Level Containers explains, the content pane is the main container in all frames, applets, and dialogs.) A BorderLayout places components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area. Tool bars that are created using JToolBar must be created within a BorderLayout container, if you want to be able to drag and drop the bars away from their starting positions.
add(component, BorderLayout.CENTER) //preferred
BoxLayout
The BoxLayout class puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.
CardLayout
The CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays. An alternative to using CardLayout is using a tabbed pane, which provides similar functionality but with a pre-defined GUI.
FlowLayout
is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide. Both panels in CardLayoutDemo, shown previously, use FlowLayout.
FlowLayout
GridBagLayout
is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.
GridBagLayout
GridLayout
simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
GridLayout
GroupLayout
is a layout manager that was developed for use by GUI builder tools, but it can also be used manually. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. Consequently, however, each component needs to be defined twice in the layout. The Find window shown above is an example of a GroupLayout.
GroupLayout
SpringLayout
is a flexible layout manager designed for use by GUI builders. It lets you specify precise relationships between the edges of components under its control. For example, you might define that the left edge of one component is a certain distance (which can be dynamically calculated) from the right edge of a second component. SpringLayout lays out the children of its associated container according to a set of constraints.
SpringLayout
NO LAYOUT:Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layout manager. With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes.
setBounds(int x-coordinate, int y-coordinate, int width, int height): This is the method of the Container class. This method sets the components according to the mentioned coordinate values in the method as parameter. This method takes four argument as follows:
First is the X-coordinate of the frame. Second is the Y-coordinate of the frame. Third is the width of the component. And last determines the height of the component.
These values are passed through the setBounds() method after setting the layout manager using the setLayout() method for the Container.
import javax.swing.*; import java.awt.*; public class AbsCoordinate{ public static void main(String[] args) { JFrame frame = new JFrame("Laying Out Components Using Absolute Coordinates "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(null); JLabel label1 = new JLabel("RoseIndia.Net"); JTextField field = new JTextField(20); JButton button1 = new JButton("OK"); JButton button2 = new JButton("Cancel"); label1.setBounds(100, 50, 100, 50); field.setBounds(75, 100, 200, 25); button1.setBounds(40, 200, 75, 25);
button2.setBounds(200, 200, 75, 25); panel.add(label1); panel.add(field); panel.add(button1); panel.add(button2); frame.add(panel); frame.setSize(400,400); frame.setVisible(true); } }
Layout managers have different strengths and weaknesses. This section discusses some common layout scenarios and which layout managers might work for each scenario. The scenarios listed below are given for information purposes, in case you
are curious about which type of manager is used in different situations, or in case you absolutely must code your manager manually. If none of the layout managers we discuss is right for your situation, feel free to use other layout managers that you may write or find. Also keep in mind that flexible layout managers such as GridBagLayout and SpringLayout can fulfill many layout needs. Scenario: You need to display a component in as much space as it can get. If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or GridBagLayout might be a good match. Scenario: You need to display a few components in a compact row at their natural size. Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager.SpringLayout is also good for this. Scenario: You need to display a few components of the same size in rows and columns. GridLayout is perfect for this. Scenario: You need to display a few components in a row or column, possibly with varying amounts of space between them, custom alignment, or custom component sizes. BoxLayout is perfect for this. Scenario: You need to display aligned columns, as in a form-like interface where a column of labels is used to describe text fields in an adjacent column. SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a makeCompactGrid method that lets you easily align multiple rows and columns of components. Scenario: You have a complex layout with many components. Consider either using a very flexible layout manager such as GridBagLayout or SpringLayout, or grouping the components into one or more JPanels to simplify layout. If you take the latter approach, each JPanel might use a different layout manager.
This section illustrates you how to show the icon on the button in Java Swing.
Code description:
import javax.swing.*; import java.awt.*; public class IconButton{ public static void main(String[] args){ JFrame frame = new JFrame("Icon on button"); JButton button = new JButton("Roseindia.net"); Icon imgicon = new ImageIcon("icon_confused.gif"); JPanel panel = new JPanel(); button.setIcon(imgicon); panel.add(button); frame.add(panel, BorderLayout.NORTH); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} }
Here, you will see a simple toolbar, Which have three command buttons like: cut, copy and paste with small images. For showing these three buttons on the toolbar of the frame various APIs as follows have been used:
JToolBar(): This is the constructor of the JToolBar class which has been used to create a new toolbar.
JToolBar.HORIZONTAL This is the constant field of the JToolBar class which support to situating the Tool Bar horizontal on the frame.
import javax.swing.*; import java.awt.*; public class CreateToolbar{ public static void main(String[] args) { JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste"); JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL); JButton cutbutton = new JButton(new ImageIcon("cut.gif")); toolbar.add(cutbutton); JButton copybutton = new JButton(new ImageIcon("copy.gif")); toolbar.add(copybutton); JButton pastebutton = new JButton(new ImageIcon("paste.gif")); toolbar.add(pastebutton); frame.getContentPane().add(toolbar,BorderLayout.NORTH); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,400); frame.setVisible(true);
} }
This is written like: JCheckBox chk = new JCheckBox("This is the Check Box"); This component of the javax.swing.*; is added to the frame using the add(component) method of the JFrame class.
ButtonGroup: This is the class of the javax.swing.*; package, which is used to create a group of radio buttons from which you can select only one option from that group of the radio buttons. This is class is used by creating a instance of if using it's constructor. Radio Buttons are added to the specified group using the add(JRadioButton) method of the ButtonGroup class.
JRadioButton: This is the class has been used to create a single radio button for the application.
setSelected(): This method sets the value of the radio button. This method takes a boolean value either true or false. If you pass true value then the radio button will be selected otherwise the radio button is not selected.
import javax.swing.*; import java.awt.*; public class CreateRadioButton{ public static void main(String[] args) { CreateRadioButton r = new CreateRadioButton(); } public CreateRadioButton(){ JRadioButton Male,Female; JFrame frame = new JFrame("Creating a JRadioButton Component"); JPanel panel = new JPanel(); ButtonGroup buttonGroup = new ButtonGroup(); Male = new JRadioButton("Male"); buttonGroup.add(Male); panel.add(Male); Female = new JRadioButton("Female"); buttonGroup.add(Female); panel.add(Female); Male.setSelected(true); frame.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,400); frame.setVisible(true); } }