Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphical User Interfaces
Java Software Development Paradigm Lecture # 12. Basics of GUI.
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.
Corresponds with Chapter 12
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
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.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
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.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
– 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.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
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.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
1 12 Apr 2011 GUIS: Graphical User Interfaces Their mouse had a mean time between failure of … a week … it would jam up irreparably, or... jam up on the.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Csc Basic Graphical User Interface (GUI) Components.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
GUI building with the AWT
Graphical User Interfaces -- GUIs
Swing JComponents.
8 April 2008 GUIS —Graphical User Interfaces
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
GUI building with the AWT
GUI building with the AWT
13 April 2010 GUIS: Graphical User Interfaces
Graphical User Interface
Presentation transcript:

Java GUI building with Swing

2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for many applications Uses the controls defined by your OS (whatever it is) therefore it's “least common denominator” Difficult to build an attractive GUI import java.awt.*; import java.awt.event.*;

3 Swing Present in all modern Java implementations (since 1.2) More controls, and they are more flexible Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*; import javax.swing.event.*; and import java.awt.*; import java.awt.event.*; You may not need all of these packages

4 Swing vs. AWT Swing is built “on top of” AWT, so you need to import AWT and use a few things from it Swing is bigger and slower Swing is more flexible and better looking Swing and AWT are incompatible--you can use either, but you can’t mix them Actually, you can, but it’s tricky and not worth doing Basic controls are practically the same in both AWT: Button b = new Button ("OK"); Swing: JButton b = new JButton("OK"); Swing gives far more options for everything (buttons with pictures on them, etc.)

5 To build a GUI... 1.Make somewhere to display things (a Container ) Usually you would use a JFrame or a JApplet 2.Create some Components (buttons, text areas, panels, etc.) It’s usually best to declare Components as instance variables, and Define them in your applet’s init() method or in some application method 3.Add your Components to your display area Choose a layout manager Add your Components to your JFrame or JApplet according to the rules for your layout manager 4.Attach Listeners to your Components Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs, and executes some code to deal with it

6 Container s and Component s A GUI is built by putting components into containers The job of a Container is to hold and display Component s Some frequently used types (subclasses) of Component are JButton, JCheckbox, JLabel, JTextField, and JTextArea A Container is also a Component This allows Containers to be nested Important Container classes are JFrame, JApplet, and JPanel JFrame and JApplet both contain other containers; use getContentPane() to get to the container you want You typically create and use JPanel s directly

7 Starting with a Container First, import some packages: import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; Second, extend a Container type: For an application, extend JFrame public class MyClass extends JFrame {... } For an applet, extend JApplet public class MyApplet extends JApplet {... } Neither of these returns a Container that you use directly; instead, Both JFrame and JApplet have a getContentPane() method that returns a Container you can use: getContentPane( ). doSomething( ) ;

8 A JApplet is a Panel is a Container java.lang.Object | java.awt.Component | java.awt.Container | java.awt.Panel | java.applet.Applet | javax.swing.JApplet …so you can display things in an Applet

9 Example: A "Life" applet Container ( Applet ) Containers ( Panel s) Component ( Canvas ) Components ( Button s) Components ( Label s) Components ( TextField s)

10 Applets An application has a public static void main(String args[ ]) method, but an applet usually does not An applet's main method is in the Browser To write an applet, you extend JApplet and override some of its methods The most important methods are init( ), start( ), and paint(Graphics g)

11 To create an applet public class MyApplet extends JApplet { … } The only way to make an applet is to extend Applet or JApplet You can add components to the applet The best place to add components is in init( ) You can paint directly on the applet, but… …it’s better to paint on a contained component Do all painting from paint(Graphics g)

12 Some types of components Label Button Checkbox Choice List Scrollbar TextField TextArea CheckboxGroup Checkbox

13 Creating components JLabel lab = new JLabel ("Hi, Dave!"); JButton but = new JButton ("Click me!"); JCheckBox toggle = new JCheckBox ("toggle"); JTextField txt = new JextField ("Initial text.", 20); JScrollbar scrolly = new JScrollbar (JScrollbar.HORIZONTAL, initialValue, bubbleSize, minValue, maxValue);

