Introduction to Java 2 Programming

Slides:



Advertisements
Similar presentations
14 Copyright © 2005, Oracle. All rights reserved. User Interface Design: Swing Basics Planning the Application Layout.
Advertisements

Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Graphical User Interfaces
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
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.
Corresponds with Chapter 12
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh 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.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
GUIs in Java Opening Discussion zHow did the project work out? How did you decide to tackle the problem? What pieces did you break it into?
CS Lecture 00 Swing overview and introduction Lynda Thomas
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.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
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.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Chapter 9: Graphical User Interfaces
Provision for GUIs in Java
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
University of Central Florida COP 3330 Object Oriented Programming
Provision for GUIs in Java
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Introduction to Java 2 Programming Lecture 8 Java Swing API, Part 1

Overview Java AWT/Swing Fundamentals of Swing (Part 1, this lesson) Brief history, introduction to the main packages Fundamentals of Swing (Part 1, this lesson) Containers Components Layouts Fundamental of Swing (Part 2, next lesson) Event-driven programming Applets (Part 3, last lesson) Writing, and deploying applets

Java AWT Abstract Windowing Toolkit Original Java GUI API Very limited in capability Few components API not well structured, particularly event handling for user actions Not entirely portable (used native widgets) The AWT was the original Java GUI API, and for a long time was the only way of building GUI applications. It’s built as a series of wrappers around native code specific to the underlying operatin system. E.g. on Window it uses the windows API internally, varies by platform. However it is very limited (few components), very poor in comparison to native toolkits, not entirely portable. API isn’t very well designed, particularly the event handling (which deals with tying user activities – e.g. pressing a button – to appliation logic). These limitations may be partly to blame for the limited penetration of Java on the desktop, and even applets beyond its initial hype: AWT is clunky. It was therefore replaced…

JFC/Swing Java Foundation Classes (or “Swing”) Replacement for AWT (although does share some classes) Also provide basis for developing new GUI features (which are being continually added) What does Swing include? 100% Java Swing components (more, and more sophisticated) Pluggable Look and Feel Support Accessibility API Better graphics support (Java 2D) Drag and Drop The JFC (or “Swing”) was added as an extension for Java 1.1, but was properly included in Java 1.2 and upwards. Swing was the project code name. The majority of Swing is implemented in pure Java making it highly portable, although it can be resource intensive. Some features (e.g. D’n’D) involve some native code. Includes many more components (we’ll see these later); pluggable look-and-feel (makes applications look more like their native counterparts), improved graphics, etc.

JFC/Swing Disadvantages Can be slow (resource hungry) Large complex API (big learning curve) Many features best suited for GUI builders, IDEs Aim of the next few lectures is to introduce the basic concepts Provide you with background so can continue studies yourself Important to use Swing and not AWT Swing is the recommended way to build Java GUIs Disadvantages include it can be sluggish, very complex, so can have a big learning curve. Some features are best explored (or hidden!) behind a decent IDE that supports GUI building. Borland Jbuilder, which is available free for personal use is a good editor. Aim of this and the remaining lectures is to introduce the basics of Swing, the basic concepts to provide with enough background to further explore the API. It’s a very OO API, so its useful to get some exposure to it as an example of a good OO framework. The Event-Handling concept is also a useful one to understand and used in many applications. It IS possible to build GUIs with AWT, but its not recommended, so I’d be doing you all a diservice if I only cover that in this course.

Introduction to GUI Programming What are the stages in building a GUI application? Design the user interface Organising pre-built GUI components to build windows, dialogs E.g buttons, tables, menus, etc Writing the application logic What does the application do? Writing event-handling code to tie the GUI components to the application logic More on event-handling in next lesson… One way to build a GUI application is to design the UI, perhaps using a GUI building tool to construct the initial design, deciding which components to use, and how they’re laid out. E.g. do we display data as a table, or a series of text fields? How do we let the user make selections, from a list, radio buttons, check boxes, etc. We might then progress to writing the application logic – the meat of the application. The remaining steps are to tie together the GUI with the application logic, so the application responds appropriately to the users activities.

Introduction to GUI Programming Essentially, JFC/Swing provides a framework which consists of: A number of GUI components that can be used to build a user interface (javax.swing) An event-handling framework for tying user actions to application code (javax.swing.event) Occasionally use classes from the AWT equivalents (java.awt, java.awt.event) Some Swing classes inherit from originals Distinguish Swing versions from AWT versions with “J” prefix. We’ll cover the first part of this in this lecture, and progress to event handling next time. Two basic packages, although there are a number of others. AWT hasn’t entirely gone away – basically it wasn’t ALL bad. Some Swing classes inherit from the AWT originals, but implemented completely differently. Swing classes are often termed “lightweight” because they have no native portion, conversely AWT classes are “heavyweight”. To make naming obvious, the Swing versions of classes originally in AWT include a “J” prefix. E.g. Button/Jbutton, Frame/Jframe.

