1/3/2016<br />
<br />
04. Graphic User Interface in Java<br />
<br />
Faculty of Information Technologies<br />
Industrial University of Ho Chi Minh City<br />
1<br />
<br />
GUI components (p3)<br />
Text component<br />
Choice component<br />
Menu<br />
Mnemonic<br />
Toolbar<br />
<br />
Tooltip<br />
Tabbed pane<br />
Scroll pane<br />
Dialog box<br />
2<br />
<br />
Text component<br />
<br />
3<br />
<br />
1<br />
<br />
1/3/2016<br />
<br />
Text component<br />
<br />
4<br />
<br />
Text component<br />
Group<br />
<br />
Description<br />
<br />
Text<br />
Also known simply as text fields,<br />
Controls text controls can display only one<br />
line of editable text. Like buttons,<br />
they generate action events.<br />
<br />
Swing Classes<br />
JTextField and its<br />
subclasses<br />
JPasswordField and<br />
JFormattedTextField<br />
<br />
Plain<br />
Text<br />
Areas<br />
<br />
JTextArea can display multiple lines JTextArea<br />
of editable text. Although a text<br />
area can display text in any font, all<br />
of the text is in the same font.<br />
<br />
Styled<br />
Text<br />
Areas<br />
<br />
A styled text component can display JEditorPane<br />
editable text using more than one and its subclass<br />
font. Some styled text components JTextPane<br />
allow embedded images and even<br />
embedded components.<br />
5<br />
<br />
JTextField<br />
<br />
•<br />
<br />
If the cursor is in the text field, the user presses the<br />
Enter key, JTextField generates an Action event<br />
o<br />
<br />
Listener?<br />
<br />
o<br />
<br />
Method in the listener?<br />
<br />
o<br />
<br />
Register listener to the text field?<br />
<br />
• … implements ActionListener<br />
<br />
• public void actionPerformed(ActionEvent e)<br />
• void addActionListener(ActionListener listener)<br />
<br />
6<br />
<br />
2<br />
<br />
1/3/2016<br />
<br />
JTextField Demo<br />
public class JTextFieldDemo extends JFrame<br />
implements ActionListener<br />
{<br />
JTextField mmText;<br />
JLabel resultLabel;<br />
public JTextFieldDemo() {<br />
super("Chuyen doi don vi");<br />
setLayout(new GridLayout(2,2));<br />
<br />
public void actionPerformed(ActionEvent e)<br />
{<br />
double cm, mm;<br />
mm =<br />
Double.parseDouble(mmText.getText());<br />
cm = mm/10;<br />
resultLabel.setText<br />
(Double.toString(cm));<br />
}<br />
<br />
add (new JLabel (“Nhap vao so millimet:"));<br />
add (mmText = new JTextField (10));<br />
add (new JLabel (“So centimet tuong ung:"));<br />
add (resultLabel = new JLabel ("---"));<br />
mmText.addActionListener (this);<br />
<br />
setDefaultCloseOperation(EXIT_ON_CLOSE);<br />
<br />
setSize(300,90);<br />
<br />
}<br />
<br />
}<br />
<br />
public static void main(String[] args) {<br />
new JTextFieldDemo().setVisible(true);<br />
}<br />
<br />
7<br />
<br />
JPasswordField<br />
<br />
•<br />
•<br />
<br />
Purpose: used to enter a password<br />
A JPasswordField is similar to a JTextField except the<br />
characters typed in by the user are not echoed back to<br />
the user<br />
o<br />
<br />
Instead, an alternate character such as asterisks (*) is<br />
displayed<br />
<br />
o<br />
<br />
You can set the echo character using the method:<br />
• public void setEchoChar(char c)<br />
<br />
8<br />
<br />
JPasswordField Demo<br />
pnlRight = new JPanel(new GridLayout(0,1));<br />
pnlRight.add(btnOk = new JButton("OK"));<br />
pnlRight.add(btnCancel=new JButton("Cancel"));<br />
<br />
public class JPasswordFieldDemo extends JFrame<br />
implements ActionListener<br />
{<br />
JPasswordField txtPassword;<br />
JButton btnOk, btnCancel;<br />
<br />
add(pnlLeft, BorderLayout.WEST);<br />
add(pnlRight, BorderLayout.CENTER);<br />
<br />
public JPasswordFieldDemo()<br />
{<br />
super("JPasswordField Demo");<br />
JPanel pnlLeft, pnlRight;<br />
txtPassword = new JPasswordField(12);<br />
txtPassword.addActionListener(this);<br />
<br />
btnOk.addActionListener(this);<br />
btnCancel.addActionListener(this);<br />
setDefaultCloseOperation(EXIT_ON_CLOSE);<br />
setsize(200, 200);<br />
}<br />
<br />
pnlLeft = new JPanel();<br />
pnlLeft.add(new JLabel("Password: "));<br />
pnlLeft.add(txtPassword);<br />
<br />
9<br />
<br />
3<br />
<br />
1/3/2016<br />
<br />
JPasswordField Demo (contd.)<br />
public void actionPerformed(ActionEvent e)<br />
{<br />
Object o = e.getSource();<br />
if (o == btnOk || o == txtPassword)<br />
{<br />
char chPassword[] = txtPassword.getPassword();<br />
String strPassword = new String(chPassword);<br />
if(strPassword.trim().equals("pass")) {<br />
JOptionPane.showMessageDialog(this,"Correct Password");<br />
System.exit(0);<br />
}<br />
else {<br />
JOptionPane.showMessageDialog(this,"Incorrect Password",<br />
"Error Message", JOptionPane.ERROR_MESSAGE);<br />
txtPassword.selectAll();<br />
txtPassword.requestFocus();<br />
}<br />
}<br />
else {<br />
System.exit(0);<br />
}<br />
}<br />
public static void main(String [] args){ new JPasswordFieldDemo().setVisible(true); }<br />
}<br />
<br />
10<br />
<br />
JTextArea<br />
<br />
•<br />
<br />
Purpose<br />
o<br />
<br />
•<br />
<br />
For texts with more than one line long<br />
<br />
Constructors<br />
o<br />
<br />
JTextArea(int rows, int cols)<br />
<br />
o<br />
<br />
JTextArea(String text, int rows, int cols)<br />
<br />
• constructs a new text area with number of rows and columns<br />
• constructs a new text area with an initial text<br />
<br />
11<br />
<br />
JTextArea Demo<br />
JPanel buttonPanel = new JPanel();<br />
buttonPanel.add(insertButton=new JButton("Insert"));<br />
buttonPanel.add(wrapButton = new JButton("Wrap"));<br />
add(buttonPanel, BorderLayout.SOUTH);<br />
textArea = new JTextArea(8, 40);<br />
add(textArea, BorderLayout.CENTER);<br />
<br />
12<br />
<br />
4<br />
<br />
1/3/2016<br />
<br />
Outline<br />
Text component<br />
Choice component<br />
Menu<br />
Mnemonic<br />
Toolbar<br />
<br />
Tooltip<br />
Tabbed pane<br />
Scroll pane<br />
Dialog box<br />
13<br />
<br />
JCheckBox<br />
<br />
•<br />
<br />
Purpose:<br />
o<br />
<br />
Used for multi-option user input that the user may select or<br />
deselect by clicking on them<br />
<br />
•<br />
<br />
Constructors:<br />
o<br />
<br />
JCheckBox()<br />
<br />
o<br />
<br />
JCheckBox(String text)<br />
<br />
• Creates an initially unchecked checkbox with no label<br />
• Creates a checkbox (initially unchecked) with the specified text;<br />
see setSelected for changing it<br />
o<br />
<br />
JCheckBox(String text, boolean selected)<br />
• Creates a checkbox with the specified text<br />
– The initial state is determined by the boolean value<br />
provided<br />
– A value of true means it is checked<br />
14<br />
<br />
JCheckBox – Methods<br />
<br />
•<br />
<br />
boolean isSelected()<br />
o<br />
<br />
•<br />
<br />
o<br />
<br />
•<br />
•<br />
<br />
sets the checkbox to a new state<br />
<br />
String getText()<br />
void setText(String text)<br />
o<br />
<br />
•<br />
<br />
returns the state of the checkbox<br />
<br />
void setSelected(boolean state)<br />
<br />
gets or sets the button’s text<br />
<br />
addItemListener<br />
o<br />
<br />
Add an ItemListener to process ItemEvent in itemStateChanged<br />
15<br />
<br />
5<br />
<br />