Lecture 27 Creating Custom GUIs

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

User Interfaces II GUI – Awt, Swing, Web
Graphical User Interfaces
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.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
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.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
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 
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Object Oriented programming Instructor: Dr. Essam H. Houssein.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
CompSci 280 S Introduction to Software Development
Graphical User Interfaces
Provision for GUIs in Java
A First Look at GUI Applications
Java Swing.
Provision for GUIs in Java
Lecture 28 Concurrent, Responsive GUIs
A Quick Java Swing Tutorial
Ellen Walker Hiram College
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Timer class and inner classes
GUIS: Graphical User Interfaces
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Graphical User Interface
Presentation transcript:

Lecture 27 Creating Custom GUIs (D&D 23)

Summary of Previous Lecture Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Waiting state Notify NotifyAll Producer/Consumer Relationship Concurrent Collections ArrayBlockingQueue synchronizedList() Issues with Threading Deadlock Starvation

Assignment – Simulation Time Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Every second the Police Units should update their status and location, based on what their current status is. This will happen 60 times in the simulation. The above loop will be executed once per second, 60 times. You may use this in your assignment! You just need to determine what logic should be added inside the loop. Don’t forget to shutdown your ExecutorService, and consider if any threads are still running when you try to output data!

Assignment – Move Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

Assignment – Move Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary In moveToPoint we are moving in the X direction first. If it reaches the X co-ordinate required, then we move in the Y direction instead. This moveToPoint method can be reused for all the different moves required by a Police Unit. Pass in a destination Point (Java class) and number of units to move. If moveToPoint returns true it means the destination was reached and we can update the status accordingly. If it returns false, then the destination was not reached.

Java Swing Components Things such as buttons, checkboxes, text fields, lists, images, menus, etc. that we find in GUIs are known as components. The Java Swing classes know two types of components: Heavyweight components: Like AWT components, they use operating system specific code in the background and are only partially written in Java. Lightweight components: Written entirely in Java. Most Swing components are lightweight. The only heavyweight Swing component we are focusing on is the JFrame. Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

Java Swing vs AWT Some basic Swing components have counterparts in AWT, e.g., a JButton is the Swing version of a Button. Swing has more components than AWT. We will use only Swing components due to their lightweight nature. They have effectively replaced AWT as the choice for creating custom Graphical User Interfaces. As Swing components are entirely built by the JVM they do not require OS code. This means they are platform independent and will look similar across multiple operating systems. AWT will be dependent on the OS that is being used to run the program. We will still need to understand some AWT methods that are used with Swing components. Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

Java Swing Classes Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

GUI Elements Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Windows: These are also called ‘top-level containers’ and represent the initial window created to hold Swing elements inside it. The Swing JFrame is and example of this. Components: GUI Widgets that can be placed inside containers. These are the elements that we use to view data in the GUI and interact with the GUI. E.g. JButton, JLabel, JTextArea, JTable. Containers: Logical grouping of swing components. These can have separate default layouts applied to arrange the order of swing components inside the container.

GUI Elements Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary JButton JLabel JTable The blue area with the top bar represents the JFrame. This contains multiple JPanels (Orange areas) which in turn are used to organize their contained Swing Components logically. Our GUI should have the structure Window->Container->Component. Specifically with Swing this is JFrame->JPanel->JComponent

Creating a JFrame Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We can design our own Frame by extending the JFrame class. There are a variety of methods that can be used to customize the style of the frame. setLayout(): Set the layout of the frame. This will organize how the containers and components fit inside the frame. setSize(): Determines the original size of the frame setDefaultCloseOperation(): We are setting this to terminate the program upon closing the JFrame

Creating a JPanel Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We can design our own Panel by extending the JPanel class. We can customize this like a frame, with methods like setBorder(). We can also create custom Components, and use the add() method to add them to the Panel. In this simple example we are adding a simple JTextArea component to our Panel.

Creating our GUI We can now create our GUI in the main thread. Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We can now create our GUI in the main thread. Instantiate the SimulationFrame object we defined earlier, passing in the desired name. Instantiate a CustomPanel object Add this to the ContentPane of the frame Finally call the setVisible(true) method on the frame

Creating our GUI Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary

Model-View-Controller (MVC) Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Controller Change State User input Change Display View Model Model has changed!

Model-View-Controller (MVC) Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Controller: This takes in the user input and interprets this to determine how the state of the data should be changed. For our ambulance example, this is the execution of our Ambulance objects on their threads to change their state. Model: The model holds all of the data and its state. In our Ambulance example, this is the List<Ambulance> that stores every Ambulance that we have created. View: This is the GUI that we create that displays the model that we have created in the back end. The view and model don’t have access to the logic of each other, but can pass this information through an interface.

Creating a JTable Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We will use the MVC pattern to establish a JTable that represents the current state of all the Ambulances. The important method to highlight when creating the JTable is the setModel() method. This defines what data will be in the table, and we must create our own custom model class!

Creating a Table Model Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary To create a table model we will extend the AbstractTableModel class which defines a set of methods that we need to override to create a functioning model. getColumnCount() getRowCount() getValueAt(int row, int col) getColumnName(int col) This table model can then be used in the setModel method in our JTable to apply data to the GUI.

Creating a JTable Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We pass in our List of Ambulance objects that we want to show in the GUI table. We also create a String[] to define the column names.

JTable getValueAt() Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary arg0 refers to the row, with each row containing an Ambulance record. arg1 refers to the column, with each row containing a different type of data (i.e ID,Location,Status,Patient).

Instantiating the JTable Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary This is very similar to creating our custom Panel from earlier. The only difference is our Panel now takes in a SimulationTableModel parameter. Instantiate our SimulationFrame Instantiate a SimulationTableModel with a list of Ambulances Instantiate a SimulationTablePanel with our model Add the Panel to the ContentPane of the frame Set the frame to visible

Problems! Assignment Java Swing/ AWT Swing Elements MVC Pattern JTable Summary We are creating our GUI on the main thread. If there are also calculations being done on this thread, it will make the GUI slow and unresponsive! While our ‘Model’ is being changed while the simulation is running, there is no communication between our ‘Model’ and ‘View’. When we click on data, or arrange the columns, we notice that some data is updated. This is because we are Notifying the View to update. How can we get this to happen when the data is changed? We will look at solving these issues in the next lecture

Summary Covered common difficulties with Assignment 2 Java Swing/ AWT Swing Elements MVC Pattern JTable Summary Covered common difficulties with Assignment 2 Recap of Java GUI creation AWT (Abstract Window Toolkit) Swing Object hierarchy and layout of Swing elements JFrame JPanel JComponent Model View Controller (MVC) pattern Creating a custom JTable