Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces."— Presentation transcript:

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

2 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

3 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

4 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

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

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

7 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

8 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

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

10 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

11 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

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

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

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

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

16 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

17 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

18 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

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

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

21 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

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

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

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

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

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

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

28 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

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

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


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

Similar presentations


Ads by Google