A Quick Java Swing Tutorial

Slides:



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

Graphic User Interfaces Layout Managers Event Handling.
Introduction to JFC Swing Written by Adam Carmi. Agenda About JFC and Swing Pluggable Look and Feel Swing Components Borders Layout Management Events.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
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.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 17 Graphical User Interfaces Swing I.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
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
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.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
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.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Graphical User Interfaces. Graphical input and output with JOptionPane.
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.
Java Swing One of the most important features of Java is its ability to draw graphics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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.
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.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
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:
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.
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
Graphical User Interfaces
A First Look at GUI Applications
Java Swing.
Provision for GUIs in Java
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Timer class and inner classes
תכנות מכוון עצמים בשפת JAVA
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Graphical User Interface
Presentation transcript:

A Quick Java Swing Tutorial

Introduction Swing – A set of GUI classes Highlights Part of the Java's standard library Much better than the previous library: AWT Abstract Window Toolkit Highlights A rich set of widgets Widget: Any GUI element (also called: components) Contents and shape are separated (MVC support)‏ Fine-grained control over the behavior and look and feel Platform independent Isolates the programmer from the operating system's GUI

Swing Components Containers Contain and manage other components. Top Level/Internal Examples: JFrame (Top Level), JScrollPane, JPanel. Basic controls Atomic components Used for showing ouput and/or getting some input Inherits JComponent Examples: JButton, JLabel, JTextArea, JTable, Jlist Usually every Swing class extends the corresponding AWT class For backward-compatibility reasons

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

Build from bottom up Create: Add: (bottom up) Frame Panel Components Listeners Add: (bottom up) listeners into components components into panel panel into frame Listener JButton JLabel JPanel JFrame

Top Level Containers: JFrame javax.swing.JFrame: Top-level window with a title and a border. Usually used as a program's main window 6 6

Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } press me

Top Level Containers: JDialog javax.swing.JDialog: More simple and limited than frames Typically used for showing a short message on the screen Also has a border and a title bar May have an owner If the owner is invisible the dialog will also be invisible Use the static method of JoptionPane to show standard dialog boxes: JOptionPane.showMessageDialog(null, "4+2=6");

JOptionPane examples 2 showConfirmDialog analogous to a System.out.print that prints a question, then reading an input value from the user (can only be one of the provided choices) import javax.swing.*; class ConfirmDialogExample { public static void main(String[] args) { int choice = JOptionPane.showConfirmDialog(null, "Erase your hard disk?"); if (choice == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "Disk erased!"); } else { JOptionPane.showMessageDialog(null, "Cancelled."); }

Internal Containers Not Top level containers Can contain other non-top level components Examples: JScrollPane: Provides a scrollable view of its components JSplitPane: Separates two components JTabbedPane: User chooses which component to see

Containers - Layout Each container has a layout manager Determines the size, location of contained widgets. Setting the current layout of a container: void setLayout(LayoutManager lm) LayoutManager implementing classes: BorderLayout BoxLayout FlowLayout GridLayout

Swing Components

Event-driven Programming program's execution is indeterminate on-screen components cause events to occur when they are clicked / interacted with events can be handled, causing the program to respond, driving the execution thru events (an "event-driven" program)

Action events (ActionEvent) most common / simple event type in Swing represent an action occurring on a GUI component created by: button clicks check box checking / unchecking menu clicks pressing Enter in a text field etc.

Listening for events attach listener to component listener’s appropriate method will be called when event occurs (e.g. when the button is clicked) for Action events, use ActionListener