Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
More Swing (Chapter 14)‏ CS 180 Recitation - April 18, 2008 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L03 (Chapter 15) Creating.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
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.
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.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
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 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
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.
A Quick Java Swing Tutorial
Graphical User Interfaces
A First Look at GUI Applications Radio Buttons and Check Boxes
A First Look at GUI Applications
Java GUI.
Graphical User Interface (pronounced "gooey")
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Chapter 7 Creating User Interfaces
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

Java Programming, Second Edition Chapter Thirteen Understanding Swing Components

In this chapter, you will: Use the JFrame class Use additional JFrame class methods Use Swing event listeners Use JPanel class methods Use the JCheckBox class

Use the ButtonGroup classes Create a drop-down list and combo box using the JComboBox class Create JScrollPanes Create JToolBars

Using the JFrame Class GUI Components Insert the import statement import javax.swing.*; to take advantage of the Swing GUI components and their methods Also called widgets, which stands for windows gadgets Within the awt package, components are defined in the Component class When you use components in a Java program, you usually place them in containers

Using the JFrame Class Container- Type of component that holds other components so that you can treat a group of several components as a single entity Usually takes the form of a window Defined in the Container class

Using the JFrame The JFrame class is a subclass of the awt Component class The JFrame is the best container option for hosting Java applications The setSize() method allows you to set the physical size of a JFrame The setVisible() method makes the JFrame component visible or invisible Create a JFrame so that you can place other objects within it for display using a JPanel

Using the JFrame The JFrame class has four constructors JFrame() constructs a new frame that is initially invisible JFrame(GraphicsConfiguration gc) creates a JFrame in the specified GraphicsConfiguration of a screen device and a blank title JFrame(String title) creates a new, initially invisible JFrame with the specified title JFrame(String title, GraphicsConfiguration gc) creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen

Using Additional JFrame Class Methods When you extend the JFrame class, you inherit several useful methods The syntax to use any of these methods is to use a JFrame object, a dot, and the method name A JFrame’s action in response to a user clicking the Close button is set by passing an argument to the setDefaultCloseOperation() method placed inside the JFrame constructor method The most common action is to close the application using the argument JFrame.EXIT_ON_CLOSE

Using Additional JFrame Class Methods EXIT_ON_CLOSE exists the program when the JFrame is closed DISPOSE_ON_CLOSE closes the frame, disposes of the JFrame object, and keeps running the application DO_NOTHING_ON_CLOSE keeps the JFrame and continues running HIDE_ON_CLOSE closes the JFrame and continues running

Using Swing Event Listeners Classes that respond to user events must implement an interface that deals with the events These interfaces are called event listeners Each listener can handle a specific event type A class can implement as many event listeners as needed

Using Swing Event Listeners When a user event takes place, the appropriate method is automatically called by the system

Using JPanel Class Methods Components are added to a container’s content pane using the following steps: Create a JPanel object Add components to the JPanel using the add() method and using the component as the argument Call the setContentPane() method with the panel object created to set the application's content pane

Using JPanel Class Methods Swing components have a default size for the objects created A component’s size can be changed using the component’s setPreferredSize() method

Using the JCheckBox Class JCheckBox Class- Consists of a JLabel positioned beside a square You can click the square to display or remove a check mark

Using the ButtonGroup Classes ButtonGroup Classes Can group several JCheckBoxes so a user can select only one at a time When you group JCheckBox objects, all other JCheckBoxes are automatically turned off when the user selects any one checkbox Either create a ButtonGroup and then create the individual JCheckBoxes, or you can create the JCheckBoxes and then create the Button group Similar to a set of radio buttons but more than one can be checked

Creating a Drop-Down List and Combo Box Using the JComboBox Class JComboBox class- For picking items from a list or entering text into a field Another option is a drop-down list, also called a choice list A drop-down list can also be configured to be a combo box

Creating a Drop-Down List and Combo Box Using the JComboBox Class You can build a JComboBox by using a constructor with no arguments and then adding items to the list with the addItem() method The following statements create a JComboBox with three options JComboBox majorChoice = new JComboBox(); majorChoice.addItem(“English”); majorChoice.addItem(“Math”); majorChoice.addItem(“Sociology”);

Creating JScrollPanes A JScrollPane is a container whose methods can be used to hold any component that can be scrolled Add a JTextArea component to use both multiple rows and columns

JScrollPane forms The JScrollPane constructor takes one of four forms: JScrollPane() creates an empty JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane(Component) creates a JScrollPane that displays the contents of the specified component JScrollPane(Component, int, int) creates a JScrollPane that displays the specified component, vertical scrollbar, and horizontal scrollbar. JScrollPane(int, int) creates a scroll pane with specified vertical and horizontal scrollbars

Horizontal and vertical scrollbar constants HORIZONTAL_SCROLLBAR_AS_NEEDED HORIZONTAL_SCROLLBAR_ALWAYS HORIZONTAL_SCROLLBAR_NEVER VERTICAL_SCROLLBAR_AS_NEEDED VERTICAL_SCROLLBAR_ALWAYS VERTICAL_SCROLLBAR_NEVER

Creating JToolBars A Swing GUI can be designed so that a user can move a toolbar from one section of a graphical user interface to another section This type of toolbar is called dockable toolbar The process that allows you to move and then attach the toolbar is called docking A toolbar is created in Swing with the JToolBar class

Creating JToolBars Constructor methods for the JToolBar class include the following: JToolBar() creates a new toolbar that will line up components in a horizontal direction JToolBar(int) creates a new toolbar with a specified orientation of horizontal or vertical