Sitemap

Head First Java (Chapter 13) A Very Graphic Story

5 min readJul 18, 2022

--

Hi everyone, Today I am going to explain the important points of Head First Java chapter 13. I am mainly focusing on swing components.

The component is the same name as the widget. These components can use with our GUI. For example Text fields, buttons, scrollable lists, radio buttons, etc. In a swing virtually all components are capable of holding other components. In a previous article, we learn how to create a Jframe. And here I will explain how to work with panels, layouts, and swing components.

Layout Manager

A layout manager is a java object. It associates with the particular components, almost always a background component. A layout manager is responsible for the size and placement of components. The layout manager has several flavors. If we create a background component that background component has its own layout manager.

We can identify three main layout managers.

Border Layout

This border layout manager divides background components into five regions. That regions are east, west, north, south, and center. We can add only one component to each region. Components laid out by this manager usually don’t get to have their preferred size. It is the default layout manager for a frame.

Sample Code-

class A {public static void main(String[] args) {
A gui = new A();
gui.go();
}public void go() {
JFrame frame = new JFrame();
JButton east = new JButton("East");
JButton west = new JButton("West");
JButton north = new JButton("North");
JButton south = new JButton("South");
JButton center = new JButton("Center");
frame.getContentPane().add(BorderLayout.EAST, east);
frame.getContentPane().add(BorderLayout.WEST, west);
frame.getContentPane().add(BorderLayout.NORTH, north);
frame.getContentPane().add(BorderLayout.SOUTH, south);
frame.getContentPane().add(BorderLayout.CENTER, center);
frame.setSize(300, 300);
frame.setVisible(true);
}
}

Output-

Flow Layout

FlowLayout manager works like a word processor. So when a component won’t fit horizontally, it drops to the next “line” in the layout. FlowLayout is the default layout manager for a panel.

Sample Code-

class A {public static void main(String[] args) {
A gui = new A();
gui.go();
}public void go() {JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
JButton button = new JButton("Show me");
panel.add(button);
frame.getContentPane().add(BorderLayout.EAST, panel);
frame.setSize(400, 400);
frame.setVisible(true);
}}

Output-

Press enter or click to view image in full size

Box Layout

It works like a Flowlayout, but each component gets to have its own size, and the components are placed in the order in which they’re added. The box Layout manager can stack the components vertically or horizontally.

Sample Code-

class A {public static void main(String[] args) {
A gui = new A();
gui.go();
}public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("Show me");
JButton buttonTwo = new JButton("Button 02");
panel.add(button);
panel.add(buttonTwo);
frame.getContentPane().add(BorderLayout.EAST, panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
}

Output-

Swing Components-

JTextField-

Constructors

JTextField field = new JTextField(20);
JTextField field = new JTextField(“Your name”);

How to use it

Get Kalpa Sandaruwan’s stories in your inbox

Join Medium for free to get updates from this writer.

Get text out of it

System.out.println(field.getText());

Put text in it

field.setText(“whatever”); 
field.setText(““);

Get an ActionEvent when the user presses return or enter

 field.addActionListener(myActionListener);

Put the cursor back in the field

field.requestFocus();

JTextArea

Constructor

JTextArea text = new JTextArea(10,20);

How to use it

Make it have a vertical scrollbar only

JScrollPane scroller = new JScrollPane(text); text.setLineWrap(true);

Replace the text that’s in it

text.setText(“Not all who are lost are wandering”);

Append to the text that’s in it

text.append(“button clicked”);

Put the cursor back in the field

text.requestFocus();

JCheckBox

Constructor

JCheckBox check = new JCheckBox(“Goes to 11”);

How to use it

Listen for an item event (when it’s selected or deselected)

 check.addItemListener(this);

Handle the event (and find out whether or not it’s selected)

public void itemStateChanged(ItemEvent ev) { 
String onOrOff = “off”;
if (check.isSelected()) onOrOff = “on”;
System.out.println(“Check box is “ + onOrOff);
}

JList

Constructor

String [] listEntries = {“alpha”, “beta”, “gamma”, “delta”,  “epsilon”, “zeta”, “eta”, “theta “}; 
JList list = new JList(listEntries);

How to use it

Make it have a vertical scrollbar

JScrollPane scroller = new JScrollPane(list); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panel.add(scroller);

Set the number of lines to show before scrolling

list.setVisibleRowCount(4);

Register for list selection events

list.addListSelectionListener(this);

Important Points in the chapter.

Layout managers control the size and location of components nested within other components.

When you add a component to another component (sometimes referred to as a background component, but that’s not a technical distinction), the added component is controlled by the layout manager of the background component.

A layout manager asks components for their preferred size, before making a decision about the layout. Depending on the layout manager’s policies, it might respect all, some, or none of the component’s wishes.

The BorderLayout manager lets you add a component to one of five regions. You must specify the region when you add the component, using the following syntax: add(BorderLayout.EAST, panel);

With BorderLayout, components in the north and south get their preferred height, but not width. Components in the east and west get their preferred width, but not height. The component in the center gets whatever is left over (unless you use pack()).

The pack() method is like shrink-wrap for the components; it uses the full preferred size of the center component, then determines the size of the frame using the center as a starting point, building the rest based on what’s in the other regions.

FlowLayout places components left to right, top to bottom, in the order they were added, wrapping to a new line of components only when the components won’t fit horizontally.

FlowLayout gives components their preferred size in both dimensions.

References

[1]Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021)

--

--