COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo Program l Layout Managers l Buttons and Action Listeners l Container.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Chapter 4 l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 Department of Computer Science Purdue University.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
©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.
CS102--Object Oriented Programming Lecture 18: – The Swing Package Copyright © 2008 Xiaoyan Li.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
Java Programming Chapter 10 Graphical User Interfaces.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 12- GUI’s, Java, and Swing.. Overview n What are GUI’s n How Java does GUI’s- Swing n Buttons n Containers n Text I/O and Swing n Review.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Comp 249 Programming Methodology Chapter 17 Swing I Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Chapter 141 More Swing Chapter Objectives learn to add menus, icons, borders, and scroll bars to GUIs learn to use the BoxLayout manager and the.
Chapter 14- More Swing, Better looking applications.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
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 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
Chapter 14Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 14 l Menus l Making GUIs Pretty (and More Functional) l Box.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Swing.
Graphical User Interface (pronounced "gooey")
Graphical User Interface (GUI) Programming III
A Quick Java Swing Tutorial
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
Window Interfaces Using Swing Objects Chapter 12
More Swing Chapter 14 Menus Making GUIs Pretty (and More Functional)
A Quick Java Swing Tutorial
Window Interfaces Using Swing Objects Chapter 12
Presentation transcript:

COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Menus l Making GUIs Pretty (and More Functional) l Box Containers and Box Layout Managers l More on Events and Listeners l Another Look at the Swing Class Hierarchy More Swing

COMP 150: Introduction to Object-Oriented Programming 2 Lecturer: Dr. AJ Bieszczad Menus l Three Swing classes used to put a menu in a program: »JMenuBar »JMenu »JMenuItem l Menu items behave in the same way as buttons AbstractButton JButton JMenu JMenuItem

COMP 150: Introduction to Object-Oriented Programming 3 Lecturer: Dr. AJ Bieszczad A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m);... JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Create a menu Create a menu item A menu item uses an action listener the same way a button does.

COMP 150: Introduction to Object-Oriented Programming 4 Lecturer: Dr. AJ Bieszczad A GUI with a Menu JMenu memoMenu = new JMenu("Memos"); JMenuItem m; m = new JMenuItem("Save Memo 1"); m.addActionListener(this); memoMenu.add(m); m = new JMenuItem("Save Memo 2"); m.addActionListener(this); memoMenu.add(m);... JMenuBar mBar = new JMenuBar(); mBar.add(memoMenu); setJMenuBar(mBar); Each menu item is added to the menu. The menu is added to the menu bar. One way to add a menu bar to a JFrame

COMP 150: Introduction to Object-Oriented Programming 5 Lecturer: Dr. AJ Bieszczad Nested Menus JMenu is a descendant of JMenuItem Every JMenu object is also a JMenuItem A JMenu can be a menu item in another menu l This allows nested menus l Clicking on a nested menu shows the items in the nested menu and allows them to be selected. AbstractButton JButton JMenu JMenuItem

COMP 150: Introduction to Object-Oriented Programming 6 Lecturer: Dr. AJ Bieszczad Making GUIs Pretty (and More Functional) l Adding Icons The JScrollPane Class for Scroll Bars l Adding Borders l Changing the Look and Feel

COMP 150: Introduction to Object-Oriented Programming 7 Lecturer: Dr. AJ Bieszczad Using Icons l Icons are (small) pictures l Icons may be added to labels, buttons, and menu items. The ImageIcon class can be used to convert a picture to an icon: ImageIcon SmileyFaceIcon = new ImageIcon(“smiley.gif”); The setIcon method can be used to add an icon to a component: JLabel helloLabel = new JLabel(“Hello”); ImageIcon dukeWavingIcon = new ImageIcon(“duke_waving.gif”); helloLabel.setIcon(dukeWavingIcon);

COMP 150: Introduction to Object-Oriented Programming 8 Lecturer: Dr. AJ Bieszczad The JScrollPane Class for Scroll Bars l A view port is used when not all information can be displayed on screen at once. l Scroll bars move a view port around to show different parts of the information. JScrollPane is a class that can provide a view port with scroll bars. An example using JScrollPane with a JTextArea called theText and a JPanel called textPanel : JScrollPane scrolledText = new JScrollPane(theText); textPanel.add(scrolledText);

COMP 150: Introduction to Object-Oriented Programming 9 Lecturer: Dr. AJ Bieszczad Adding Borders l A border is an area that frames a component. l Swing provides several different types of borders: »BevelBorder —makes component look raised or lowered »EtchedBorder —similar to BevelBorder but can’t set size »EmptyBorder —extra space around the component »LineBorder —colored border of a given thickness »MatteBorder —similar to LineBorder but can adjust thickness on each side of the component l An example of adding a bevel border to a button: testButton.setBorder(new BevelBorder(BevelBorder.LOWERED));

