In this example, we will learn how to use BorderLayout in GUI/swing based applications.
BorderLayout is a simple layout manager that can be handy in certain layouts. It is a default layout manager for JFrame, JWindow, JDialog, JInternalFrame, and JApplet.
Java Swing BorderLayout Example
The following example demonstrates the usage of a BorderLayout manager. Note that in the below example, a BorderLayout object has five areas. These areas are specified by the BorderLayout constants:
PAGE_START
PAGE_END
LINE_START
LINE_END
CENTER
Here is the complete code:
package net.javaguides.javaswing.examples;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
/**
* Class demonstrates the usage of BorderLayout.
* @author javaguides.net
*
*/
public class BorderLayoutDemo {
public static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
// set BorderLayout to Panel
pane.setLayout(new BorderLayout());
if (!(pane.getLayout() instanceof BorderLayout)) {
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(
java.awt.ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);
//Make the center component big, since that's the
//typical usage of BorderLayout.
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("BorderLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Output
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment