L4 GUI Components - 1
L4 GUI Components - 1
But except for the simplest programs, flow layout manager is not very
appealing. Another layout manager is called the border layout manager. This
layout manager lets you control choose where you want to place each
component: center, north, south, east or west. For example:
class ButtonPanel extends JPanel{
public ButtonPanel(){
yellowButton=new JButton("Yellow");
blueButton=new JButton("Blue");
1- The GUI itself. This is a front end for the program. It is what the user sees,
containing buttons and other interfaces for the user to provide input, and
various displays for the output.
In Java two components are used to get text input: text fields and text areas.
Text fields take one line of text only while text areas can take multiple lines of
text. JTextField for text fields and JTextArea for text areas are available in
Java. These two classes have a parent class called JTextComponent. Many
methods are inherited from this class.
TextFields
You can create a new TextField object like this:
JTextField t=new JTextField(20); //20 columns
Or
JTextField t=new JTextField(“Enter text”, 20);//initial value
JLabel class defines labels which can be used to write textual information
inside components. The next example will use JButtons, JTextFields and
JLabels to create a small application that can add two numbers:
addB=new JButton("Add");
add(addB, "South");
textField1=new JTextField(15);
textField2=new JTextField(15);
textResult=new JTextField(18);
add(panel, "Center");
ButtonHandler handler=new ButtonHandler();
addB.addActionListener(handler);
setSize(300,200);
}
The ButtonHandler which acts as the listener for our button component defines
the actionPerformed method; this method obtains the text fields’ strings and
converts them to integer and then adds the two numbers.
This simple program can add integer numbers only; it cannot even handle
doubles. What’s more, there is no text validation which will cause serious
problems for your program. To make your program robust, you must consider
input validation.
b- the buttons and labels are arranged more appropriately. Use layout
managements you have seen to achieve this.
c- Also make sure that the Add button at the bottom looks like a button
and does not fill the whole bottom section of the frame.
d- add another button to the frame next to the “Add” button so that when
it is clicked the program is gracefully ended.
e- add yet another button, again next to the previous two, called “Clear”
which will clear the 3 text fields.
This will create a password field that is 15 columns long (you can store more
than 15 characters inside it). The first parameter is of type String and if
supplied, will set the field to a default password.
The following method returns the text contained in the password field:
char[] getPassword(); //not a string?
This will create a text area 6 lines of 60 columns each. An example follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TextAreaFrame extends JFrame implements ActionListener
{ public TextAreaFrame()
{ JPanel p = new JPanel();
insertButton = new JButton("Insert");
p.add(insertButton);
insertButton.addActionListener(this);
add(p, "South");
setTitle("TextAreaTest");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Text area components do not have scroll bars. To be able to use scrollable text
you must first create a JScrollPane component and insert the text area object
inside it:
JScrollPane scrollPane;
scrollPane = new JScrollPane(textArea);
Scroll bars automatically appear if there is more text than the textarea can
display.
setLineWrap(true) sets the text area scrollable; setting it to false disables
line wrapping.
Text area components display plain text only; for formatted text you need to
use JEditorPane and JTextPane classes.