More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Slides:



Advertisements
Similar presentations
Graphical User Interfaces
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
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.
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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
© Marty Hall, Larry Brown Web core programming 1 Advanced Swing Custom Data Models and Cell Renderers.
Graphical User Interfaces
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Introduction to Computation and Problem Solving Class 15: Constructing Interfaces with Swing Prof. Steven R. Lerman and Dr. V. Judson Harward 1.
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.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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,
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS Lecture 01 Frames and Components and events Lynda Thomas
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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 Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Topic 13 List Boxes. JList Generates an ListSelectionEvent Event monitored by ListSelectionListener ListSelectionListener invokes method valueChanged.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
Computer Science 209 GUIs Model/View/Controller Layouts.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
“Swing” - Constructing GUIs JButton JTextField. Layout Managers CENTERWEST CENTER BORDERLAYOUT this.setLayout(new BorderLayout()); this.add(displayCanvas,
Advanced Swing Custom Data Models and Cell Renderers.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUIs and Events Rick Mercer.
Java Graphics.
GUIs Model/View/Controller Layouts
Java Swing.
Graphical user interface with Swing by jose maria gonzalez pinto
Advanced Swing Lists.
Tim McKenna Layout Mangers in Java Tim McKenna
Chapter 13: Advanced GUIs and Graphics
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Panels & Layout Managers
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Advanced GUIs and Graphics
Presentation transcript:

More GUIs, events, static

model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere"); private JList guiViewOfList; // The view private DefaultListModel model; // the Model private void setLayout() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Do this almost always this.setSize(120, 180); // 120 pixels wide and 180 pixels tall this.setLocation(110, 50); // upper left corner // DefaultListModel and JList work together as model/view guiViewOfList = new JList (); guiViewOfList.setModel(model); // Adds two elements to the center (BorderLayout.CENTER is default), only see add(guiViewOfList); add(lineDisplay); What is wrong with this picture?

Layout and Sizing Issues Use BorderLayout for now If you have more than one element Set the preferred size of the components Add them to a new JPanel Add that JPanel to the center of the JFrame add(aPanel, BorderLayout.CENTER);

How to fix things? // Solution? Set the preferred size, // add both to a JPanel, then add one JPanel JPanel panel = new JPanel(); lineDisplay.setPreferredSize(new Dimension(150, 100)); guiViewOfList.setPreferredSize(new Dimension(50, 140)); guiViewOfList.setBackground(Color.CYAN); panel.add(guiViewOfList); panel.add(lineDisplay); add(panel);

Can set fonts, background, and editable // Get a fixed pitch font lineDisplay.setFont(new Font("Courier", Font.BOLD, 14)); lineDisplay.setBackground(Color.CYAN); lineDisplay.setEditable(false); guiViewOfList.setBackground(Color.GRAY); panel.setBackground(Color.RED );

Model / View relationship with list DefaultListModel implements ListModel and therefore can be used with JList, a graphical view of a list (construct with or set model) // DefaultListModel and JList work together as model/view guiViewOfList = new JList (); guiViewOfList.setModel(model);

The JList is selectable, set and get private class JListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent arg0) { int index = guiViewOfList.getSelectedIndex(); if (index >= 0 && index < model.size()) { // The JList is listening for changes to the model model.remove(index); // after a remove, the JList updates itself } } }

Add a mouse listener to out panel class ListenToMouse implements MouseListener

Difference between static and non-static A static method can be called without instantiating it’s class Math.abs(5-12); This won’t compile (the constructor is private) Math m = new Math(); Use static methods when you don’t need instance variables

TemperatureConverter has static methods public class TemperatureConverter { // Return the Celcius equivalent of Fahrenheit public static double F2C(double f) { return roundToOneDecimal((5.0 / 9.0) * (f )); } Tests do not need to instantiate the public void testF2C() { assertEquals(100.0, TemperatureConverter.F2C(212.0), 0.1); assertEquals(0.0, TemperatureConverter.F2C(32.0), 0.1); assertEquals(37.0, TemperatureConverter.F2C(98.6), 0.1); }