COMP 150: Introduction to Object-Oriented Programming 10 Lecturer: Dr. AJ Bieszczad Box Layout Manager l Useful for a single column or single row of components Specify X_AXIS (horizontal) or Y_AXIS (vertical) layout as second parameter to constructor for layout manager l Provides a means of separating components in a row or column »Strut—allocates a fixed amount of space between two components »Glue—allocates a variable amount of space between two components A Box container is a container that is automatically given a BoxLayout manager.

COMP 150: Introduction to Object-Oriented Programming 11 Lecturer: Dr. AJ Bieszczad Box Layout Versus Other Layouts l Horizontal box layout is similar to flow layout. l Vertical box layout is similar to grid layout with only one column. l Big advantage of box layout is control over spacing using struts and glue. l Note that it is possible to use struts and glue with other layout managers but they will probably not work as intended.

COMP 150: Introduction to Object-Oriented Programming 12 Lecturer: Dr. AJ Bieszczad Box Layout Demo Program JPanel horizontalPanel = new JPanel(); horizontalPanel.setLayout( new BoxLayout(horizontalPanel, BoxLayout.X_AXIS)); Component horizontalStrut = Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE); horizontalPanel.add(horizontalStrut); JButton hStopButton = new JButton("Red"); hStopButton.addActionListener(this); horizontalPanel.add(hStopButton); Static method in Box class used to create a strut of a particular size for spacing Specifies a horizontal layout

COMP 150: Introduction to Object-Oriented Programming 13 Lecturer: Dr. AJ Bieszczad Struts and Glue l invisible components used to add space between visible components l horizontal strut: »programmer specifies width, which layout manager does not change l vertical strut: »programmer specifies height, which layout manager does not change l glue: »no specified size »can be added to layout to specify where extra space should go when container grows

COMP 150: Introduction to Object-Oriented Programming 14 Lecturer: Dr. AJ Bieszczad Setting the Spacing Between Components To control spacing with layouts other than the Box layout, use setHgap and setVgap : public void setHgap(int hgap) »sets the horizontal gap between components »argument is size of gap in pixels public void setVgap(int vgap) »sets the vertical gap between components »argument is size of gap in pixels Can also use EmptyBorder in any layout manager which will add space as part of a component

COMP 150: Introduction to Object-Oriented Programming 15 Lecturer: Dr. AJ Bieszczad The Box Container Class A Box object works like a panel with a BoxLayout manager. l Created using static methods: Box horizontalBox = Box.createHorizontalBox(); Box verticalBox = Box.createVerticalBox(); Automatically given a BoxLayout manager when created »You should not use setLayout method with a box object.

COMP 150: Introduction to Object-Oriented Programming 16 Lecturer: Dr. AJ Bieszczad The CardLayout Manager l Allows a set of views (components) to choose among »Only one view is visible at a time. »Can go through views in order or jump to any view. »Often the components added to a CardLayout will be panels. Each component added to a CardLayout has a string associated with it that works like a name: deckPanel.add("start", startCardPanel); »The string can be used later to display that component (or view): dealer.show(deckPanel, "start"); l Need reference to layout manager to change views, so do not use anonymous object: deckPanel.add(new CardLayout()); legal but useless

COMP 150: Introduction to Object-Oriented Programming 17 Lecturer: Dr. AJ Bieszczad CardLayoutDemo deckPanel = new JPanel(); dealer = new CardLayout(); deckPanel.setLayout(dealer);... deckPanel.add("start", startCardPanel);... deckPanel.add("green", greenCardPanel);... deckPanel.add("red", redCardPanel);... dealer.show(deckPanel, "red");... dealer.next(deckPanel); if redCardPanel is currently displayed, will show startCardPanel Only one of these three panels will be visible at a time. will show redCardPanel

COMP 150: Introduction to Object-Oriented Programming 18 Lecturer: Dr. AJ Bieszczad Inner Classes An inner class is a class defined within another class. Advantages: l They make the outer class more self contained. »If WindowDestroyer is used, must make sure that class is available. »If InnerDestroyer (inner class version of WindowDestroyer ) is used, it will always be available. l Inner class has access to all instance variables and methods of outer class, including private ones. l Avoid name conflicts. »You could have another (outer) class called InnerDestroyer and there would not be a conflict.

COMP 150: Introduction to Object-Oriented Programming 19 Lecturer: Dr. AJ Bieszczad The WindowListener Interface For a class to be a listener for window events, it must implement the WindowListener interface. By implementing the WindowListener interface, a window can be its own listener. l The advantage of making a window its own listener is that it is easy to call methods from the listener since they are in the same object.

