Metric Converter What layout do we use? What is the class hierarchy?

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
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.
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.
Metric Converter What layout do we use? What is the class hierarchy? What listeners do we use? The code for this application is on our class web-site.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
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.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
DAT602 Database Application Development Lecture 6 JAVA Swing.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Creating Buttons – Lesson 51 Creating Buttons Lesson 5.
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.
SWING IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
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.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
POS 407 Assignments Emilio Yanine. Workshop 3 Programming Assignment Implement FHiLo as an Applet, call it JHiLo2.java Modify applet to use multiple JPanels.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
JTree javax.swing.tree javax.swing.event. Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
For (int i = 1; i
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
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.
JSplitPane & JTabbedPane
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
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)
Event Handling CS 21a: Introduction to Computing I First Semester,
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
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.
CSE 205 Exam 2 Review By Cheri Thompson and Katherine Kincade.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
A Quick Java Swing Tutorial
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Java FX: Scene Builder.
Advanced Swing Trees.
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Timer class and inner classes
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Tonga Institute of Higher Education
Graphical User Interface
Presentation transcript:

Metric Converter What layout do we use? What is the class hierarchy? The code for this application is on our class web-site What layout do we use? What is the class hierarchy? What listeners do we use?

On-line Ordering Applet The code for this application is on our class web-site What layout do we use? What is the class hierarchy? What listeners do we use?

Simple Text Editor Allows simple text editing What layout do we use? The code for this application is on our class web-site Allows simple text editing What layout do we use? What is the class hierarchy? What listeners do we use?

Trees Graph: Collection of nodes and edges Nodes are the objects that hold data Edges are links between nodes Tree: A graph without cycles Root: A distinguished node at the top of the tree Purpose Trees and graphs are probably the most used data structures in Computer Science They are useful to represent Directory structures Discussion forums Simulation of weather, climate change, global positioning Relational databases

Lab Project The main frame has a border layout Create a GUI that displays and modify a tree The main frame has a border layout The North section has a JTabbedPane The Center section has a JScrollPane showing a JTree The South section has a label that displays user help messages

Sample JTree Methods More information is in the lab instructions JTree related classes JTree (Java tree class) DefaultTreeModel (Object that controls how tree displays) DefaultMutableTreeNode (nodes to hold data in the tree) Instantiate A Node for the tree: root = new DefaultMutableTreeNode(“name”); Tree Model: treeModel = new DefaultTreeModel(root); A JTree: tree = Jtree(treeModel); Add a node to the root, make it display, and highlight the new node root.add(new DefaultMutableTreeNode("child node"); tree.scrollPathToVisible(new TreePath(child.getPath())); treeModel.nodeStructureChanged(parent); tree.setSelectionPath( (newTreePath(node.getPath()))); Display tree: for (int r=0; r<tree.getRowCount()-1; r++) tree.expandRow(r); Collapse tree: for (int r=tree.getRowCount()-1;r>=0;r--) tree.collapseRow(r); More information is in the lab instructions

Additional JTree methods Path TreePath(node.getPath()); gets a path of nodes from root to node. Tree.scrollPathToVisible(path); makes a path visible on the display. treeModel.nodeStructureChanged(node); forces a node to be displayed. treeModel.removeNodeFromParent(node); removes a node from the tree. Node.getParent() gets the parent node. (DefaultMutableTreeNode)tree.getLastSelectedPathComponent() can be used to get the node which is selected Tree.setSelectionPath(new TreePath(node.getPath()) sets the selected node. Node.setUserObject(“new name”) changes the node’s text. treeModel.nodeChanged(node); to force a modified node to display Tree.expandPath(path) and Tree.collapse(path)

Sample JTree Code Instantiate and Attach a listener DefaultMutableTreeNode root = new DefaultMutableTreeNode(“Root”); DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); tree.addTreeSelectionListener(this); Initially fill tree with nodes int childName = 0; For (int childX=1; childX<=4; childX++) { child = new DefaultMutableTreeNode (“Node “ + childName++); root.add(child); for (int grandX=1; grandX<=4; grandX++) { grandChild = new DefaultMutableTreeNode (“Node “+childName++); child.add(grandChild); } }

A Tabbed Panel Instantiate a TabbedPane Add a panel to the tabbed pane JTabbedPane tabs = new JTabbedPane(); Add a panel to the tabbed pane JPanel treeChanges = new TreeChangePanel(); tabs.addTab(“Modify”, treeChanges); Set which pane shows initially tabs.setSelectedIndex(0);

Containers Class Signature line Public class TreeChangePanel implements ActionListener Access to the JTree and other instance variables Alternative1: Pass references to the constructor Alternative 2: Nest Panel classes within the main class Set Layout in the TreeChanges class setLayout(new BoxLayout(this, BoxLayout,X_AXIS) ); Create and add the buttons JButton addButton = new JButton("Add"); add(addButton); Attach the listener addActionListener(this);

The ActionListener Signature line Determine which button was clicked public void actionPerformed(ActionEvent ae) Determine which button was clicked Alternative 1: if (ae.getSource()==addButton) Alternative 2: JButton button = (JButton)ae.getSource(); if (button.getText()=="Add") Event specific logic (for the 'add' option) Set error label if a tree node is not selected Create a new node Add the new node to the node that is selected Set the path to the new node visible Indicate that the tree structure changed Select the node just added

TreeSelectionListener This listener responds to changes in the JTree structure The valueChanged method handles the event It's purpose is to set the label field to the node that the user selects The initial version in the lab instructions can trigger a NullPointeException if there is no node selected How do we get around this? If there is no last selected path component, don't try to call the toString() method.