1.  Container  JComponent  AbstractButton  JButton  JMenuItem  JCheckBoxMenuItem  JMenu  JRadioButtonMenuItem  JToggleButton  JCheckBox  JRadioButton.

Slides:



Advertisements
Similar presentations
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
Advertisements

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 program.
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
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.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
 What are the basic GUI components we’ve learned so far? › JFrame › JPanel › JButton › JLabel › JTextArea › JTextField › JCheckBox › JRadioButton › Paint.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©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.
Advanced Java Class GUI, part 2. JComponent methods addXXXListener(XXXListener) repaint() – [optional arguments: delay and coordinates of sub-area to.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
CS3 - AWT/Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Creating Graphical User Interfaces (GUIs) in Java CSE301 University of Sunderland Harry R Erwin, PhD.
1 Introduction to JFC/Swing Alex Chaffee jGuru Training by the MageLang Institute
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.
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.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
CSE 219 Patterns in Programming More Design Patterns.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
CSE1030-HR GUI The Big Picture Building the View Building the Controller Separating the Concerns Going Further.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Swing / Session1 / 1 of 30 Module Introduction Distributed Computing in Java.
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.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
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.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
1CS480: Graphical User Interfaces. Dario Salvucci, Drexel University. Lecture 3: Layout Basics.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
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.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Chap 7. Building Java Graphical User Interfaces
MSIS670: Object-Oriented Software Engineering
IFS410: Advanced Analysis and Design
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Presentation transcript:

1

 Container  JComponent  AbstractButton  JButton  JMenuItem  JCheckBoxMenuItem  JMenu  JRadioButtonMenuItem  JToggleButton  JCheckBox  JRadioButton 2

 JComponent  JComboBox  JLabel  JList  JMenuBar  JPanel  JPopupMenu  JScrollBar  JScrollPane 3

 JComponent  JTextComponent  JTextArea  JTextField  JPasswordField  JTextPane  JHTMLPane 4

 FontChooser  JColorChooser  JDesktopIcon  JDirectoryPane  JFileChooser  JImagePreviewer  JInternalFrame  JLayeredPane  JDesktopPane  JOptionPane  JProgressBar  JRootPane  JSeparator  JSlider  JSplitPane  JTabbedPane  JTable  JToolBar  JToolTip  JTree  JViewport 5

 A fixed-size image or glyph  Can be used with almost all components (e.g. JButton)  Icon is an interface that any class can implement  Icon used over Image because Image is asynchronously loaded and not serializable 6

7

 All subclass Window, not JComponent  Not lightweight, have peer  Components added to content pane  RootPaneContainer interface - container delegate 8

 No longer add components directly to top level containers  aFrame.add (new Button (“Help”));  Add to “content pane”  aJFrame.getContentPane().add (…);  Layout manager too - default BorderLayout  JDialog, JFrame, JWindow, JApplet, JInternalFrame 9

public class FrameTester { public static void main (String args[]) { JFrame f = new JFrame ("JFrame Example"); Container c = f.getContentPane(); c.setLayout (new FlowLayout()); for (int i = 0; i < 5; i++) { c.add (new JButton ("No")); c.add (new Button ("Batter")); } c.add (new JLabel ("Swing")); f.setSize (300, 200); f.show(); } 10

 When user selects window manager Close option for JFrame, has default behavior  Frame did nothing  JFrame hides itself  setDefaultCloseOperation (operation)  DO_NOTHING_ON_CLOSE  HIDE_ON_CLOSE  DISPOSE_ON_CLOSE  No EXIT_ON_CLOSE operation 11

 If using Swing components in an applet, subclass JApplet, not Applet  JApplet is a subclass of Applet  Sets up special internal component event handling, among other things  Can have a JMenuBar  Default LayoutManager is BorderLayout 12

 Standard dialog boxes  Yes, No, Cancel - or custom prompts  Message  Input  Anything goes (you can specify everything)  All dialogs are modal - blocks current thread  String response = JOptionPane.showInputDialog(this, "Enter input:"); 13

14

 JLabel - like Label  Still single line of text  Also supports Icon, Border,  Position text/icon in 9 areas, vs. 3 alignments  Also position text/icon relative to each other  JButton - like Button  Still single line of text  Also supports Icon, positioning,... 15

 JPanel - like Panel  Double-buffered (no JCanvas)  JCheckBox - like Checkbox  JRadioButton for mutual exclusion group  Grouped with ButtonGroup, not CheckboxGroup  JToggleButton - no AWT equivalent  Provides a “stay pressed” state  Great for tool bars 16

 JComboBox - like Choice  Editable - setEditable(boolean)  Auto-initialize from array  JComboBox jc = new JComboBox (aStringArray);  JList - like List  Auto-initialize from array  Scrolling not directly supported  Must put in JScrollPane 17

 JScrollPane - like ScrollPane  Scrolling component set in constructor or  Container delegate  Added to viewport / getViewPort().add()  Can place objects in inner four corners, column headers or row headers  Tables automatically use column header area 18

 JTextField - like TextField  Supports text justification  JPasswordField for passwords  Cannot clear/unset echo character  JTextArea - like TextArea  JTextPane - styled text support  JEditorPane - lightweight HTML/RTF editor/viewer 19

 JScrollBar - like Scrollbar  JSlider - Scrollbar for picking values  Display major / minor ticks  Associate labels with ticks 20

JSlider right, bottom; right = new JSlider(JSlider.VERTICAL, 1, 9, 3); Hashtable h = new Hashtable(); h.put (new Integer (1), new JLabel("Mercury")); h.put (new Integer (2), new JLabel("Venus"));... h.put (new Integer (9), new JLabel("Pluto")); right.setLabelTable (h); right.setPaintLabels (true); right.setInverted (true); bottom = new JSlider(JSlider.HORIZONTAL, 0, 100, 25); bottom.setMajorTickSpacing (10); bottom.setPaintLabels (true); 21

 In JComponent class hierarchy  JMenuBar - MenuBar (JFrame.setJMenuBar)  JMenu - Menu  JMenuItem - MenuItem  JCheckBoxMenuItem - CheckboxMenuItem  JRadioButtonMenuItem - no AWT  Group with ButtonGroup  JSeparator - menu separator  added by addSeparator 22

 JPopupMenu - like PopupMenu  Added addSeparator method 23

 Displays progress of operation  Can be used like a gauge  Usage:  Initialize JProgressBar progressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(numberSubOperations);  Go progressBar.setValue(progressBar.getMinimum()); for (int i = 0; i < numberSubOperations; i++) { progressBar.setValue(i); performSubOperation(i); } 24

 Context-sensitive text string that pops up when mouse rests over a particular object  JToolTip class supports this  Rarely used  Use setToolTipText method of JComponent  Singleton ToolTipManager manages tool tip operations 25

 Display components in single row/column  Can float or dock  Can contain any component  Best if all the same, or similar type  Consider using JToggleButton  Has addSeparator method 26

 Tabbed panel control  Similar to using CardLayout with buttons for selecting cards  Use addTab to add components/panels 27

 Allows user-controlled resizing of two components  Can move divider programmatically with setDividierLocation  int parameter  absolute position  float parameter  percentage 28

 Basically, a JPanel with a default layout manager of BoxLayout  You specify direction  Offers non-visual components for spacing/stretching  Glue and Struts 29

 Arranges components along either x or y axis in the order added  Unlike AWT layout managers, components’ positions and sizes may be specified separately  Along non-primary axis, makes all components as tall/wide as tallest/widest component 30

 Basically, a JPanel with a default layout manager of BoxLayout  You specify direction  Offers Glue and Struts for spacing 31

 Used by JScrollPane  Not created directly 32

 Used by JViewport  Not used directly 33

34

35

36

 Listener classes in support of each event  Each event has source(s) within Swing  Class EventListenerList available to maintain list of all listeners  Responsibility of class maintaining list to provide type safety, and routine to notify all listeners  Inherit Component/Container 1.1 events 37

 Sort of a button and a toolbar icon and an Action Event Listener rolled into one  Action interface extends ActionListener  For when multiple controls need same behavior  AbstractAction class Action implementation  Manages list of controls listening to action  Adding Actions supported by JMenu, JPopupMenu, and JToolBar 38

 You can add an Action to a toolbar  The toolbar makes a button for it and asks the Action what icon to use  Helps separate behavior from UI  Easier to script or change program logic 39

 Define Action class CutAction extends AbstractAction { public CutAction () { super (“Cut”, new ImageIcon(“Scissors.gif”); } public void actionPerformed (ActionEvent e) { System.out.println ("Selected: " + getValue (Action.NAME)); }  Add to multiple places (Action a = new MyAction(...);)  aJMenu.add (a) / aJToolBar.add (a) / …  Disable a, disables menu, toolbar,... 40

 TextAction extends AbstractAction  Ask text component how to handle operation  Action actions[] = aJTextComp.getActions();  Find Action to perform operation  Search through array  Associate Action to component  addActionListener(...) 41

 Get Action List Hashtable commands = new Hashtable(); Action[] actions = jt.getActions(); for (int i = 0; i < actions.length; i++) { Action a = actions[i]; commands.put(a.getValue(Action.NAME), a); }  Find action / associate to component JButton cut = new JButton("Cut"); Action cutIt = (Action)commands.get (DefaultEditorKit.cutAction); cut.addActionListener (cutIt); 42

 KeyStroke represents a keystroke KeyStroke stroke = KeyStroke.getKeyStroke (KeyEvent.VK_J, ActionEvent.ALT_MASK, true); // ALT-J  Associate to JComponent jb.registerKeyboardAction (new MyActionListener(), stroke, JComponent.WHEN_FOCUSED);  When keystroke happens within component, action happens  Conditions: WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 43

44

45

 Model - Defines state of system  Underlying logical representation  View - Defines how user sees model  Visual representation of data in model  Controller - Defines how user interacts with model  User interaction handler  Model changes  Views notified 46

 Separation of Model and View  Multiple views of the same model  Model not affected when view changed  View uses Controller to specify response mechanism  MVC is not only for GUI components 47

48

 Swing uses MVC variation  View/Controller combined into delegate  View/Controller communication typically complex; delegate simplifies  Example: Checkbox  Has state true/false in Model  Screen corresponds to Delegate-View  Mouse clicks are handled by Delegate-Controller, sending input to Model 49

50

51

 Data Model - TreeModel  default: DefaultTreeModel  getChild, getChildCount, getIndexOfChild, getRoot, isLeaf  Selection Model - TreeSelectionModel  View - TreeCellRenderer  getTreeCellRendererComponent  Node - DefaultMutableTreeNode 52

 No longer just text  Can display Icon  Can change display line when selected  Data Model - ListModel  default: DefaultListModel  getSize / getElementAt (position)  View - ListCellRenderer  getListCellRendererComponent() 53

 Data Model - ComboBoxModel  Extends ListModel  get/set SelectedItem  Same cell renderer as JList 54

 Can just create JTable from data[][] and columnName[] and not worry about anything else  Data Model - TableDataModel  default: DefaultTableModel  getRowCount, getValueAt, setValueAt, getColumnCount, getColumnName,...  View - JTable  Contains JTableColumns 55

56

57