Implementing GUIs in Java ● The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs: – Abstract Window Toolkit (AWT): native.

Slides:



Advertisements
Similar presentations
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Advertisements

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.
 The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs:  Abstract Window Toolkit (AWT) : native GUI components  Swing.
Corresponds with Chapter 12
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
CSE 219 Computer Science III Graphical User Interface.
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.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
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.
GUI development in Java L. Grewe Two methodologies for things to appear on the screen 1. By painting / drawing e.g. drawing an Image e.g. drawing an.
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.
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.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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.
Java Swing Reference: 1.Swing: A Beginner’s Guide by Herbt Schildt, TMH Edition.
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.
University of Limerick1 Software Architecture Java Layout Managers.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components 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.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
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.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Welcome To java
Object-Orientated Analysis, Design and Programming
Abstract Window Toolkit (AWT): native GUI components
Graphical User Interfaces
Swing JComponents.
A First Look at GUI Applications
Advanced GUI Programming
Modern Programming Language Java
A Quick Java Swing Tutorial
Overview of Java GUIs ….
Graphical User Interface
Presentation transcript:

Implementing GUIs in Java ● The Java Foundation Classes (JFC) are a set of packages encompassing the following APIs: – Abstract Window Toolkit (AWT): native GUI components – Swing: lightweight GUI components – 2D: rendering two-dimensional shapes, text, and images – Accessibility: allowing compatibility with, for example, screen readers and screen magnifiers

Abstract Window Toolkit (AWT) ● Provides basic UI components: – Buttons, lists, menus, textfields, etc – Event handling mechanism – Clipboard and data transfer – Image manipulation – Font manipulation – Graphics ● Platform independence is achieved through peers, or native GUI components

AWT Packages java.awt Basic component functionality java.awt.accessibility Assistive technologies java.awt.color Colors and color spaces java.awt.datatransfer Clipboard and data transfer support java.awt.dnd Drag and drop java.awt.event Event classes and listeners java.awt.font 2D API font package java.awt.geom 2D API geometry package java.awt.im Input methods java.awt.image Fundamental image manipulation classes java.awt.peer Peer interfaces for component peers java.awt.print 2D API support for printing java.awt.swing Swing components

Peers and Platform Independence ● The first AWT (Java 1.0) was rolled out in an incredible 6 weeks using peers ● Thus an AWT menu on the Solaris platform, for example, actually creates a Motif menu object as its peer ● UI components that have peers are called heavyweight because – they are rendered in their own (opaque) windows and thus are expensive to use, – they must be rectangular and cannot have transparent backgrounds, and – they are not amenable to being subclassed

Using Peers Java Program Java AWT Native Window System Peers A Java program creates and displays an AWT component, which creates and displays a native component, or peer.

Lightweight Components ● AWT 1.1 introduced the notion of lightweight components which: – are contained within a heavyweight component's window – do not have peers – are rendered in their container's window rather than one of their own – do not incur performance penalties and can have transparent backgrounds ● Almost all Swing components are lightweight ones that extend either java.awt.Component or java.awt.Container

Some AWT Components Object Component ContainerButtonList Scrollbar Label Canvas Button JComponent

AWT vs. Swing ● Swing does not replace the AWT; it is built on top of it ● All 1.0 AWT components are heavyweight; corresponding Swing components are lightweight ● Swing component names begin with ``J'': – Component (AWT) vs. JComponent (Swing) – Button (AWT) vs. JButton (Swing) ● Always use Swing components; however, since Swing is built on top of AWT, you will need to know some AWT methods

Some Swing Components JComponent AbstractButton JButton JMenuIte m JToggleButton JCheckBox JLabel JList JScrollBar JFileChooser

JComponent s ● Note that JComponent s are containers ● JComponent s do not extend their AWT counterparts: – For example, the JButton class is not a subclass (direct or indirect) of Button ● However, some Swing components are not JComponent s – For example, some Swing containers are direct subclasses of their AWT counterparts

Some AWT Containers Container JComponentPanel ScrollPane Window DialogFrame Applet

Swing Components That Are Not JComponents (in red) Container JComponentPanel ScrollPane Window DialogFrame Applet JDialog JWindow JFrame JApplet

Some More Swing Components That Are JComponents JComponent JLayeredPaneJPanel JScrollPane JInternalFrame JDesktopPane JTable JTree

Some AWT Component Methods ● void setBackground(Color c) ● void setForeground(Color c) ● void setEnabled(boolean b) ● void setVisible(boolean b) ● void setFont(Font f) ● void setSize(Dimension d) ● void setLocation(int x, int y) All but setSize and setLocation are overridden by the JComponent class.

Example: A Simple Framed Window import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); frame.setVisible(true); }

Notes on the Example ● setSize and setLocation require java.awt.* ; the rest require javax.swing.* ● The JFrame constructor argument is used as a title ● The Dimension constructor takes an integer width and height, respectively ● The setLocation method takes a pair of integer coordinates (x,y) where (0,0) is the upper left corner of the display ● The visibility of a JFrame is set to false by default

Example Output Display ● This window was managed by the K Desktop Environment (KDE) ● Clicking the Close button (X) will cause the display to be hidden, but the program will continue since no listeners are set up yet ● Can use ctl-C to kill the Java Virtual Machine

