Java: Null Layout Manager (swing)
how to use the Null Layout Manager
Layout Managers are used in the orginization of Panels and Frames. The proper layout should be chosen to accomodate, frame resizings and use.
+ signs represent empty space.
Null:
*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[b] clumped in one corner of a window.
This Layout is [b]Very simple to use, but 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: william_a_wilson@hotmail.com
-William. § (marvin_gohan)






Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment