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.

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

Graphical User Interfaces Java’s AWT and Swing APIs.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
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.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
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 
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.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
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.
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.
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.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
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.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
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 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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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
A First Look at GUI Applications
Java Swing.
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

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 with a graphical user interface (GUI)  table controls, list controls, tree controls, buttons, and labels, and so on…

 In April 1997, JavaSoft announced the Java Foundation Classes (JFC).  a major part of the JFC is a new set of user interface components called Swing. AWTSwingAccessibility Java 2D Drag And Drop

 AWT  java.awt  java.awt.color  java.awt.datatransfer  java.awt.event  java.awt.font  java.awt.geom  java.awt.image  Swing  javax.accessibility  javax.swing  javax.swing.colorchoos er  javax.swing.event  javax.swing.filechooser  javax.swing.plaf  javax.swing.table  javax.swing.text.html  javax.swing.tree

 A GUI consists of different graphic Component objects which are combined into a hierarchy using Container objects.  Component class  An abstract class for GUI components such as menus, buttons, labels, lists, etc.  Container  An abstract class that extends Component. Containers can hold multiple components.

 Container  Type of component that holds other components  Can treat group as single entity  Defined in Container class  Often takes form of window  Drag  Resize  Minimize  Restore  Close

 Swing provides:  A wide variety of components (tables, trees, sliders, progress bars, internal frame, …)  Swing components can have tooltips placed over them  Arbitrary keyboard events can be bound to components  Additional debugging support.  Support for parsing and displaying HTML based information

JFrame window = new JFrame(" title " );  A JFrame is a Window with all of the adornments added  JFrame inherits from Frame, Window, Container, Component, and Object  A JFrame provides the basic building block for screen-oriented applications

import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame ( "My First GUI Program" ) ; window.setVisible(true); }

 Set size and title window.setSize(200, 100); window.setTitle("My frame");  Close JFrame  Click Close button  JFrame becomes hidden and application keeps running  Default behavior  To change this behavior  Use setDefaultCloseOperation() method window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

import javax.swing.*; import java.awt.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame( "My First GUI Program" ); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setSize( 250, 150 ); window.setVisible(true); }

 JFrame s have several panes:  Components are placed in the Content Pane Glass pane Layered pane Menu bar Content pane

 JComponent  JComboBox, JLabel, JList, JMenuBar, JPanel, JPopupMenu, JScrollBar, JScrollPane, JTable, JTree, JInternalFrame, JOptionPane, JProgressBar, JRootPane, JSeparator, JSlider, JSplitPane, JTabbedPane, JToolBar, JToolTip, Jviewport, JColorChooser, JTextComponent, …

label = new JLabel("text", JLabel.RIGHT ); Font typeSet = new Font("Arial", Font.BOLD, 36 );  JLabels are components that you can fill with text.  When creating a label you can specify the initial value and the alignment you wish to use within the label.  You can use getText() and setText() to get and change the value of the label.  You can use setFont() to change the font, style and size of your text

import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame ( "My First GUI Program" ) ; JLabel label = new JLabel( "Hello World" ); Font typeSet = new Font( "Century", Font.BOLD, 28 ); label.setFont(typeSet); window.add( label ); window.setSize(250, 100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); }

 JButton extends Component, displays a string, and delivers an ActionEvent for each mouse click.  Normally buttons are displayed with a border  In addition to text, JButtons can also display icons button = new JButton( ”text“ );

import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame window = new JFrame( "My First GUI Program" ); JButton button = new JButton( "Click Me!!" ); window.add( button ); window.setSize(250, 100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.setVisible(true); }

 Layout Manager  An interface that defines methods for positioning and sizing objects within a container. Java defines several default implementations of LayoutManager.  Geometrical placement in a Container is controlled by a LayoutManager object

19  Containers may contain components (which means containers can contain containers!!).  All containers come equipped with a layout manager which positions and shapes (lays out) the container's components.  Much of the action in Swing occurs between components, containers, and their layout managers.

20  Layouts allow you to format components on the screen in a platform-independent way  The standard JDK provides many classes that implement the LayoutManager interface, including:  FlowLayout  GridLayout  BorderLayout  BoxLayout  CardLayout  OverlayLayout  GridBagLayout

21  Invoke the setLayout() method on the container to use the new layout. setLayout( new FlowLayout() );  The layout manager should be established before any components are added to the container  JFrame will otherwise stack all components on top of each other

22  When you add components to the screen, they flow left to right (centered) based on the order added and the width of the screen.  Very similar to word wrap and full justification on a word processor.  If the screen is resized, the components' flow will change based on the new width and height  FlowLayout is the default layout for the JPanel class.

import javax.swing.*; import java.awt.*; public class LayOut { public static void main( String args[] ) { JFrame window = new JFrame( "Layout Program" ); JLabel title = new JLabel( "Layout Example" ); JLabel name = new JLabel( "Enter your name" ); JTextField textInput = new JTextField( 12 ); JButton button = new JButton( "Enter your name" ); window.setSize(200, 150); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); window.setLayout( new FlowLayout() ); window.add( title ); window.add( name ); window.add( textInput ); window.add( button ); } }

24 import javax.swing.*; import java.awt.*; public class ShowFlowLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.getContentPane().setLayout( new FlowLayout() ); for ( int i = 0; i < 10; i++ ) { win.getContentPane().add( new JButton( String.valueOf( i ) ) ); } win.setVisible(true); }

25 You can resize with the mouse and the buttons will move to fill in the space

 Programs respond to events that are generated outside the control of the program  User types a key  The left mouse button is pressed  A CD is removed from the CD drive  When an event occurs, it is handled by an event handler  Event driven programming involves 1. writing the handlers, and 2. arranging for the handler to be notified when certain events occur

 An event is represented by an object that gives information about the event and identifies the event source  Event sources are typically components, but other kinds of objects can also be event sources  A listener is an object that wants to be notified when a particular event occurs  An event source can have multiple listeners registered on it  A single listener can register with multiple event sources

 In order for an object to be notified when a particular event occurs, the object 1. must implement the appropriate Listener interface 2. must be registered as an event listener on the appropriate event source

ActionListener Type User clicks a button, presses return while typing in a text field, or chooses a menu item ActionListener Users closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener A component becomes visible ComponentListener A component gets the keyboard focus FocusListener A table of list selection changes ListSelectionListener