Java Chapter -13
Hi all! I am back with another article. In this article we are going to learn about Java Swing. Let’s get into the business.
What’s a component? The things we put in a GUI. The things a user sees and interacts with. Textfields, buttons, scrollable lists, radio buttons, etc. are all components. In fact, they all extend javax.swing.JComponent.
In Swing, virtually all components are capable of holding other components. In other words, you can stick just about anything into anything else. For example, A JPanel is usually used as a background for grouping other components, but even a JPanel can be interactive. Just as with other components, you can register for the JPanel’s events including mouse clicks and keystrokes.
Have anyone of you heard about Layout Manager? If you don’t know that’s fine. Layout Manager objects control the size and location of the widgets in a Java GUI. Layout Manager is in charge of the size and placement of components. It decided how big buttons should be, and where they are relative to each other and the frame.
Layout managers come in many variants, and each background component can have its own layout manager. Layout managers have their own policies to follow when building a layout.
For example, one layout manager might insist that all components in a panel must be the same size, arranged in a grid, while another layout manager might let each component choose its own size, but stack them vertically.
output of above code is looks like below,
How does a layout manager arrange their component how they decide it?
Get farook fazni’s stories in your inbox
Join Medium for free to get updates from this writer.
Layout component might have different policies for arranging components, but r. In general, the process of laying out a background component looks something like this,
- create a panel and add three buttons to it.
- The panel’s layout manager asks each and every button that how much size that button prefers to be.
- The panel’s layout manager uses its layout policies to decide whether it should respect all the buttons’ preferences, part of their preference, or none of the their preferences.
- Add the panel to a frame.
- The frame’s layout manager asks the panel that how much size the panel prefers to be.
- The frame’s layout manager uses its layout policies to decide whether it should respect all, part, or none of the panel’s preferences as in 3rd point
There are Three big Layout managers and they are,
BorderLayout
The BorderLayout manager divides a background component into five regions. we can add only one component per region to a background that controlled by a BorderLayout manager. BorderLayout Manager is the default layout manager for a frame. BorderLayout cares about five regions.
And the output is looks like below image.
Flow Layout
FlowLayout arrange the Components from left to right and top to bottom, in the order of that they were added. Therefore, FlowLayout serves as a JPanel’s default layout manager. When the panel added to the frame, the BorderLayout manager controls the panel’s size and placement, but the FlowLayout manager controls the panel’s internal elements. FlowLayout is the default layout manager for a panel. FlowLayout cares about the flow of the components.
BoxLayout
A BoxLayout manager is almost similar like FlowLayout. In BoxLayout each component can have its own size, and the components are placed in the order which they’re added. But, unlike FlowLayout, a BoxLayout manager can stack the components vertically. It’s just same like a FlowLayout but instead of having automatic ‘component wrapping’, you can insert a sort of ‘component return key’ and force the components to start a new line. BoxLayout to the rescue.
Let’s take a look at some component and how to use them,
JTextField
Constructors:
JTextField jTextField= new JTextField(30);//30 means column not pixels
JTextField jTextField= new JTextField(“Enter your name”);
To Get the Output we use:
System.out.println(jTextField.getText());
To Input the Text:
jTextField.setText(“here”);
jTextField.setText(“ “);
Get an ActionEvent when the user press the return or enter:
jTextField.addActionListener(myActionListener);
Select or Highlight the text in the field:
jTextField.selectAll();
Put the cursor back in the field (so the user can just start typing):
jTextField.requestFocus();
As I explained There are some more like this you can check it. Most of the images in this article are taken from the Head First Java book which is written by Kathy Sierra, Bert Bates. Don’t forget to read the book.
Reference: Head First Java, 2nd Edition [Book] (oreilly.com)









