Advanced Programming in Java

Slides:



Advertisements
Similar presentations
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Advertisements

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Graphical User Interface(GUI)
Basics of GUI Programming Chapter 11 and Chapter 22.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
Graphical User Interface(GUI)
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Java Swing.
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Timer class and inner classes
GUIS: Graphical User Interfaces
Chapter 7 (Book Chapter 14)
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
GUI and Event-Handling Programming
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Advanced Programming in Java Sharif University of Technology Fall 2015

Agenda Graphical User Interface (GUI) GUI Components Event Handlers Inner Classes Anonymous Inner Classes Sharif University of Technology

Graphical User Interface (GUI) GUI presents a mechanism for interacting with application. a user-friendly mechanism What are other mechanisms? Console Applications File Web Applications … GUI is pronounced “GOO-ee” Sharif University of Technology

GUI Components GUIs are built from GUI components GUI component: sometimes called controls or widgets widget: short for window gadgets GUI component: an object with which the user interacts via the mouse or the keyboard Example? Button, Textbox, Menu, … Sharif University of Technology

Sharif University of Technology

Swing An AWT component’s behavior may be different between platforms. AWT was the early way for GUI in java Abstract Window Toolkit Java.awt package An AWT component’s behavior may be different between platforms. Swing is a newer approach In this presentation we review Swing GUI components javax.swing package Sharif University of Technology

JOptionPane JOptionPane.showInputDialog JOptionPane.showMessageDialog JOptionPane class has simple static methods for input and output Using Dialog boxes JOptionPane.showInputDialog Returns a String JOptionPane.showMessageDialog Shows a dialog box Sharif University of Technology

JOptionPane Sample String name = JOptionPane.showInputDialog("Enter your name:"); JOptionPane.showMessageDialog(null, "Salam "+name +"!"); Sharif University of Technology

Modal Dialog JOptionPane dialogs are called modal dialogs While the dialog is on the screen… the user cannot interact with the rest of the application Sharif University of Technology

JFrame We create a window containing Swing GUI components This window object is usually instance of JFrame or a subclass of JFrame JFrame provides the basic attributes and behaviors of a window a title bar at the top minimize and maximize buttons close button Sharif University of Technology

JFrame Example JFrame frame = new JFrame(); frame.setSize(300, 150); frame.setVisible(true); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); Sharif University of Technology

Swing GUI Components JButton JLabel JTextField JComboBox JRadioButton JMenu … Sharif University of Technology

JLabel and JButton JLabel label = new JLabel("Salam"); label.setToolTipText("Tooltip1"); frame.add(label); JButton button = new JButton("Click!"); button.setToolTipText("salam :-)"); frame.add(button); Sharif University of Technology

Other GUI Components JTextField textbox = new JTextField(10); frame.add(textbox); JComboBox combo = new JComboBox( new String[]{"Red", "Blue", "Green"}); frame.add(combo); Sharif University of Technology

Layout Management You must attach each GUI component to a container such as a JFrame. You typically must decide where to position each GUI component known as specifying the layout Java provides several layout managers They help you position components Sharif University of Technology

FlowLayout In FlowLayout layout manager: components are placed on a container from left to right in the order in which they’re added When no more components can fit on the current line continue on the next line Other layouts: GridLayout, BoxLayout, … Sharif University of Technology

Layouts Three ways to arrange components in a GUI Layout managers Simple and Fast Absolute positioning provides the greatest level of control GUI’s appearance. Visual programming in an IDE They generate codes They may use layout managers Sharif University of Technology

Absolute Positioning Set Container’s layout to null Specify the absolute position of each GUI component with respect to the upper-left corner of the Container by using Component methods setSize and setLocation or setBounds absolute positioning can be tedious unless you have a support by an IDE Sharif University of Technology

IDE Support Many IDEs provide GUI design tools You can specify a component’s exact size and location in a visual manner by using the mouse simplifies creating GUIs each IDE generates this code differently You should know the underlying codes Eclipse Plugin Sharif University of Technology

