© 2001-2002 Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers.

Slides:



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

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Io package as Java’s basic I/O system continue’d.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Design Patterns and Graphical User Interfaces Horstmann ,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
DAT602 Database Application Development Lecture 6 JAVA Swing.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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,
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CSE 219 Patterns in Programming More Design Patterns.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.
More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
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.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Advanced Swing Custom Data Models and Cell Renderers.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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,
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
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.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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:
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CSE 205 Exam 2 Review By Cheri Thompson and Katherine Kincade.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
GUIs and Events Rick Mercer.
Advanced Swing Trees.
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
Advanced Swing Lists.
A Quick Java Swing Tutorial
Chapter 13: Advanced GUIs and Graphics
עקרונות תכנות מונחה עצמים תרגול 8: MVC
A Quick Java Swing Tutorial
Advanced GUIs and Graphics
Presentation transcript:

© Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers

Advanced Swing2 Agenda Building a simple static JList Adding and removing entries from a JList at runtime Making a custom data model –Telling JList how to extract data from existing objects Making a custom cell renderer –Telling JList what GUI component to use for each of the data cells

Advanced Swing3 MVC Architecture Custom data models –Changing the way the GUI control obtains the data. Instead of copying data from an existing object into a GUI control, simply tell the GUI control how to get at the existing data. Custom cell renderers –Changing the way the GUI control displays data values. Instead of changing the data values, simply tell the GUI control how to build a Swing component that represents each data value. Main applicable components –JList –JTable –JTree

Advanced Swing4 JList with Fixed Set of Choices Build JList: pass strings to constructor –The simplest way to use a JList is to supply an array of strings to the JList constructor. Cannot add or remove elements once the JList is created. String options = { "Option 1",..., "Option N"}; JList optionList = new JList(options); Set visible rows –Call setVisibleRowCount and drop JList into JScrollPane optionList.setVisibleRowCount(4); JScrollPane optionPane = new JScrollPane(optionList); someContainer.add(optionPane); Handle events –Attach ListSelectionListener and use valueChanged

