Java Video Tutorial 28

Swing Layout ManagersIn this part of my Java Video Tutorial, I cover 3 different Swing layout managers. I revisit the the layout manager we have been using by default being Flow Layout. As well I introduce the Border and Box layout managers.

I answer many common questions like how do you get to components in a Border frame quadrant? How do I add space between components and much more.

The code follows the video and will help you better learn.

If you like videos like this share it

Code from the Video

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

public class Lesson28 extends JFrame{

	JButton button1, button2, button3, button4, button5;
	String outputString = "";
	public static void main(String[] args){
		new Lesson28();
	public Lesson28(){
		// Create the frame, position it and handle closing it
		this.setTitle("My Sixth Frame");
		// Create a flow layout (Default)
		JPanel thePanel = new JPanel();
		// Define the flow layout alignment
		// FlowLayout.RIGHT, FlowLayout.CENTER
		thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		// You can also define the pixels that separate the components
		// FlowLayout(alignment, horz gap, vertical gap)
		 button1 = new JButton("Button 1");
		JPanel thePanel = new JPanel();
		thePanel.setLayout(new BorderLayout());
		// Create buttons
		button1 = new JButton("Button 1");
		button2 = new JButton("Button 2");
		button3 = new JButton("Button 3");
		button4 = new JButton("Button 4");
		button5 = new JButton("Button 5");
		// If you put components in the same space the
		// last one in stays and everything else goes
		// EX.
		// thePanel.add(button1, BorderLayout.NORTH);
		// thePanel.add(button2, BorderLayout.NORTH);
		// Only button2 shows
		thePanel.add(button1, BorderLayout.NORTH);
		thePanel.add(button2, BorderLayout.SOUTH);
		thePanel.add(button3, BorderLayout.EAST);
		thePanel.add(button4, BorderLayout.WEST);
		thePanel.add(button5, BorderLayout.CENTER);
		/* If you want more than one component to show
		// up in the same part of a border layout put
		// them in a panel and then add the panel to
		// the border layout panel
		JPanel thePanel2 = new JPanel();
		thePanel.add(thePanel2, BorderLayout.NORTH);
		/* BOX LAYOUT */
		Box theBox = Box.createHorizontalBox();
		// You can also use Box theBox = Box.createVerticalBox();
		// You can also separate the components with struts
		// A rigid area gives you the option to space using
		// horizontal and vertical spacing
		// theBox.add(Box.createRigidArea(new Dimension(30, 20)));
		// When you use a glue you position the components as
		// far apart as possible while remaining on the screen
		// There is also a createVerticalGlue
		// this.add(thePanel); // Don't use for BOX LAYOUT

6 Responses to “Java Video Tutorial 28”

  1. mr.roshik says:

    hi derek, missed you, i only like net tut plus and all your tutorial to learn web design, almost 2 months i was away from internet, i will be regular here 🙂 god bless u, take care

    • admin says:

      Thanks for stopping by 🙂 It’s funny, that website asked me to do tutorials for them They are in the top 615 in regards to traffic online. I’m not even in that ballpark. I rank around 87,000

  2. Gotti says:

    follow me @MoeStillBlazin #F4F

  3. lidosurf says:

    Hello Again Derek –

    I am an old mainframe guy trying to make the transition. Your tutorials are great! I would like to focus mostly on server side Java. Could you advise me what I should concentrate on (and stay away from).

    Thanks for doing such a Great job.

    • Derek Banas says:

      Thank you very much 🙂 I have sadly not spent a lot of time covering JEE, which is what I think you are looking for. I had a vote a while back and asked everyone if they wanted to see how to develop tablet apps for android, or JEE and Android won. I plan on covering JEE some day, but it will probably be a while till I get to it. Sorry about that

      • lidosurf says:

        Thanks Derek. Your feedback is apriciated.
        I’ll start looking into JEE. I’ll have a head start when you post your video.

Leave a Reply

Your email address will not be published.