Adding Color – Color.black – Color.blue – Color.cyan – Color.darkGray – Color.gray – Color.green – Color.lightGray – Color.magenta – Color.orange – Color.pink – Color.red – Color.white – Color.yellow The java.awt.Color class has the following static fields (data members):

Changing Background Color import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); contentPane.setBackground(Color.red); frame.setVisible(true); }

Content Panes ● Q: Why not just: frame.setBackground(Color.red); ? ● A: In order to be lightweight, Swing's top-level window objects must be built on top of a lightweight AWT Container object introduced in version 1.1 ● This container is called a content pane ● Swing top-level window classes: – JWindow – JFrame – JApplet – JDialog – JInternalFrame

Adding a Label and Button import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH); frame.setVisible(true); }

New Display Resized

Notes on the Code ● Since the frame is a top-level Swing window, components must be add ed to its content pane ● When components are added to a container, how they are placed is dependent upon the container's layout manager ● The default layout manager for a JFrame is a BorderLayout manager (described later) ● When adding to a container whose layout manager is BorderLayout, the second parameter should be a location defined in the BorderLayout class

Adding a List of Options import java.awt.*; import javax.swing.*; public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH); String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER); frame.setVisible(true); }

New Display Note that "Option 3" has been selected.

Adding a Check Box and Slider public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(400,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE IS A LABEL"); contentPane.add(label, BorderLayout.NORTH); JButton button = new JButton("BUTTON"); contentPane.add(button, BorderLayout.SOUTH); String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); contentPane.add(list, BorderLayout.CENTER); JCheckBox cbox = new JCheckBox("Check"); contentPane.add(cbox, BorderLayout.WEST); JSlider slider = new JSlider(); contentPane.add(slider, BorderLayout.EAST); frame.setVisible(true); }

New Display

Layout Management ● A layout manager determines the location and size of components placed into a container ● Different layout managers use different algorithms for determining size and location: – BorderLayout : places at compass locations and center – FlowLayout: places components in rows, left to right – GridLayout : places in rectangular grid – BoxLayout : places in a single row or column

Changing the Layout public class SwingTest { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); JLabel label = new JLabel("HERE IS A LABEL"); JButton button = new JButton("BUTTON"); String[] options = {"Option 1", "Option 2", "Option 3"}; JList list = new JList(options); JCheckBox cbox = new JCheckBox("Check"); JSlider slider = new JSlider(); contentPane.add(label); contentPane.add(button); contentPane.add(list); contentPane.add(cbox); contentPane.add(slider); frame.setVisible(true); }

New Display Resized

Default Layout Managers ● The default layout manager for content panes is BorderLayout. Recall that the following Swing components have content panes: – JWindow – JFrame – JDialog – JApplet – JInternalFrame ● The other Swing container is the JPanel, whose default layout manager is FlowLayout.

JPanel s ● A JPanel object can be used for grouping components into a container, which can then be added to another container ● The JPanel constructor with no arguments creates a panel with a FlowLayout manager ● Another JPanel constructor takes any layout manager as an argument ● A JPanel can also be used a a blank area for drawing custom graphics

JPanel Example JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER); JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);

JPanel Example Output Note use of SwingConstants.CENTER argument in JLabel constructor.

Changing JPanel Layout JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER); JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.setLayout (new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);

New Output ● The button panel is to the west because no other component was placed there ● The BoxLayout constructor requires both the component being laid out and either: – BoxLayout.X_AXIS – BoxLayout.Y_AXIS

Tweaking Layouts ● Some layout constructors allow hgap and vgap, integers specifying the number of pixels separating components horizontally and vertically ● FlowLayout allows the specification of whether the line of components should be left- justified, right-justified, or centered new FlowLayout(int align) new FlowLayout(int align, int hgap, int vgap) new BorderLayout(int hgap, int vgap) new GridLayout(int rows, int cols) new GridLayout(int rows, int cols, int hgap, int vgap)

Tweaking Example JFrame frame = new JFrame("Test Frame"); frame.setSize(new Dimension(300,200)); frame.setLocation(100,100); Container contentPane = frame.getContentPane(); LayoutManager lm = contentPane.getLayout(); ((BorderLayout)lm).setHgap(25); JLabel label = new JLabel("HERE ARE SOME BUTTONS", SwingConstants.CENTER); JButton button1 = new JButton("BUTTON1"); JButton button2 = new JButton("BUTTON2"); JButton button3 = new JButton("BUTTON3"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(button1); panel.add(button2); panel.add(button3); contentPane.add(label, BorderLayout.NORTH); contentPane.add(panel, BorderLayout.CENTER); frame.setVisible(true);

Tweaking Example Output ● The LayoutManager returned by getLayout() is an interface type that the BorderLayout class implements ● The setHgap method we want is in the BorderLayout class ● So we must cast the LayoutManager to BorderLayout in order to use setHgap

Sizing Hints ● Layout managers often need to resize their components to make things fit – For example, the widths and heights of components in a BoxLayout are adjusted according to both preferred and maximum heights and widths ● If you don't like the size of the components a layout manager comes up with, you may have to give sizing hints using the following methods from the JComponent class: – void setMinimumSize(Dimension d) – void setPreferredSize(Dimension d) – void setMaximumSize(Dimension d)