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

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

Embed Size (px)

Citation preview

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

Enterprise Applications CE00465-MGUI Clients 1

Clients with Graphical User Interfaces

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

Enterprise Applications CE00465-MGUI Clients 2

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 themimport java.awt.*;

import java.awt.event.*;

• java.awt is the original Java GUI API

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

Enterprise Applications CE00465-MGUI Clients 3

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

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

Enterprise Applications CE00465-MGUI Clients 4

JFrame

• a JFrame is a window with a title bar

• can add the other components to it– JMenuBar, JButtons, JTextFields…….

• 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

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

Enterprise Applications CE00465-MGUI Clients 5

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

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

Enterprise Applications CE00465-MGUI Clients 6

Simple Example of a JFrame

• Doesn’t do anything yet

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

Enterprise Applications CE00465-MGUI Clients 7

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

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

Enterprise Applications CE00465-MGUI Clients 8

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

West Center East

South

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

Enterprise Applications CE00465-MGUI Clients 9

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

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

Enterprise Applications CE00465-MGUI Clients 10

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

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

Enterprise Applications CE00465-MGUI Clients 11

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

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

Enterprise Applications CE00465-MGUI Clients 12

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

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

Enterprise Applications CE00465-MGUI Clients 13

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

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

Enterprise Applications CE00465-MGUI Clients 14

GridLayout

• Places components in rows and columns

• Fills up left to right by rowsetLayout(new GridLayout(2,3));

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

Enterprise Applications CE00465-MGUI Clients 15

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!!

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

Enterprise Applications CE00465-MGUI Clients 16

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

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

Enterprise Applications CE00465-MGUI Clients 17

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

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

Enterprise Applications CE00465-MGUI Clients 18

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

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

Enterprise Applications CE00465-MGUI Clients 19

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

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

Enterprise Applications CE00465-MGUI Clients 20

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

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

Enterprise Applications CE00465-MGUI Clients 21

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

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

Enterprise Applications CE00465-MGUI Clients 22

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

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

Enterprise Applications CE00465-MGUI Clients 23

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

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

Enterprise Applications CE00465-MGUI Clients 24

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

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

Enterprise Applications CE00465-MGUI Clients 25

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

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

Enterprise Applications CE00465-MGUI Clients 26

Message types

• ERROR_MESSAGE

• INFORMATION_MESSAGE

• WARNING_MESSAGE

• QUESTION_MESSAGE

• PLAIN_MESSAGE

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

Enterprise Applications CE00465-MGUI Clients 27

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_OPTIONint confirm = JOptionPane.showConfirmDialog(this,

"Are you sure you want to delete?", "Confirm order delete", JOptionPane.YES_NO_OPTION);

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

Enterprise Applications CE00465-MGUI Clients 28

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

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

Enterprise Applications CE00465-MGUI Clients 29

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

------- ------- -------

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

Enterprise Applications CE00465-MGUI Clients 30

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