Advanced Swing5 Simple JList: Example Code public class JListSimpleExample extends JFrame {... public JListSimpleExample() { super("Creating a Simple JList"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6"}; sampleJList = new JList(entries); sampleJList.setVisibleRowCount(4); sampleJList.addListSelectionListener (new ValueReporter()); JScrollPane listPane = new JScrollPane(sampleJList);... }

Advanced Swing6 Simple JList: Example Code (Continued) private class ValueReporter implements ListSelectionListener { /** You get three events in many cases -- one for the * deselection of the originally selected entry, one * indicating the selection is moving, and one for the * selection of the new entry. In the first two cases, * getValueIsAdjusting returns true; thus, the test * below since only the third case is of interest. */ public void valueChanged(ListSelectionEvent event) { if (!event.getValueIsAdjusting()) { Object value = sampleJList.getSelectedValue(); if (value != null) { valueField.setText(value.toString()); }

Advanced Swing7 Simple JList: Example Output

Advanced Swing8 JList with Changeable Choices Build JList: –Create a DefaultListModel, add data, pass to constructor String choices = { "Choice 1",..., "Choice N"}; DefaultListModel sampleModel = new DefaultListModel(); for(int i=0; i<choices.length; i++) { sampleModel.addElement(choices[i]); } JList optionList = new JList(sampleModel); Set visible rows –Same: Use setVisibleRowCount and a JScrollPane Handle events –Same: attach ListSelectionListener and use valueChanged Add/remove elements –Use the model, not the JList directly

Advanced Swing9 Changeable JList: Example Code String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6"}; sampleModel = new DefaultListModel(); for(int i=0; i<entries.length; i++) { sampleModel.addElement(entries[i]); } sampleJList = new JList(sampleModel); sampleJList.setVisibleRowCount(4); Font displayFont = new Font("Serif", Font.BOLD, 18); sampleJList.setFont(displayFont); JScrollPane listPane = new JScrollPane(sampleJList);

Advanced Swing10 Changeable JList: Example Code (Continued) private class ItemAdder implements ActionListener { /** Add an entry to the ListModel whenever the user * presses the button. Note that since the new entries * may be wider than the old ones (e.g., "Entry 10" vs. * "Entry 9"), you need to rerun the layout manager. * You need to do this before trying to scroll * to make the index visible. */ public void actionPerformed(ActionEvent event) { int index = sampleModel.getSize(); sampleModel.addElement("Entry " + (index+1)); ((JComponent)getContentPane()).revalidate(); sampleJList.setSelectedIndex(index); sampleJList.ensureIndexIsVisible(index); }

Advanced Swing11 Changeable JList: Example Output

Advanced Swing12 JList with Custom Data Model Build JList –Have existing data implement ListModel interface getElementAt –Given an index, returns data element getSize –Tells JList how many entries are in list addListDataListener –Lets user add listeners that should be notified when an item is selected or deselected. removeListDataListener –Pass model to JList constructor Set visible rows & handle events: as before Add/remove items: use the model

Advanced Swing13 Custom Model: Example Code public class JavaLocationListModel implements ListModel { private JavaLocationCollection collection; public JavaLocationListModel (JavaLocationCollection collection) { this.collection = collection; } public Object getElementAt(int index) { return(collection.getLocations()[index]); } public int getSize() { return(collection.getLocations().length); } public void addListDataListener(ListDataListener l) {} public void removeListDataListener(ListDataListener l) {} }

Advanced Swing14 Actual Data public class JavaLocationCollection { private static JavaLocation[] defaultLocations = { new JavaLocation("Belgium", "near Liege", "flags/belgium.gif"), new JavaLocation("Brazil", "near Salvador", "flags/brazil.gif"), new JavaLocation("Colombia", "near Bogota", "flags/colombia.gif"),... };... } JavaLocation has toString plus 3 fields –Country, comment, flag file

Advanced Swing15 JList with Custom Model: Example Code JavaLocationCollection collection = new JavaLocationCollection(); JavaLocationListModel listModel = new JavaLocationListModel(collection); JList sampleJList = new JList(listModel); Font displayFont = new Font("Serif", Font.BOLD, 18); sampleJList.setFont(displayFont); content.add(sampleJList);

Advanced Swing16 JList with Custom Model: Example Output

Advanced Swing17 JList with Custom Cell Renderer Idea –Instead of predetermining how the JList will draw the list elements, Swing lets you specify what graphical component to use for the various entries. Attach a ListCellRenderer that has a getListCellRendererComponent method that determines the GUI component used for each cell. Arguments to getListCellRendererComponent –JList: the list itself –Object: the value of the current cell –int: the index of the current cell –boolean: is the current cell selected? –boolean: does the current cell have focus?

Advanced Swing18 Custom Renderer: Example Code public class JavaLocationRenderer extends DefaultListCellRenderer { private Hashtable iconTable = new Hashtable(); public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean hasFocus) { JLabel label = (JLabel)super.getListCellRendererComponent (list,value,index,isSelected,hasFocus); if (value instanceof JavaLocation) { JavaLocation location = (JavaLocation)value; ImageIcon icon = (ImageIcon)iconTable.get(value); if (icon == null) { icon = new ImageIcon(location.getFlagFile()); iconTable.put(value, icon); } label.setIcon(icon);... return(label); }}

Advanced Swing19 Custom Renderer: Example Output

Advanced Swing20 Summary Simple static JList –Pass array of strings to JList constructor Simple changeable JList –Pass DefaultListModel to JList constructor. Add/remove data to/from the model, not the JList. Custom data model –Have real data implement ListModel interface. –Pass real data to JList constructor. Custom cell renderer –Assign a ListCellRenderer –ListCellRenderer has a method that determines the Component to be used for each cell

© Marty Hall, Larry Brown Web core programming 21 Questions?