COMP 150: Introduction to Object-Oriented Programming 20 Lecturer: Dr. AJ Bieszczad The WindowListener Interface Implementation of the WindowListener interface requires these seven methods to be defined: »public void windowOpened(WindowEvent e) »public void windowClosing(WindowEvent e) »public void windowClosed(WindowEvent e) »public void windowIconified(WindowEvent e) »public void windowDeiconified(WindowEvent e) »public void windowActivated(WindowEvent e) »public void windowDeactivated(WindowEvent e) l If a method will be not be used, it should be defined with an empty body WindowAdapter is a class that implements all seven methods of the WindowListener with empty bodies.

COMP 150: Introduction to Object-Oriented Programming 21 Lecturer: Dr. AJ Bieszczad Programming the Close-Window Button The WindowListener interface can be used to program the close-window button. l If the close-window button is not programmed, by default it will close the window but not exit the program. l For a window that does not close when the close-window button is clicked, use a method call like this: setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE); l The CloseWindowDemo uses this method call l When the close-window button is clicked, the program displays a confirmation dialog instead of closing the window.

COMP 150: Introduction to Object-Oriented Programming 22 Lecturer: Dr. AJ Bieszczad CloseWindowDemo Program public CloseWindowDemo() { setSize(WIDTH, HEIGHT); setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE); addWindowListener(new InnerDestroyer()); setTitle("Close Window Demo"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout());... } Prevents window from closing so that user can confirm or cancel before window is closed. Constructor for the CloseWindowDemo class, which inherits from JFrame. Defined on next slide

COMP 150: Introduction to Object-Oriented Programming 23 Lecturer: Dr. AJ Bieszczad CloseWindowDemo Program private class InnerDestroyer extends WindowAdapter { public void windowClosing(WindowEvent e) { ConfirmWindow askWindow = new ConfirmWindow(); askWindow.setVisible(true); } Inherits from WindowAdapter so it does not have to define all seven window event methods. Definition of inner class used as listener for the CloseWindowDemo class. ConfirmWindow closes window and exits program if user confirms close.

COMP 150: Introduction to Object-Oriented Programming 24 Lecturer: Dr. AJ Bieszczad CloseWindowDemo Program public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Yes")) System.exit(0); else if (e.getActionCommand().equals("No")) dispose(); //Destroys only the ConfirmWindow. else System.out.println("Error in Confirm Window."); } actionPerformed method from the ConfirmWindow inner class The main window will only be closed if the user clicks the “Yes” button in the ConfirmWindow

COMP 150: Introduction to Object-Oriented Programming 25 Lecturer: Dr. AJ Bieszczad Changing Components l A program can add or remove components after a GUI has been displayed, but that is beyond the scope of the book. l Making components visible or not visible gives a similar effect. The setVisible method is used in the VisibleDemo program to make only one of the red and green labels visible at a time. (code on next slide)

COMP 150: Introduction to Object-Oriented Programming 26 Lecturer: Dr. AJ Bieszczad Changing Components public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(“Red”)) { colorPanel.setBackground(Color.red); stopLabel.setVisible(false); goLabel.setVisible(true); validate(); }... } There is similar code for when the Green button is pressed, which turns the background green and hides the go label. The actionPerformed method from the VisibleDemo program Visibility changes won’t occur until the validate method is called.

COMP 150: Introduction to Object-Oriented Programming 27 Lecturer: Dr. AJ Bieszczad JComponent JLabelJMenuBar AbstractButton JButton JMenu JMenuItem Swing Class Abstract Class Another Look at the Swing Class Hierarchy All of the basic properties of JButton and JMenuItem are inherited from AbstractButton. JButton and JMenuItem are similar because they are derived from the same abstract class. Since AbstractButton is an abstract class, no objects of that class can be made. The purpose of the AbstractButton class is to provide a place for code that is common to JButton and JMenuItem and avoid repeated code.

COMP 150: Introduction to Object-Oriented Programming 28 Lecturer: Dr. AJ Bieszczad JComponent JLabelJMenuBar AbstractButton JButton JMenu JMenuItem Swing Class Abstract Class Another Look at the Swing Class Hierarchy JLabel and JButton inherit from a common ancestor, namely JComponent, so they have some similarities. Notice, however, that JLabel and JButton are not derived from the same class, even though they have a common ancestor. The hierarchy reflects the fact that JButton and JMenuItem are more similar than JLabel and JButton. Also notice that JMenu inherits from JMenuItem, so it can be used anywhere a JMenuItem can. This allows nested menus.

COMP 150: Introduction to Object-Oriented Programming 29 Lecturer: Dr. AJ Bieszczad Summary You can add icons to JButtons, JLabels, and JMenuItems. A JMenuBar can be added to a JFrame with the method setJMenuBar or with the usual add method. Both buttons and menu items fire action events and so should have an ActionListener registered with them. You can use the class JScrollPane to add scroll bars to a text area. You can define a window listener class by having it implement the WindowListener interface. If you want a close-button to do something other than close the window, you must use SetDefaultCloseOperation. If you change the visibility of a component you should use the validate method to update the GUI.