GUI Tutorial 3

Download Report

Transcript GUI Tutorial 3

GUI Tutorial Day 3
Custom Dialog
Create, display, hide, retrieve information
Start as usual – with JFrame
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class ShowDialogGUI extends JFrame {
private JButton button1, button2;
public ShowDialogGUI(){
setTitle("Show Dialog");
setSize(200, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("Login");
button2 = new JButton("Say hello");
// Experiment with different locations!
add(button1, BorderLayout.CENTER);
add(button2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
ShowDialogGUI gui = new ShowDialogGUI();
gui.setVisible(true);
}
}
http://leepoint.net/notes-java/GUI/layouts/20borderlayout.html
Now create a custom dialog – extend JDialog
public class MyDialog extends JDialog {
private JTextField myName;
Notice the use of inheritance
private JPasswordField password;
public MyDialog() {
setTitle("Login Dialog");
setSize(300, 200);
JDialog has many of the same methods as JPanel
setLayout(new GridLayout(2, 2));
JLabel nameLabel = new JLabel("Name");
Creates a grid layout with the specified
number of rows and columns. All
JLabel pwdLabel = new JLabel("Password"); components in the layout are given equal
size. One, but not both, of rows and cols
password = new JPasswordField();
can be zero, which means that any number
add(nameLabel);
of objects can be placed in a row or in a
add(myName);
column.
add(pwdLabel);
myName = new JTextField(20);
add(password);
}
}
… continued next slide
Try: add a JButton while grid is set to 2x2 – what happen?
Try: remove the layout manager – what happens?
Now get the dialog to display

Add an action listener to the button in the JFrame
public ShowDialogGUI() {
private MyDialog dialog; // need instance var for dialog
. . .
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {Note use of getSource to check which button
dialog = new MyDialog();
dialog.setVisible(true);
Actions for button2, first ensure dialog has been created
} else {
if (dialog != null) {
String name = dialog.getMyName();
JOptionPane.showMessageDialog(null, "Hello " + name);
}
}}}

Remember to add the button listener to both buttons.
Test
May want an OK button
public MyDialog()
{
…
JButton button = new JButton("OK");
anonymous listener; class definition
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
add(button);
notice ); at end of statement
Syntax Hint:
button.addActionListener(new ButtonListener());
What about the grid layout?
Need to return the name
public String getMyName()
{
return myName.getText();
}
What happens if you don’t have this method but press Hello?
Complete program for reference –
dialog JFrame
class ButtonListener implements ActionListener
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1) {
public class ShowDialogGUI extends JFrame {
dialog = new MyDialog();
private MyDialog dialog;
dialog.setVisible(true);
private JButton button1, button2;
}
public ShowDialogGUI()
else {
{
if (dialog != null)
setTitle("Show Dialog");
{
setSize(200, 100);
String name = dialog.getName();
button1 = new JButton("Login");
button2 = new JButton("Say Hello");
JOptionPane.showMessageDialog(null, "Hello "
+ name);
button1.addActionListener(new ButtonListener());
}
button2.addActionListener(new ButtonListener());
}
add(button1, BorderLayout.CENTER);
}
add(button2, BorderLayout.SOUTH);
}
}
public static void main(String[] args) {
ShowDialogGUI gui = new ShowDialogGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
gui.setVisible(true);
}
}
Complete program for reference –
custom dialog
JButton button = new JButton("OK");
import javax.swing.*;
button.addActionListener(new ActionListener() {
import java.awt.*;
public void actionPerformed(ActionEvent e)
{
public class MyDialog extends JDialog {
setVisible(false);
private JTextField name;
}
private JPasswordField password;
});
public MyDialog()
add(button);
{
}
setTitle("Login Dialog");
setSize(300, 200);
} // end of MyDialog ctor
setLayout(new GridLayout(2, 2));
JLabel nameLabel = new JLabel("Name");
name = new JTextField(20);
public String getName()
{
return name.getText();
JLabel pwdLabel = new JLabel("Password");
password = new JPasswordField();
}
add(nameLabel);
add(name);
add(pwdLabel);
add(password);
} // end of class