14 Adding components to the Applet class MyApplet extends JApplet { public void init () { getContentPane().add(lab); // or this.getContentPane().add(lab); getContentPane().add(but); getContentPane().add(toggle); getContentPane().add(txt); getContentPane().add(scrolly);...

15 Creating a JFrame When you create an JApplet, The size of the applet is determined by the HTML page The browser makes the applet visible When you write a GUI for an application, you need to do these things yourself public class MyApplication extends JFrame { void createMyGUI() {... add components... pack(); // compute the size and lay it out setVisible(true); // make the JFrame visible } }

16 Arranging components Every Container has a layout manager You do not directly control where components are placed; the layout manager does this for you The default layout for a Panel or JPanel is FlowLayout A JApplet is a Panel ; therefore, the default layout for a JApplet is FlowLayout The default layout for a JFrame is BorderLayout You can set the layout manager explicitly; for example, setLayout(new FlowLayout()); setLayout(new BorderLayout());

17 FlowLayout Use add( component ); to add to a component when using a FlowLayout Components are added left-to-right If no room, a new row is started Exact layout depends on size of Applet Components are made as small as possible FlowLayout is convenient but often ugly Note: The following code and screenshots use AWT Components rather than Swing Components I haven’t had a chance to make new screenshots The layout managers are identical, not just similar There is no real difference with Swing instead of AWT

18 Complete example: FlowLayout import java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); // default add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); } }

19 BorderLayout At most five components can be added If you want more components, add a Panel, then add components to it. setLayout (new BorderLayout()); add (new Button("NORTH"), BorderLayout.NORTH);

20 BorderLayout with five Button s public void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER); }

21 Complete example: BorderLayout import java.awt.*; import java.applet.*; public class BorderLayoutExample extends Applet { public void init () { setLayout(new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); }

22 Using a Panel Panel p = new Panel(); add(p, BorderLayout.SOUTH); p.add(new Button ("Button 1")); p.add(new Button ("Button 2"));

23 GridLayout The GridLayout manager divides the container up into a given number of rows and columns: new GridLayout( rows, columns ) All sections of the grid are equally sized and as large as possible

24 Complete example: GridLayout import java.awt.*; import java.applet.*; public class GridLayoutExample extends Applet { public void init () { setLayout(new GridLayout(2, 3)); add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } }

25 Making components active Most components already appear to do something-- buttons click, text appears To associate an action with a component, attach a listener to it Components send events, listeners listen for events Different components may send different events, and require different listeners

26 Listeners Listeners are interfaces, not classes class MyButtonListener implements ActionListener { An interface is a group of methods that must be supplied When you say implements, you are promising to supply those methods

27 Writing a Listener For a JButton, you need an ActionListener b1.addActionListener (new MyButtonListener ( )); An ActionListener must have an actionPerformed(ActionEvent) method public void actionPerformed(ActionEvent e) { … }

28 MyButtonListener public void init () {... b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); }

29 Listeners for JTextField s An ActionListener listens for someone hitting the Enter key An ActionListener requires this method: public void actionPerformed (ActionEvent e) You can use getText( ) to get the text A TextListener listens for any and all keys A TextListener requires this method: public void textValueChanged(TextEvent e)

30 Summary I: Building a GUI Create a container, such as JFrame or JApplet Choose a layout manager Create more complex layouts by adding JPanel s; each JPanel can have its own layout manager Create other components and add them to whichever JPanel s you like

31 Summary II: Building a GUI For each active component, look up what kind of Listener s it can have Create (implement) the Listener s often there is one Listener for each active component Active components can share the same Listener For each Listener you implement, supply the methods that it requires For applets, write the necessary HTML

32 Vocabulary AWT – The Abstract Window Toolkit provides basic graphics tools (tools for putting information on the screen) Swing – A much better set of graphics tools Container – a graphic element that can hold other graphic elements (and is itself a Component ) Component – a graphic element (such as a Button or a TextArea) provided by a graphics toolkit listener – A piece of code that is activated when a particular kind of event occurs layout manager – An object whose job it is to arrange Components in a Container

33 The End