GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Graphical User Interfaces
Graphical User Interfaces Java’s AWT and Swing APIs.
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and event-driven programming An introduction.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Java Programming Chapter 10 Graphical User Interfaces.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Computer Science 209 GUIs Model/View/Controller Layouts.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©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
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Welcome To java
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
A Quick Java Swing Tutorial
Advanced Programming in Java
Graphical User Interface
Presentation transcript:

GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces

GUI Clients 2 Enterprise Applications CE00465-M Java’s Abstract Windows Toolkit Java provides classes for graphical components in the package java.awt –abstract windows toolkit the sub-package java.awt.event has classes for handling Graphical User Interface (GUI) events need to import both packages to use them import java.awt.*; import java.awt.event.*; java.awt is the original Java GUI API

GUI Clients 3 Enterprise Applications CE00465-M Java Swing AWT had some problems –It supported everything you could do in an HTML form, free standing frames, menus, and other objects, but more complex GUI features were more difficult –It was heavyweight: There were some portability problems because it relied heavily on the runtime platform’s native user interface components and it wasn’t always possible to hide differences in the way these components behaved Swing was introduced to address some of these problems –based on awt, but Swing has more components (e.g. selectable list) –contained in package javax.swing –written entirely in Java (i.e. lightweight) awt could only incorporate lowest common denominator capabilities present on every platform Swing 's capabilities are platform-independent –Flexible; you can change the look and feel

GUI Clients 4 Enterprise Applications CE00465-M JFrame a JFrame is a window with a title bar can add the other components to it –JMenuBar, JButton s, JTextField s……. make the client application class a subclass of JFrame –inherit JFrame 's attributes and methods –add our own method to set up and add the GUI components call it from the constructor –add other methods to interact with rest of the system

