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.

Slides:



Advertisements
Similar presentations
G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)
Advertisements

Introduction to Java 2 Programming
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.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Graphical User Interfaces
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
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.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
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.
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.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Advanced GUI Programming
Java Swing.
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
תכנות מכוון עצמים בשפת JAVA
Steps to Creating a GUI Interface
Advanced GUIs and Graphics
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, …

lbl = new JLabel( text", JLabel.RIGHT ); 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.

import javax.swing.*; public class SwingFrame { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); JLabel label = new JLabel( "Hello World" ); win.getContentPane().add( label ); win.setVisible(true); } } // SwingFrame

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 win = new JFrame( "My First GUI Program" ); JButton button = new JButton( "Click Me!!" ); win.getContentPane().add( button ); win.setVisible(true); } } // SwingFrame

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 1. To change the layout used in a container you first need to create the layout. 2. Then you invoke the setLayout() method on the container to use the new layout. The layout manager should be established before any components are added to the container JPanel p = new JPanel() ; p.setLayout( new FlowLayout() );

22 FlowLayout is the default layout for the JPanel class. 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

23 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); } } // ShowFlowLayout

24

CS3 - AWT/Swing25 Arranges components in rows and columns If the number of rows is specified columns = number of components / rows If the number of columns is specified Rows = number of components / columns The number of columns is ignored unless the number of rows is zero. The order in which you add components matters Component 1 (0,0), Component 2 (0,1), …... Components are resized to fit the row-column area

CS3 - AWT/Swing26 import javax.swing.*; import java.awt.*; public class ShowGridLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.getContentPane().setLayout( new GridLayout( 2, 0 ) ); for ( int i = 0; i < 10; i++ ){ win.getContentPane().add( new JButton( String.valueOf( i ) ) ); } win.setVisible(true); } } // ShowGridLayout

CS3 - AWT/Swing27 gridLayout( 2, 4 ) gridLayout( 0, 4 ) gridLayout( 4, 4 )gridLayout( 10, 10 )

CS3 - AWT/Swing28 BoxLayout provides an easy way to lay out components horizontally or vertically. Components are added in order. BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or preferred heights (for vertical layout). Static methods in Box class are available for glue and struts.

CS3 - AWT/Swing29 import javax.swing.*; import java.awt.*; public class ShowBoxLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); win.getContentPane().setLayout( new BoxLayout( win.getContentPane(), BoxLayout.X_AXIS ) ); for ( int i = 0; i < 10; i++ ){ win.getContentPane().add( new JButton( String.valueOf( i ) ) ); win.getContentPane().add( Box.createHorizontalGlue() ); } win.pack(); win.setVisible(true); } } // ShowBoxLayout

CS3 - AWT/Swing30 Note that components retain their preferred size.

CS3 - AWT/Swing31 BorderLayout provides 5 areas to hold components. These are named after the four different borders of the screen, North, South, East, West, and Center. When a Component is added to the layout, you must specify which area to place it in. The order in which components are added is not important. The center area will always be resized to be as large as possible

CS3 - AWT/Swing32 import javax.swing.*; import java.awt.*; public class ShowBorderLayout { public static void main( String args[] ) { JFrame win = new JFrame( "My First GUI Program" ); win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); Container content = win.getContentPane(); content.setLayout( new BorderLayout() ); content.add( BorderLayout.NORTH, new JButton( "North" ) ); content.add( "South", new JButton( "South" ) ); content.add( "East", new JButton( "East" ) ); content.add( "West", new JButton( "West" ) ); content.add( "Center", new JButton( "Center" ) ); win.setVisible(true); } } // ShowBorderLayout

CS3 - AWT/Swing33

CS3 - AWT/Swing34 A JFrame is not the only type of container that you can use in Swing The subclasses of Container are: JPanel JWindow JApplet Window is subclassed as follows: JDialog JFrame

CS3 - AWT/Swing35

CS3 - AWT/Swing36 JFrame with BorderLayout JButton JLabel JPanel with GridLayout

CS3 - AWT/Swing37 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalcGui implements { // Labels for the buttons private static final String labels = "789X456/123-0C=+"; private static final int NUMROWS = 4; private static final int NUMCOLS = 4; private JLabel display; // The display public CalcGui( String name ) { // A Frame for the calculator JFrame win = new JFrame(name);

CS3 - AWT/Swing38 // Create the button panel JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(NUMROWS, NUMCOLS)); JButton b; for ( int i = 0 ; i < labels.length() ; i++ ) { b = new JButton( labels.substring( i, i + 1 ) ); buttons.add( b ); } // Create the display display = new JLabel( "0", JLabel.RIGHT ); display.setFont( new Font( "Courier", Font.BOLD, 24 ) );

CS3 - AWT/Swing39 // "Assemble" the calculator Container content = win.getContentPane(); content.setLayout( new BorderLayout() ); content.add( "North", display ); content.add( "Center", buttons ); // Display it and let the user run with it :-) win.pack(); win.setVisible(true); }