Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
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.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
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.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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 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.
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: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of.
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.
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.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Java Swing.
A Quick Java Swing Tutorial
Chapter 13: Advanced GUIs and Graphics
Graphical User Interface
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
Graphical User Interface
Presentation transcript:

Swing Components

Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract Window Toolkit – Contents and shape are separated (MVC support) – Fine-grained control over the behavior and look and feel – Platform independent

Swing component inheritance hierarchy Component defines methods used in its subclasses (for example, paint and repaint ) Container collection of related components When using JFrame, add components to content pane (a Container) JComponent superclass to most Swing components

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 Every Swing class extends the corresponding AWT class For backward-compatibility reasons

Jcomponent features Pluggable look and feel Can look like different platforms, at run-time Shortcut keys (mnemonics) Direct access to components through keyboard Tool tips Describe component when mouse rolls over it

JComponent Methods setVisible(boolean mode) true means visible setToolTipText(String toolTip) mouse hover shows this text setForeground(Color foreColor) foreground color of the component setBackground(Color backcolor) background color of the component setOpaque(boolean mode) background is opaque if true and transparent if false setEnabled(boolean mode) enabled if true and can respond to user input

Jframe features Constructors: JFrame() no text in the title bar JFrame(String titlebartext) Methods: getContentPane() – returns the content pane object for the window setDefaultCloseOperation(int operation) sets the default operation when the user closes the “X” in the upper corner setSize(int width, int height) sets the window size in pixels setVisible(boolean mode) displays window if mode = true; hides if false

GUI application tasks Call constructor of the JFrame superclass Get an object reference to the content pane container ‐ GUI objects are added to this pane Set the layout manager to arrange GUI components in the window Instantiate each component Add each component to the content pane Set the window size Display the window

Example Import javax.swing.*; Import java.awt.BorderLayout; public class First { public static void main(String[] args) { JFrame frame = new JFrame("My First Frame"); // operation to do when the window is closed. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(new JLabel("I Love Swing"), BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }

JDialog JDialog is directly descended from the Dialog class JDialog contains a rootPane hierarchy including a contentPane All dialogs are modal, which means the current thread is blocked until user interaction with it has been completed. JOptionPane.showMessageDialog – reports something that has happened to the user JOptionPane.showConfirmDialog – asks for a confirming response e.g. yes, no, cancel JOptionPane.showInputDialog – prompt the user for input

JFileChooser javax.swing.JFileChooser: – Allows the the user to choose a file – Supports “open” and “save”: showOpenDialog(),showSaveDialog() Example JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) System.out.println("File: " + fc.getSelectedFile());

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

Border Layout Position must be specified, e.g. add (“North”, myComponent)

Box Layout The BoxLayout class puts components in a single row or column. It respects the components’ requested maximum sizes.

Flow Layout FlowLayout is the default layout manager for every J Panel. It simply lays out components from left to right, s tarting new rows if necessary

Grid Layout GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns

Menus Menu Bar – JMenuBar() – add( JMenu ) JMenuItem( String ) JMenuItem( String,int) Menu – JMenu( String ) – add( JMenuItem )

JLabel Labels – Provide text instructions on a GUI – Read-only text – Programs rarely change a label's contents – Class JLabel(subclass of JComponent) Methods – Can declare label text in constructor – myLabel.setToolTipText( "Text" ) – myLabel.setText( "Text" ) – myLabel.getText()

JButton Methods of class JButton – Constructors JButton myButton = new JButton("Label"); JButton myButton = new JButton("Label", myIcon ); – setRolloverIcon( myIcon ) Sets image to display when mouse over button Class ActionEvent getActionCommand returns label of button that generated event

JCheckBox When JCheckBox changes – ItemEvent generated Handled by an ItemListener, which must define itemStateChanged –Register handlers with With addItemListener Class ItemEvent – getStateChange Returns ItemEvent.SELECTED or ItemEvent.DESELECTED

JRadioButton Radio buttons – Have two states: selected and deselected – Normally appear as a group Only one radio button in group selected at time Selecting one button forces the other buttons off – Mutually exclusive options – ButtonGroup maintains logical relationship between radio buttons Class JRadioButton – Constructor JRadioButton( "Label", selected ) If selected true, JRadioButtoninitially selected

JList List – Displays series of items – may select one or more items Class JList –Constructor JList( arrayOfNames ) Takes array of Objects(Strings) to display in list – setVisibleRowCount( n ) Displays n items at a time Does not provide automatic scrolling