Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.

Similar presentations


Presentation on theme: "GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information."— Presentation transcript:

1 GUI Tutorial Day 3

2 Custom Dialog Create, display, hide, retrieve information

3 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

4 Now create a custom dialog – extend JDialog public class MyDialog extends JDialog { private JTextField myName; private JPasswordField password; public MyDialog() { setTitle("Login Dialog"); setSize(300, 200); setLayout(new GridLayout(2, 2)); JLabel nameLabel = new JLabel("Name"); myName = new JTextField(20); JLabel pwdLabel = new JLabel("Password"); password = new JPasswordField(); add(nameLabel); add(myName); add(pwdLabel); add(password); } … continued next slide Notice the use of inheritance JDialog has many of the same methods as JPanel Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column. Try: add a JButton while grid is set to 2x2 – what happen? Try: remove the layout manager – what happens?

5 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) { dialog = new MyDialog(); dialog.setVisible(true); } else { if (dialog != null) { String name = dialog.getMyName(); JOptionPane.showMessageDialog(null, "Hello " + name); } }}}  Remember to add the button listener to both buttons. Note use of getSource to check which button Actions for button2, first ensure dialog has been created

6 Test

7 May want an OK button public MyDialog() { … JButton button = new JButton("OK"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); add(button); Syntax Hint: button.addActionListener(new ButtonListener()); What about the grid layout? anonymous listener; class definition notice ); at end of statement

8 Need to return the name public String getMyName() { return myName.getText(); } What happens if you don’t have this method but press Hello?

9 Complete program for reference – dialog JFrame import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; public class ShowDialogGUI extends JFrame { private MyDialog dialog; private JButton button1, button2; public ShowDialogGUI() { setTitle("Show Dialog"); setSize(200, 100); button1 = new JButton("Login"); button2 = new JButton("Say Hello"); button1.addActionListener(new ButtonListener()); button2.addActionListener(new ButtonListener()); add(button1, BorderLayout.CENTER); add(button2, BorderLayout.SOUTH); } class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { dialog = new MyDialog(); dialog.setVisible(true); } else { if (dialog != null) { String name = dialog.getName(); JOptionPane.showMessageDialog(null, "Hello " + name); } public static void main(String[] args) { ShowDialogGUI gui = new ShowDialogGUI(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE); gui.setVisible(true); }

10 Complete program for reference – custom dialog import javax.swing.*; import java.awt.*; public class MyDialog extends JDialog { private JTextField name; private JPasswordField password; public MyDialog() { setTitle("Login Dialog"); setSize(300, 200); setLayout(new GridLayout(2, 2)); JLabel nameLabel = new JLabel("Name"); name = new JTextField(20); JLabel pwdLabel = new JLabel("Password"); password = new JPasswordField(); add(nameLabel); add(name); add(pwdLabel); add(password); JButton button = new JButton("OK"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); add(button); } } // end of MyDialog ctor public String getName() { return name.getText(); } } // end of class


Download ppt "GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information."

Similar presentations


Ads by Google