Advanced Swing Lists.

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Made with love, by Zachary Langley Applets The Graphics Presentation.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
COSC 3461: Module 8 Model-View-Controller (MVC) Architecture.
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
13-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
© Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers.
Object-Oriented Analysis and Design
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
14-Jul-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
C++ fundamentals.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Design Patterns and Graphical User Interfaces Horstmann ,
MVC CompSci 230 S Software Construction. MVC Architecture  A typical application includes software to  maintain application data,  document text.
DAT602 Database Application Development Lecture 6 JAVA Swing.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
Java SE 8 for Programmers, Third Edition
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
3461 Model-View Controller Advanced GUI concepts.
Creating Your Own Widgets CompSci 230 S Software Construction.
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
Object-Oriented Software Engineering Using Threads and simple Animation.
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.
Advanced Swing Custom Data Models and Cell Renderers.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Chapter 8: Understanding Collections Textbook: Chapter 4.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
Creating Your Own Widgets
A First Look at GUI Applications Radio Buttons and Check Boxes
Lecture 16 More GUI programming
Advanced Swing Trees.
GUI Components Part II.
CompSci 280 S Introduction to Software Development
Advanced Swing Tables.
THURSDAY, OCTOBER 17 IN LAB
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Composite Pattern Context:
EE 422C Sets.
Collections Framework
Constructors, GUI’s(Using Swing) and ActionListner
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Advanced ProgramMING Practices
Computer Science 209 The Adapter Pattern.
CE 221 Data Structures and Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Advanced ProgramMING Practices
Model, View, Controller design pattern
Presentation transcript:

Advanced Swing Lists

Contents List Models Inserting or Removing Values Rendering Values

I. List Models The list component uses the model-view- controller design pattern to separate the visual appearance from the underlying data. The JList class is responsible for the visual appearance of the data: It actually knows very little about how the data are stored All it knows is that it can retrieve the data through some object that implements the ListModel interface.

The ListModel interface public interface ListModel { int getSize(); Object getElementAt(int i); void addListDataListener(ListDataListener l); void removeListDataListener(ListDataListener l); } The JList Gets a count of elements Retrieves each one of the elements. Adds itself as a ListDataListener. That way, if the collection of elements changes, the JList gets notified so that it can repaint itself.

The ListModel interface This turns out to be easy to implement. The tedious part, adding and removing listeners, has been done for us in the AbstractListModel class, which we extend. We only need to supply the getSize and getElementAt methods.

A Very Long List A list of all three-letter words There are 26 x 26 x 26 = 17,576 three-letter combinations. Rather than storing all these combinations, we recompute them as requested when the user scrolls through them.

II. Inserting or Removing Values The DefaultListModel class implements the ListModel interface and manages a collection of objects. Construct a DefaultListModel object, fill it with the initial values, and associate it with the list. DefaultListModel model = new DefaultListModel(); model.addElement("quick"); model.addElement("brown"); . . . JList list = new JList(model);

Now you can add or remove values from the model object. The model object then notifies the list of the changes, and the list repaints itself. model.removeElement("quick"); model.addElement("slow");

III. Rendering Values You can easily represent your list values with any drawing. Install a list cell renderer into the JList object for all custom drawing by calling setCellRenderer. A list cell renderer is any class that implements the following interface: interface ListCellRenderer { Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus); }

getListCellRendererComponent This method is called for each cell. It returns a component that paints the cell contents. The component is placed at the appropriate location whenever a cell needs to be rendered. One way to implement a cell renderer is to create a class that extends JComponent.

class MyCellRenderer extends JComponent class MyCellRenderer extends JComponent implements ListCellRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){ ... return this; } public void paintComponent(Graphics g) { // paint code goes here } public Dimension getPreferredSize() { // size measurement code goes here } // instance fields }