Extending JFrame You can also extend JFrame to create new frames… Sharif University of Technology

public class MyFrame extends JFrame{ JLabel label; JButton button; JTextField textbox; JComboBox combo; public MyFrame(){ super("Frame Title"); label = new JLabel("Salam"); label.setToolTipText("Label Tooltip"); add(label); button = new JButton("Click!"); button.setToolTipText("salam :-)"); add(button); textbox = new JTextField(10); add(textbox); combo = new JComboBox( new String[]{"Red", "Blue", "Green"}); add(combo); } Sharif University of Technology

Event Handling GUIs are event driven User interaction => events An Event drives the program to perform a task Some events: clicking a button typing in a text field selecting an item from a menu closing a window moving the mouse Sharif University of Technology

Event Handling (2) event handler : The code that performs a task in response to an event event handling : The overall process of responding to events Sharif University of Technology

Sharif University of Technology

Sharif University of Technology

Event Handlers button = new JButton("Click!"); ActionListener al = new ClickListener(); button.addActionListener(al); public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, “Salam!!!”); } Sharif University of Technology

Better Event Handlers I want to show a message-box According to the value of a component For example a text-field or a combo-box How can I do that? Inner Classes Sharif University of Technology

public class MyFrame extends JFrame{ public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, textbox.getText()); } JButton button; JTextField textbox; public MyFrame(){ button = new JButton("Click!"); button.addActionListener(new ClickListener()); add(button); textbox = new JTextField(10); add(textbox); Sharif University of Technology

Inner Classes

Class Types class FriendlyClass{ } public class OuterClass { private int value; public class Inner{ public void f(){ … Friendly Class Public class Inner Class Sharif University of Technology

Inner Classes Declared in another class Instantiated using a reference object of outer class Has access to this object The inner class can be static No reference object of outer class is needed No access to outer class is provided Sharif University of Technology

OuterClass.this is implicitly saved in inner object public class OuterClass { private int value = 2; class Inner{ public void innerMethod(){ OuterClass.this.value = 5; } public void outerMethod(){ Inner inner = new Inner(); inner.innerMethod(); public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); OuterClass.this is implicitly saved in inner object Sharif University of Technology

Why we need outer reference? public class OuterClass { private int value = 2; class Inner{ public void f(){ OuterClass.this.value = 5; } public static void main(String[] args) { OuterClass outer = new OuterClass(); OuterClass.Inner inner = outer.new Inner(); System.out.println(outer.value); inner.f(); Why we need outer reference? Sharif University of Technology

OuterClass.Inner in = new OuterClass.Inner(); class OuterClass { static class Inner{ public void f(){ System.out.println("f() invoked"); } public class MainClass { public static void main(String[] args) { OuterClass.Inner in = new OuterClass.Inner(); in.f(); Sharif University of Technology

Inner Class Specifiers static final Access Specifiers public private Friendly (no specifier) protected Sharif University of Technology

Anonymous Inner Class An inner class With no name Created once Used once No access to this class from any other place Once created and used Sharif University of Technology

Inner inner = new Inner() { public void innerMethod() { interface Inner{ void innerMethod();} public class OuterClass { private int value = 2; public void outerMethod(){ Inner inner = new Inner() { public void innerMethod() { OuterClass.this.value = 5; } }; inner.innerMethod(); public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); Sharif University of Technology

Anonymous Inner Class Usually implements an interface Or extends another class And Overrides some special method Main Application : Event Handlers Sharif University of Technology

public class MyFrame extends JFrame{ JButton button; JTextField textbox; public MyFrame(){ button = new JButton("Click!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, textbox.getText()); } }); add(button); textbox = new JTextField(10); add(textbox); Sharif University of Technology

Further Reading javaw Java Web Applications SWT Java Look and Feels Web Interface SWT Java Look and Feels Sharif University of Technology

Sharif University of Technology

Sharif University of Technology