Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo Program l Layout Managers l Buttons and Action Listeners l Container.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Java Swing 4 th April 2008 CS 180 Department of Computer Science, Purdue University.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Swing. Introduction to Swing A GUI (graphical user interface) is a windowing system that interacts with the user The Java AWT (Abstract Window Toolkit)
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 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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.
Chapter 12- GUI’s, Java, and Swing.. Overview n What are GUI’s n How Java does GUI’s- Swing n Buttons n Containers n Text I/O and Swing n Review.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Layout Managers Arranges and lays out the GUI components on a container.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
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.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Event-Driven Programming and GUIs l Swing Basics and.
COMP 150: Introduction to Object-Oriented Programming 1 Lecturer: Dr. AJ Bieszczad l Event-Driven Programming and GUIs l Swing Basics and a Simple Demo.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
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.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
1 Lecture 8: User Interface Components with Swing.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Creating Graphical User Interfaces
Window Interfaces Using Swing Objects Chapter 12
Window Interfaces Using Swing Objects Chapter 12
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University

Announcements Project 8 is out  Milestone due on Dec 3rd, 10:00 pm  Final due on Dec 10th, 10:00 pm No classes, recitations and labs next week. No study group meeting next Tuesday. Consulting hours will be held only on Monday 7-10 pm.

Event Driven Programming Most GUI programs involve events and event handlers. A GUI event is an object that represents some action such as clicking the mouse, dragging the mouse, pressing a keyboard key, clicking the close-window button on a window, etc. When an object generates an event, it is said to fire the event.

4 Event Driven Programming An event is an object that represents an action Event handling is similar to exception handling Difference –  Exceptions are created by user code or java interpreter.  Events are created by external actions, such as user interactions through a GUI

Programming Example: A Simple Window This simple program produces a window and displays some text. JFrame : to create a window JLabel : to create a label getContentPane().add() : add a component such as a label to the content pane of the window setTitle() : set the title of the window setSize() : set the size of the window setVisible() : Method setVisible permits the programmer to specify when GUI objects should be displayed and when they should not

public class makeWindow { public static void main(String args[]) { JFrame myWindow=new JFrame(); //create the window myWindow.setSize(300, 200); //set the title of the window myWindow.setTitle("this is a window"); //create the label JLabel myLabel=new JLabel("this is a label"); //add the label to the content pane of the window myWindow.getContentPane().add(myLabel); //set color of the content pane myWindow.getContentPane().setBackground(Color.CYAN); //make the window visible myWindow.setVisible(true); } A Frame This area is the content pane Example Programming Example: A Simple Window

7 Layout Managers A layout manager arranges objects within a container After a container has been created, you can set its layout manager using the setLayout method.  For example: Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); FlowLayout : It simply lays out components in a single row, starting a new row if its container is not sufficiently wide.

8 Layout Managers BorderLayout : It places components in up to five areas: top, bottom, left, right, and center.  Every content pane is initialized to use a BorderLayout. GridLayout : It simply arranges a bunch of components in a grid of rows and columns. Components are ordered in a row- major fashion.

Example - Border Layout A BorderLayout manager can place a component into any of the five regions. Regions which are unused give up their space to BorderLayout.CENTER. This layout limits the GUI to five objects, these are almost always five (or fewer) JPanels. Equivalent forms for center: content.add(label3, BorderLayout.CENTER); and content.add(label3, “Center”); and (for center ONLY) content.add(label3); Even though BorderLayout is default, it’s better to set the layout explicitly

Buttons A button is a GUI component that looks like a button and fires an event when it is clicked using a mouse. Like a label, a button is created and added to a container. Unlike a label, a button can fire an event and the event can cause a GUI to perform some action. Buttons are instances of the JButton class.

Adding Buttons A button is created using JButton Button_Name = new JButton(“Button_Label”); A button is added to a container using Container_Name.add(Button_Name);

Action Listeners and Action Events For each button, the GUI needs to  register (specify) the listener object(s).  define the methods to be invoked when an event is fired. Buttons fire action events which are handled by action listeners. An action listener is an object of type ActionListener, and ActionListener is an interface Note: A user defined GUI class can itself be its own listener if it implements the ActionListener interface.

Action Listeners and Action Events, cont. To make a class into an ActionListener,  add implements ActionListener to the heading of the class definition  Define a method named actionPerformed.  register the ActionListener object with the component that will fire the event using the method addActionListener(..) The actionPerformedMethod of the ActionListener class will be called every time the object fires an event. This is an important step that must not be forgotten.  (A component may register with more than one listener.)

Buttons and an Action Listener

The actionPerformed Method An actionListener class must have a method named actionPerformed that has one parameter of type ActionEvent. syntax public void actionPerformed(ActionEvent e) { Code_for_Actions_Performed }

Method setActionCommand Every object that fires an action event has an associated string known as the action command for that component. e.getActionCommand() returns the action command for the component that fired the event e. The default action command for a button is its name. Method setActionCommand(String) can be used to change the action command for the object.

. Example with buttons

Example with buttons

The JPanel Class A GUI can be organized hierarchically, with window-like containers inside other containers. Components can be placed in a JPanel which can be placed in another JPanel, … which can be placed in a JFrame. E.g, to place two components in BorderLayout.SOUTH for example, simply place the two components in a panel and place the panel in the BorderLayout.SOUTH position. The panel has its own layout manager.

Example with JPanel Container contentPane = getContentPane (); contentPane.setBackground (Color.BLUE); contentPane.setLayout (new BorderLayout ()); JPanel buttonPanel = new JPanel (); buttonPanel.setBackground (Color.WHITE); buttonPanel.setLayout (new FlowLayout ()); JButton stopButton = new JButton ("Red"); stopButton.setBackground (Color.RED); stopButton.addActionListener (this); buttonPanel.add (stopButton); JButton goButton = new JButton ("Green"); goButton.setBackground (Color.GREEN); goButton.addActionListener (this); buttonPanel.add (goButton); contentPane.add (buttonPanel, BorderLayout.SOUTH); Adding panel to the frame Adding a green button to the panel Adding a red button to the panel Creating a panel with flow layout

The JPanel Class, cont. Panel with two buttons since BorderLayout limits the GUI to five objects, there are usually JPanels (five or fewer) in the pane.

The Container Class An object of a class which descends from class Container is called a container class and can have components added to it. Examples –  JFrame is a descendent of class Container, permitting any JFrame object to hold labels, buttons, panels, and other components.  JPanel is a descendent of class Container, permitting any JPanel object to hold labels, buttons, other panels, and other components.

Text I/O using JTextField Create a text field with some initial text JTextField textfield = new JTextField("Initial Text"); Create a text field with some initial text and a default number of columns. The number of columns controls the preferred width of the component textfield = new JTextField("Initial Text", cols); Use textfield.getText() to return the written text

Text I/O using JTextArea Create a text area with some initial text JTextArea textarea = new JTextArea("Initial Text"); Create a text area with some initial text and a default number of rows and columns. This number of rows and columns controls the preferred width and height of the component; textarea = new JTextArea(“InitialText", rows, cols);

Quiz GUI components can be added to an object of any class that descends from the _______ class.