GUI Clients 5 Enterprise Applications CE00465-M Simple Example of a JFrame import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyClient extends JFrame { public MyClient() { super( "SavingsAccountRemote EJB Client" ); createGUI(); setDefaultCloseOperation( EXIT_ON_CLOSE ); setSize( 600, 300 ); setVisible( true ); } private void createGUI() { } public static void main( String[] args ) { MyClient myMyClient= new MyClient(); }

GUI Clients 6 Enterprise Applications CE00465-M Simple Example of a JFrame Doesn’t do anything yet

GUI Clients 7 Enterprise Applications CE00465-M Layout Managers The position of graphical components is controlled by a Layout Manager The default layout manager for a JFrame object is BorderLayout Alternative layout managers –FlowLayout –GridLayout –GridBagLayout

GUI Clients 8 Enterprise Applications CE00465-M BorderLayout components can be added to one of five regions only one component per region –to add more, define a JPanel object, add the components to the JPanel, then add the JPanel to the region North WestCenterEast South

GUI Clients 9 Enterprise Applications CE00465-M BorderLayout Example Note you need to get a Container object ( contentPane ) before adding component objects to it We’ve also introduced JLabel and JButton objects import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyClient extends JFrame { private JButton okButton; private JLabel helloLabel; public MyClient() { super( "Border Layout Example" ); createGUI(); setDefaultCloseOperation( EXIT_ON_CLOSE ); setSize( 220, 150 ); setVisible( true ); } private void createGUI() { okButton = new JButton("OK"); helloLabel = new JLabel("Hello"); setLayout(new BorderLayout()); Container contentPane = getContentPane(); contentPane.add( helloLabel,BorderLayout.CENTER ); contentPane.add( okButton,BorderLayout.SOUTH ); } public static void main( String[] args ) { MyClient myMyClient= new MyClient(); }

GUI Clients 10 Enterprise Applications CE00465-M Using Components Declare components as attributes Create components in constructor Add listeners to active components Add components to the frame Write actionPerformed() method (see later) for active components Write any methods called by these methods

GUI Clients 11 Enterprise Applications CE00465-M JButtons JButtons can be added to any subclass of JContainer Need to declare JButtons as attributes – private JButton addButton,saveButton,deleteButton,findButton; In the constructor (or a method called by it) –create the buttons –register them with an action listener (see later) –add them to a JPanel object –add the panel to the JFrame object content pane

GUI Clients 12 Enterprise Applications CE00465-M BorderLayout Example It’s better to add the components to a JPanel object and then add the JPanel object to the container So far the OK Button doesn’t do anything import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyClient extends JFrame { private JButton okButton; private JLabel helloLabel; private JPanel p1,p2; public MyClient() { super( "Border Layout Example" ); createGUI(); setDefaultCloseOperation( EXIT_ON_CLOSE ); setSize( 220, 150 ); setVisible( true ); } private void createGUI() { okButton = new JButton("OK"); helloLabel = new JLabel("Hello"); p1= new JPanel(); p2 = new JPanel(); p1.add(okButton); p2.add(helloLabel); setLayout(new BorderLayout()); Container contentPane = getContentPane(); contentPane.add( p2,BorderLayout.CENTER ); contentPane.add( p1,BorderLayout.SOUTH ); } public static void main( String[] args ) { MyClient myMyClient= new MyClient(); }

GUI Clients 13 Enterprise Applications CE00465-M FlowLayout Displays the components from left to right in the order they are added Centres the line of components Wraps to next line when first line is full The example below shows the effect of resizing the window with the JPanel p3 set to FlowLayout p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); p1.add(okButton); p2.add(helloLabel); p3.setLayout(new FlowLayout()); p3.add(p2); p3.add(p1); Container contentPane = getContentPane(); contentPane.add(p3);

GUI Clients 14 Enterprise Applications CE00465-M GridLayout Places components in rows and columns Fills up left to right by row setLayout(new GridLayout(2,3));

GUI Clients 15 Enterprise Applications CE00465-M GridBagLayout Allows precise placement of components on a grid of cells Components can occupy more than one cell Component can occupy entire cell, or be padded with white spaces Flexible but complicated!!

GUI Clients 16 Enterprise Applications CE00465-M Events in Java When a user interacts with a graphical system, an event is generated –mouse click on interface component –key press Events can also be generated externally –display needs to be repainted java.awt.event.* and javax.swing.event.* contain classes to deal with events

GUI Clients 17 Enterprise Applications CE00465-M Events in Java Every time an event occurs, a new object of the appropriate class is generated –WindowEvent window opening and closing, window activation –ActionEvent component actions such as button presses, menu and checkbox selection –AdjustmentEvent moving of scrollbars –KeyEvent key presses –MouseEvent mouse clicks, moves, drags

GUI Clients 18 Enterprise Applications CE00465-M EventListener We need to register an event listener for each active component When a component generates an event object, it is sent to the registered listener Two ways to program: –The class must implement the appropriate listener interface WindowListener ActionListener AdjustmentListener KeyListener MouseListener –Or we can use anonymous inner classes

GUI Clients 19 Enterprise Applications CE00465-M Example implementing the Action Listener Interface Note you have one actionPerformed() method and must distinguish between the different action events in the actionPerformed() method body public class MyClient extends JFrame implements ActionListener {………… …………… private void createGUI() { okButton = new JButton("OK"); p1= new JPanel(); p1.add(okButton); okButton.addActionListener(this); Container contentPane = getContentPane(); contentPane.add( p1); } public void actionPerformed(ActionEvent e) { message(); } public void message() { JOptionPane.showMessageDialog(this,"OK Button hit"); }

GUI Clients 20 Enterprise Applications CE00465-M Example using an anonymous inner class Note you have an actionPerformed() method for every ActionListener public class MyClient extends JFrame {………… …………… private void createGUI() { okButton = new JButton("OK"); p1= new JPanel(); p1.add(okButton); okButton.addActionListener( new ActionListener(){ //anonymous inner class public void actionPerformed( ActionEvent event ) { message(); } }); Container contentPane = getContentPane(); contentPane.add( p1); } public void message() { JOptionPane.showMessageDialog(this,"OK Button hit"); }

GUI Clients 21 Enterprise Applications CE00465-M Dialog Boxes A simple way to –get input from the user –confirm actions –output messages JOptionPane class has several standard dialog boxes –call static methods to show

GUI Clients 22 Enterprise Applications CE00465-M Dialog Boxes- JOptionPane.showInputDialog() used for simple input of one data item String parameter: the prompt –can also specify title and icon as parameters if the user clicks OK button, the input is returned as a String if the user clicks Cancel button, returns null String primaryKeyString = JOptionPane.showInputDialog(this, "Please enter a Record id");

GUI Clients 23 Enterprise Applications CE00465-M Dialog Boxes- JOptionPane.showInputDialog() To input integers or doubles –convert the returned String using a parse method –use Double.parseDouble() for doubles –Use Integer.parseInt() for Integers int id = Integer.parseInt( JOptionPane.showInputDialog(this, "Please enter a Record id" ));

GUI Clients 24 Enterprise Applications CE00465-M Dialog Boxes- JOptionPane.showMessageDialog() Used to output a message or warning Parameters –parent frame (usually this ) –message to output JOptionPane.showMessageDialog(this,“Add Record Button hit");

GUI Clients 25 Enterprise Applications CE00465-M Dialog Boxes- JOptionPane.showMessageDialog() Can have additional parameters –title String to display at top of dialog –message type Integer code –Icon image to display each message type has a default icon JOptionPane.showMessageDialog(this, "Item " + itemID + " not found", "Not found", JOptionPane.WARNING_MESSAGE);

GUI Clients 26 Enterprise Applications CE00465-M Message types ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE PLAIN_MESSAGE

GUI Clients 27 Enterprise Applications CE00465-M Dialog Boxes- JOptionPane.showConfirmDialog() Confirm whether an action should proceed Can choose which buttons to display –YES_NO_OPTION –YES_NO_CANCEL_OPTION –OK_CANCEL_OPTION. Returns an integer corresponding to option selected –YES_OPTION, NO_OPTION, CANCEL_OPTION, OK_OPTION, CLOSED_OPTION int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete?", "Confirm order delete", JOptionPane.YES_NO_OPTION);

GUI Clients 28 Enterprise Applications CE00465-M Menus Menus can be added to any subclass of JFrame Need to declare the active components as attributes –private JMenuRecord mitmAddRecord, mitmSaveRecord, mitmDeleteRecord, mitmFindRecord, mitmExit; In the constructor (or a method called by it) –create the menu components –add an action listener to them –add them to the JFrame object

GUI Clients 29 Enterprise Applications CE00465-M Menus public void addMenu() { // create a menu bar and put it at the top of the frame JMenuBar mBar = new JMenuBar(); setJMenuBar(mBar); // create and attach a pull-down menu to deal with items JMenu mnuItem = new JMenu("Record"); mBar.add(mnuItem); // attach menu items to menu mnuItem.add(mitmAddRecord); mnuItem.add(mitmSaveRecord); mnuItem.add(mitmDeleteRecord); mnuItem.add(mitmFindRecord); mnuItem.addSeparator(); mnuItem.add(mitmExit );

GUI Clients 30 Enterprise Applications CE00465-M Menus // adding action listeners using anonymous inner classes mitmAddRecord.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent event ) { addSavingsAccount(); }}); mitmSaveRecord.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent event ) { updateSavingsAccount(); }}); mitmDeleteRecord.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent event ) { deleteSavingsAccount(); }}); mitmFindRecord.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent event ) { getSavingsAccount(); }}); mitmExit.addActionListener(new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); }});