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

Slides:



Advertisements
Similar presentations
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Advertisements

CPSC 233 Tutorial #17. GUI programming Objective – To understand better how to create simple interactive GUI programs.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.
20-Jun-15 GUI building with Swing. 2 How to build a GUI Create a window in which to display things—usually a JFrame (for an application), or a JApplet.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Graphical User Interfaces
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
23-Aug-15 GUI building with Swing. 2 How to build a GUI Create a window in which to display things—usually a JFrame (for an application), or a JApplet.
CIT 590 Swing/GUI in Java. How to create a GUI Create a window in which to display things—usually a JFrame (for an application), Use the setLayout(LayoutManager.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Interfaces & Polymorphism part 2:
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS Lecture 01 Frames and Components and events Lynda Thomas
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
Computer Science 209 GUIs Model/View/Controller Layouts.
Ch15 簡單計算機 物件導向系統實務. Ch22_Main.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch22_Main { public static void main(String.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
회원가입 - GUI ** 오지*.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Modular Event Handling
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
GUI building with Swing
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Course Outcomes of Advanced Java Programming AJP (17625, C603)
GUI building with Swing
GUI building with Swing
A Quick Java Swing Tutorial
Advanced Programming in Java
어서와 Java는 처음이지! 제13장 실전프로젝트.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

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); }

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?

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

Test

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

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 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); }

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