1 / 10

Creating and Using Custom Dialogs in Java Swing GUI Applications

In this tutorial, you'll learn how to create and manage custom dialogs in Java Swing. We will extend the JDialog class to create a login dialog that collects user input and displays a personalized greeting. Starting with a JFrame, you'll add buttons to trigger the dialog and handle user interactions using ActionListener. You'll also explore layouts, including GridLayout, and understand the importance of inheritance in Swing components. By the end of this guide, you will have a fully functional GUI application with custom dialog capabilities.

mliss
Télécharger la présentation

Creating and Using Custom Dialogs in Java Swing GUI Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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); … 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() { . . . 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); } }}} • Create an instance variable of type MyDialog • 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 getName() { return name.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_CLOSE); 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

More Related