Java Swing: Panels (Video Tutorial Part 3)

How to add forms to your Swing application using JPanel. In this tutorial I show you how to begin to organise your Swing app and divide it into separate areas; in the next tutorial we'll get onto actually using GridBagLayout to arrange the controls.

After starting the video, click the maximise button to make it fullscreen so you can see the code!




Code For This Tutorial



Main app:

 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
public class App {
 
    public static void main(String[] args) {
 
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new MainFrame("Hello World Swing!");
                frame.setSize(500, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
 

Window class:

 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class MainFrame extends JFrame {
    
    private DetailsPanel detailsPanel;
    
    public MainFrame(String title) {
        super(title);
        
        // Set layout manager
        setLayout(new BorderLayout());
        
        // Create Swing component
        final JTextArea textArea = new JTextArea();
        JButton button = new JButton("Click me!");
        
        detailsPanel = new DetailsPanel();
        
        // Add Swing components to content pane
        Container c = getContentPane();
        
        c.add(textArea, BorderLayout.CENTER);
        c.add(button, BorderLayout.SOUTH);
        c.add(detailsPanel, BorderLayout.WEST);
        
        // Add behaviour
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append("Hellon");
            }
            
        });
    }
}



 

Panel class (we'll complete this in the next tutorial):

 
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class DetailsPanel extends JPanel {
    public DetailsPanel() {
        Dimension size = getPreferredSize();
        size.width = 250;
        setPreferredSize(size);
        
        setBorder(BorderFactory.createTitledBorder("Personal Details"));
        
        JLabel nameLabel = new JLabel("Name: ");
        JLabel occupationLabel = new JLabel("Occupation: ");
        
        JTextField nameField = new JTextField(10);
        JTextField occupationField = new JTextField(10);
        
        JButton addBtn = new JButton("Add");
        
        setLayout(new GridBagLayout());
        
        GridBagConstraints gc = new GridBagConstraints();
        
        
    }
}