GUI 1: JFC and Swing Basics OOP tirgul No. 4 2009.

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Object-Oriented Software Engineering Java with added Swing.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
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.
Java Swing Chris North cs3724: HCI. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
GUI Programming in Java
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
CSE1030-HR GUI The Big Picture Building the View Building the Controller Separating the Concerns Going Further.
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.
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.
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.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
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.
Java GUI COMP 401 Fall 2014 Lecture 16 10/20/2014.
Java Swing One of the most important features of Java is its ability to draw graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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)
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Module 13: Swing API Object Oriented Programming(Java)
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Events and Event Handling
A First Look at GUI Applications
University of Central Florida COP 3330 Object Oriented Programming
Java Swing.
A Quick Java Swing Tutorial
GUI Programming III: Events
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Graphical User Interface
Presentation transcript:

GUI 1: JFC and Swing Basics OOP tirgul No

Swing Packages javax.accessibilityjavax.swing.plafjavax.swing.text javax.swingjavax.swing.plaf.basicjavax.swing.text.html javax.swing.borderjavax.swing.plaf.metaljavax.swing.text.html.parser javax.swing.colorchooserjavax.swing.plaf.multijavax.swing.text.rtf javax.swing.eventjavax.swing.plaf.synthjavax.swing.tree javax.swing.filechooserjavax.swing.tablejavax.swing.undo

Hello Swing import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { //Create and set up the window JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Resize window to fit preferred size and subcomponents layouts frame.pack(); //Display the window frame.setVisible(true); }

Swing components Windows Panels Individual components Except top-level containers, all Swing components whose names begin with "J" descend from the JComponent class that extends the java.awt. Container class, which itself extends java.awt.Component.

Swing containment hierarchy  Composite design pattern  A containment hierarchy is a tree of components that has a top-level container as its root. Swing provides three generally useful top-level container classes: JFrame, JDialog & JApplet.  To appear onscreen, every GUI component must be part of a containment hierarchy.  Each GUI component can be contained only once.  Each top-level container has a content pane that contains (directly or indirectly) the visible components in that top-level container's GUI.

Anatomy of an Application GUI JPanel JButton JFrame JLabel Appearance Internal structure JFrame JPanel JButtonJLabel containers components

Example 2: HelloWorldSwing2 public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton button = new JButton("click me!"); JButton button = new JButton("click me!"); panel.add(button); panel.add(button); panel.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20)); panel.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20)); frame.add(panel); frame.pack(); frame.setVisible(true); }

Creating a GUI Component Create it Jbutton b = new JButton(“press me”); Configure it b.setText(“press me too”); Add it panel.add(b); Listen to it/bind it to other components … JButton

Java Beans JavaBeans enable developers to create components - self-contained, reusable software units that can be visually assembled into composite components Properties are the appearance and behavior characteristics of a bean that can be changed at design time. Builder tools introspect on a bean to discover its properties and expose those properties for manipulation by a process known as introspection Beans support introspection by adhering to naming conventions – specific rules for naming bean features Beans use events to communicate with other beans. A bean that is to receive events (a listener bean) registers with the bean that fires the event (a source bean).

Java Bean example public class MyBean { /** Creates a new instance of MyBean */ public MyBean() { } /** Holds value of property title. */ private String title; /** Getter for property title. Value of property title. */ public String getTitle() { return this.title; } /** Setter for property title. title New value of property title. */ public void setTitle(String title) { this.title = title; } } extends javax.swing.JComponent { protected void paintComponent(java.awt.Graphics g) { … } }

Swing controls JButton JCheckBox JComboBoxJList JRadioButton JSlider JSpinner JTextField JPasswordField JTextArea JTree JTable JLabel JProgressBar JSeparator JToolTip

Swing containers JPanel JScrollPane JSplitPane JTabbedPane JToolBar JInternalPane JLayeredlPane

JMenu Menu bar can be added to a top-level container, it is positioned outside the content pane.

Layout Managers Left to right, Top to bottom c n s ew FlowLayoutGridLayout BorderLayout none, programmer sets x,y,w,h null One at a time CardLayout GridBagLayout JButton

Layout Combinations NORTH JPanel : BorderLayoutSOUTHNORTH SOUTH JFrame JPanel: FlowLayout JButton JTextArea

Layout Combinations Panel panel1 = new JPanel(new FlowLayout()); panel1.add(new JButton("JButton")); JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel1,BorderLayout.NORTH); panel2.add(new TextArea(5,15),BorderLayout.SOUTH); frame.add(panel2);

Events and Listeners The Hollywood principle: “Don’t call us, we’ll call you” Observer design pattern – – Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. – The key objects in this pattern are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state.

Adding an Action Listener import java.awt.event.*; … JButton button = new JButton("click me!"); button.addActionListener(new ButtonActionListener()); … class ButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("command: " + e.getActionCommand()); } Adding a listener to the button : Defining the listener : command: click me! public interface ActionListener { void actionPerformed(ActionEvent e); }

More complex scenario Any number of event listener objects can listen for all kinds of events from any number of event source objects. Multiple listeners can register to be notified of events of a particular type from a particular source. Also, the same listener can listen to notifications from different objects. Each event is represented by an object that gives information about the event and identifies the event source.

MouseListener Inteface class MouseListener1 implements MouseListener{ public void mouseClicked(MouseEvent e) { System.out.println(e.getX()+","+e.getY()); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } But we only wanted mouseClicked() …

Mouse Adapter class MouseAdapter1 extends MouseAdapter { public void mouseClicked(MouseEvent e) { System.out.println(e.getX()+","+e.getY()); } An empty implementation of MouseListener

Some common listeners  component listener listens for changes in the component's size, position, or visibility.  focus listener listens for whether the component gained or lost the keyboard focus.  key listener listens for key presses; key events are fired only by the component that has the current keyboard focus.  mouse listener listens for mouse clicks, mouse presses, mouse releases and mouse movement into or out of the component's drawing area.  mouse-motion listener listens for changes in the mouse cursor's position over the component.  hierarchy listener Listens for changes to a component's containment hierarchy of changed events.  property change listeners listen for changes to bound properties and are used by several Swing components  vetoable change listeners are used by builder tools to listen for changes on constrained properties

Concurrency  Careful use of concurrency is particularly important to the Swing programmer.  A well-written Swing program creates a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing.  A Swing programmer deals with the following kinds of code executions: ◦ initial application code; ◦ event-handling, most code that interacts with the Swing framework must execute there; ◦ time-consuming background tasks.

Event Dispatcher Thread (EDT)  It is a background processing mechanism to process events from graphical user interface event queue.  These events are primarily update events that cause user interface components to redraw themselves, or input events from input devices such as the mouse or keyboard.  Swing uses a painting model in which all screen updates must be performed sequentially.  The event dispatching thread is the only valid way to update the state of visible user interface components.

SwingUtilities  Command design pattern  invokeLater and invokeAndWait ◦ take a single argument: the Runnable that defines the new task. ◦ invokeLater schedules the task and returns ◦ invokeAndWait waits for the task to finish before returning SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } });

Hello Swing revisited public class HelloWorldSwing { public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { } ); } JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true);

Summary  Swing  JavaBeans  Components  Containers  Layouts  Events  Concurrency Find out more: