HomeComputer programmingJava programmingJava: Null Layout Manager (Swing)

Java: Null Layout Manager (Swing)

The following tutorial describes how to use the Null Layout Manager.


Layout Managers are used in the organization of Panels and Frames. The proper layout should be chosen to accommodate frame resizing and use.

Note: The + signs represent empty space.

Null:
*This indicates no Layout. Items must be manually positioned and arranged.
This layout should only be used if the window will not and cannot be resized, as the item in the window will stay where they are placed, be that hidden or clumped in one corner of a window.

This Layout is simple to use, but it is not all that professional, so be careful where you use it.

CODE example:
import java.awt.event.*;
import javax.swing.*;
public class NoLayoutExample extends JFrame {

public NoLayoutExample(String name) {
super(name);

JTextField newItemField;
JList itemsList;
JButton addButton;
JButton removeButton;

getContentPane().setLayout(null);

//The text field
newItemField = new JTextField();
newItemField.setLocation(12,12);
newItemField.setSize(150,30);
getContentPane().add(newItemField);

//The Add button
addButton = new JButton(“Add”);
addButton.setMnemonic(‘A’);
addButton.setLocation(174, 12);
addButton.setSize(100,30);
getContentPane().add(addButton);

//The List
itemsList = new JList();
JScrollPane scrollPane = new JScrollPane(itemsList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setLocation(12,45);
scrollPane.setSize(150,150);
getContentPane().add(scrollPane);

//The Remove button
removeButton = new JButton(“Remove”);
removeButton.setMnemonic(‘R’);
removeButton.setLocation(174,45);
removeButton.setSize(100,30);
getContentPane().add(removeButton);
}

public static void main(String[] args) {
JFrame frame = new NoLayoutExample(“NULL Example”);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(286, 230);
frame.setResizable(false);
frame.setVisible(true);
}
}

*NOTE: This frame’s buttons do not actually do anything. This is simply an example of using the Layout Manager.
-Try changing the fram.setResizable to true, and resize the frame.

Questions or Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

Cleveland Furia on How to Clear Google Activity
Sid Wohlfarth on How to Clear Google Activity
Riley Magsayo on How to Clear Google Activity
สมัครรับเครดิตฟรี ทันที on How to improve Remote Desktop Protocol Performance
Neal Wangstad on How to Clear Google Activity
Slavcho Andov on How to install Ubuntu
Slavcho Andov on How to fix bad sectors
Sheraz Ali on How to Backup Android