Building a GUI A GUI is built in layers. Bottom most layer is the window (Container) Contains all other components Can provide basic features like maximise/minimise buttons, title bar, menu bar, etc On top of this are layered (Component) Components, e.g. buttons, text fields or intermediate containers, e.g. panels Arrangement of components in a contained is handled by a layout manager Its job is to instruct components on how to arrange themselves so the GUI is drawn correctly. Top level containers, intermediate containers, components. This is all pretty standard stuff, very similar principle in Visual Basic and other GUI development environments. One quirk about Java is that the layout of components on a window (or form in VB parlance) is handled by a component called a layout manager. When drawing a GUI window, the components negotiate with their layout manager with how much space they need, positioning, etc. So the layout manager is ultimately responsible for arranging the components. Unlike in VB which uses absolute positioning: where you put the control is where it’ll always be. In java resizing a window can completely ruin a display, unless you’ve very carefully configured the layout manager. This is the point where you need a GUI builder. Personally I find it the most frustrating part of Swing. However, with care, you CAN build simple GUI apps by hand.

Building a GUI X Simple Application OK A Label Cancel Text field… Simple animation showing how a simple application window is constructed by layering components on top of one another. First we add some buttons, then a panel to further group components, then finally a text label and a text entry field. Cancel Text field…

The containment hierarchy This layered GUI can be viewed as a hierarchy of components NOT an inheritance hierarchy, It just describes how components are nested one within another

The containment hierarchy JFrame JButton JButton JPanel Stress that this isn’t showing inheritance, its show which components are nested inside which other components. JLabel JTextField

Swing Top level containers JWindow Basic no frills window, just a square on the screen JFrame The basic Swing window. Offers basic window controls, resizable JDialog For building dialog boxes, e.g. File open/save JApplet For building applets, embedded into a web page Referred to a containers, because in at least one case there’s not actually a window (applets are embedded in a web page). Jframe and Jdialog are most commonly used. Several built in dialog types mimicking those built in to many apps (e.g. File Open, File Save, Choosing Colors from a Palette, etc). Show demos at this point of a window, frame, and dialog.

Working with JFrames Many different possibilities, but the basics include: Setting window title Setting location on screen Setting size of window Restricting resizes Set close operation (exit the program), as by default it does nothing. Introduce the basics of working with Jframes. Show example code here.

Working with JFrames ExampleFrame2.java

Adding Components to a Frame A JFrame has several areas Window decorations (Optional) Menu bar Content pane Content pane is where components are added. Content pane is a Container object Obtain reference to the content pane, and then add another component to it JFrame frame = new JFrame(“Example”); JButton button = new JButton(“Click me!”); frame.getContentPane().add( button ); Content pane is where all the components live. When you’re using a frame object you need to invoke the getContentPane method to get a reference to the object, and then add things to it.

Adding Components to a Frame JFrameAndButton.java JFrameAndPanel.java

Adding Components Very common to extend the Swing components, particularly JFrame Create your own specialised versions May include a fixed set of components Provide extra methods for working with those components, etc. Encapsulates how the GUI is constructed Slightly different to Visual Basic where one tends to just use the basic components

Layout Managers Responsible for layout out (arranging) components in a Container Several different types with different uses None of them provide for precise x-y alignment, unlike VB forms

Border Layout This is the default layout for JFrame Divides the content pane into 5 areas (north, south, east, west, center) Areas are expanded/contracted as needed, along with their contents. Therefore ignores preferred size of the components. Center is the default if not specified. Adding two components to the same zone means they get added one on top of the other Instead add the components to a JPanel, and then add that instead.

Border Layout X NORTH WEST CENTER EAST SOUTH Simple animation showing how a simple application window is constructed by layering components on top of one another. First we add some buttons, then a panel to further group components, then finally a text label and a text entry field. SOUTH

Grid Layout Divides the container into a rectangular grid Configurable number rows/columns Each grid location is of equal size, one component assigned to each. Automatically assigns components to next available location

Other layout managers Flow Layout (default for JPanel) Card Layout Arranges components left-to-right Used to arrange buttons on a panel Card Layout Arranges components like a deck of cards Only one card visible at a time Box Layout, Grid Bag Layout Very sophisticated managers, used by GUI builders for very precise GUI designs. Not recommended for hand use!

Menus A Jframe can have only a single menu bar Instance of the Jmenu object A menu bar can have several menus on it Instances of the Jmenu object A menu can have several items on it Instances of the JmenuItem object Example

Other Swing Components SwingSet Demo