2003-01-27MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Graphical User Interfaces
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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 
GUI Programming in Java
Java Swing © Walter Milner 2005: Slide 1 Java Swing Walter Milner.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
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, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
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,
Interfaces & Polymorphism part 2:
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.
GUI programming Graphical user interface-based programming.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
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 Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
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.
Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
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.
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.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
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 Swing. Note - this presentation.. often needs to refer to source code which is too big to put on a slide So the source code is in a separate Word.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
A First Look at GUI Applications
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:

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 r Input in Frames r Reading: m Horstmann, Chapters 4, 10 and 12 m Swing tutorial Swing tutorial m Java tutorial, Chapter on Swing (downloadable) Java tutorial, Chapter on Swing

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -2 Java API r java.awt - Original GUI toolkit in JDK 1.1, implemented using native GUI libraries of the operating systems. (Problem: portability.) r javax.swing - Portable GUI toolkit added in Java 2, extending java.awt. (Gives platform-independent operation, though it is slower.) r Many other third party GUI toolkits, e.g., SWT in Eclipse.

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -3 JFrame r In many application programs one needs to import both java.awt and javax.swing.  To avoid confusion, the common class names in Swing are are prefixed with the letter “J”, e.g., JFrame, JApplet, JButton, JMenubar etc. r Frame – The Java term for a GUI window.

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -4 Creating JFrames //1. Optional: Specify who draws the window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //2. Create the frame. JFrame frame = new JFrame("FrameDemo"); //3. Optional: What happens when the frame closes? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //4. Create components and put them in the frame. //...create emptyLabel... frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //5. Size the frame. frame.pack(); //6. Show it. frame.setVisible(true);

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -5 Creating JFrames - Commentary 1. Asks that the frame should be decorated with borders etc. 2. Creates an instance of the JFrame class. 3. Specifies that the program should exit when the frame is closed. (If you don’t put this, the program might hang.) 4. Adds something called “emptyLabel” as a GUI component to the frame. 5. Asks that the frame be automatically sized based on its contents and their preferred sizes. 6. Makes the frame visible. (Otherwise, it would be hidden by default.)

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -6 Components and Containers r The things that you can display in GUI windows are called components. Examples are buttons, text fields, tables etc. r Some of the components are containers, i.e., they can contain other components inside them (which might again be containers etc.). r A JFrame is a container of a specialized sort. It has several “panes” as its components, the important one being its contentPane.

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -7 The contentPane r The contentPane is a container. So, we can add components to it. Container pane = frame.getContentPane(); pane.add(new JLabel( new ImageIcon( “world.gif”)))); pane.add(new JLabel(“Hello World!”)); pane.add(new JButton(“Click Me”)); r Or, we can create a new contentPane: JPanel pane = new JPanel(); frame.setContentPane(pane); pane.add(new JLabel(“Hello World!”));

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -8 JPanel and JApplet r Panels are simple lightweight containers. r They support graphics, in addition to user interface components. r Applets are frames that can be drawn inside web pages, included using a HTML tag such as: <APPLET code=“MyApplet.class” width=400 height=300> If you Java-enable your browser, you will see applet.

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -9 GUI Components r Display elements: text labels, image labels. r Basic controls: text fields, buttons, radio buttons, check-boxes, option lists, combo- boxes (drop-down lists), menus, sliders. r Containers: panels, scroll-panes, split-panes, tabbed-panes, toolbars. r Fancy displays: tables, tree-displays. r Fancy controls: file chooser, color chooser. r See

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -10 Using Inheritance to define Panels r Good design practice to define panels as classes, extending the JPanel class. public class MyPanel extends JPanel { int count = 0; // the state JButton button = new JButton(“Click me”); JLabel label = new JLabel(“0”); public MyPanel() { add(button); add(label); } r It is even necessary when you need to include graphics painting.

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -11 Input in panels r Users enter text in text field components r Reading the value in a text field involves four changes to what we’ve seen: import java.swing.*; import java.awt.event.*; public class MyPanel extends JPanel implements ActionListener { 1 2

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -12 Input in panels (cont.) JTextField t = new JTextField(4); public MyPanel () {... add(t); t.addActionListener(this); } public void actionPerformed (ActionEvent e) {... t.getText() } 3 4

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -13 Input in panels (cont.) “ import java.awt.event.* ” must appear verbatim (the event-handling library). “ implements ActionListener ” must appear verbatim (to say that it handles action events). JTextField variable, say t, is declared as instance variable In the constructor, in addition to calling add, call t.addActionListener(this); (to say that this object will handle the action events from t )

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -14 Input in applets (cont.) Define actionPerformed method. m Called when user types Enter key in the text field.  Use getText method of JTextField class to get the String contained in t. 4

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -15 The event model r The code above is one instance of the event model. r With the event model, the panel can respond to button clicks, mouse movements, etc. r Can also use multiple text fields and distinguish which one was modified. r More in the next lecture...

MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -16 Layout r Layout of GUI panels is a tricky business. m Need to allow for resizing of windows. m Need to be flexible about the text labels etc. r Java library defines a number of “Layout Managers” to facilitate layout. r Or, you can define your own or import third party libraries. r