Lesson 5 Miscellaneous language features Swing Programming.

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
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.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
Chapter 121 Window Interfaces Using Swing Chapter 12.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Misc Language Features Features that were added relatively recently that are now in common use.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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 
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.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
CSE 219 Computer Science III Graphical User Interface.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
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.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
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,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Multiple buttons and action calls
GUIs and Events Rick Mercer.
Graphical User Interfaces
Swing JComponents.
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Course Outcomes of Advanced Java Programming AJP (17625, C603)
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Lesson 5 Miscellaneous language features Swing Programming

Assertions Use the assert statement to insert assertions at particular points in the code. The assert statement can have one of two forms: –assert booleanExpression; –assert booleanExpression : errorMessage; The errorMessage is an optional string message that would be shown when an assertion fails.

Assertions Example: Simple check for rogue values Class Foo{ public void foo(int x[], int n){ assert n > 0 && n < x.length; “n out of range”; … } Example: Simple check for null object class Foo{ public void foo(){ Student s = StudentFactory.create(“Graduate”); assert s != null; }

Assertions Assertions are not typically used to signal errors in production client application code. Rather, they are used –During development –By library writers to signals errors to library users (who themselves are software developers!) java –ea | -da Program –(“enable assertions” or “disable assertions”)

Assertions prompt> java AssertionTest3 and you enter a valid character, it will work fine. However, if you enter an invalid character, nothing will happen. This is because, by default, assertions are disabled at runtime. To enable assertions, use the switch - enableassertion (or -ea) as follows: prompt> java -ea AssertionTest3 prompt> java -enableassertion AssertionTest3

Foreach loop // Returns the sum of the elements of a int sum(int[] a) { int result = 0; for (int i : a) result += i; return result; } //Compare to old way int sum(int[] a) { int result = 0; for (int i = 0; i < a.length; ++i) result += a[i]; return result; } Much cleaner syntax! Think “for each i in the Collection a”

ForEach loop Also used to replaced iterators in more general collections (Lists, HashMaps, etc.) More on this when we cover collections

Varargs Allows you to define/call methods with variable number of arguments. Before –void foo(int i, String[] args) … was called as –String args[] = {“hello”, “goodbye”}; foo(1, args); As of 1.5 it can also be called more flexibly as –void foo(int i, String… args) –foo(1,”hello”, “goodbye”);

Varargs Note that the vararg parameter, denoted by the ellipses (“…”), must always be the last one. Inside the method it is always handled as an array of whatever size. However, it can be called as a method with any number of arguments without having to box them in an array!

enumerations

Static import In order to access static members, it is necessary to qualify references with the class they came from. For example –Math.cos(Math.PI * theta); Now, can avoid this with: –import static java.lang.Math.PI; Once the static members have been imported, they may be used without qualification: –double r = cos(PI * theta);

Typesafe Enums Old way: –public static final int SEASON_WINTER = 0; –public static final int SEASON_SPRING = 1; –public static final int SEASON_SUMMER = 2; –public static final int SEASON_FALL = 3; New way: –enum Season {WINTER, SPRING, SUMMER, FALL };

New way Season now declares a new type that can only have the specified values! –Enhanced compiler-time checking possible –Also declares a namespace –In old way printed values are uninformative Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.

More than meets the eye Enumerated types also have many other cool high-level features that make them better than C enums –They can be looped over as collections –They can be looped over in ranges –They can be cloned –They can be printed See Enum.java in class examples

Adding methods to enum types public enum Season{ SUMMER, SPRING, FALL, WINTER; public String clothing(){ if (this == SUMMER) return("short sleeves"); else if (this == SPRING) return("Winter coat"); else if (this == FALL) return("long sleeves"); else if (this == WINTER) return("two winter coats"); return(null); } Note that enums Can be declared In their own classesscc For enums, “this” refers to the value of the operating Enumerated type (ie SUMMER, SPRING, etc.)

More on enum enum has a toString method that will give the String representation of the value of this > Season s = Season.WINTER; > String sval = s.toString(); > System.out.pritnln(sval) > WINTER

C-style printing If you like C-style printing that is now available as: import java.io.*; public class PrintTest{ public static void main(String[] args){ float x = 1.2f; PrintStream p = new PrintStream(System.out); p.printf("%s: %f\n", "the result is", x); }

Swing

Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing Graphical User Interfaces (GUIs). Commonly used classes in javax.swing package: –JButton, JTextBox, JTextArea, JPanel, JFrame, JMenu, JSlider, JLabel, JIcon, … –There are many, many such classes to do anything imaginable with GUIs –Here we only study the basic architecture and do simple examples

Swing components, cont. Each component is a Java class with a fairly extensive inheritency hierarchy: Object Component Container JComponent JPanel Window Frame JFrame

Using Swing Components Very simple, just create object from appropriate class – examples: –JButton but = new JButton(); –JTextField text = new JTextField(); –JTextArea text = new JTextArea(); –JLabel lab = new JLabel(); Many more classes. Don’t need to know every one to get started. See ch. 9 Hortsmann

Adding components Once a component is created, it can be added to a container by calling the container’s add method: Container cp = getContentPane(); cp.add(new JButton(“cancel”)); cp.add(new JButton(“go”)); How these are laid out is determined by the layout manager. This is required

Laying out components Not so difficult but takes a little practice Do not use absolute positioning – not very portable, does not resize well, etc.

Laying out components Use layout managers – basically tells form how to align components when they’re added. Each Container has a layout manager associated with it. A JPanel is a Container – to have different layout managers associated with different parts of a form, tile with JPanels and set the desired layout manager for each JPanel, then add components directly to panels.

Layout Managers Java comes with 7 or 8. Most common and easiest to use are –FlowLayout –BorderLayout –GridLayout Using just these three it is possible to attain fairly precise layout for most simple applications.

Setting layout managers Very easy to associate a layout manager with a component. Simply call the setLayout method on the Container: JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); JPanel p2 = new JPanel(); p2.setLayout(new BorderLayout()); As Components are added to the container, the layout manager determines their size and positioning.

Event handling

What are events? All components can listen for one or more events. Typical examples are: –Mouse movements –Mouse clicks –Hitting any key –Hitting return key –etc. Telling the GUI what to do when a particular event occurs is the role of the event handler.

ActionEvent In Java, most components have a special event called an ActionEvent. This is loosely speaking the most common or canonical event for that component. A good example is a click for a button. To have any component listen for ActionEvents, you must register the component with an ActionListener. e.g. –button.addActionListener(new MyAL());

Delegation, cont. This is referred to as the Delegation Model. When you register an ActionListener with a component, you must pass it the class which will handle the event – that is, do the work when the event is triggered. For an ActionEvent, this class must implement the ActionListener interface. This is simple a way of guaranteeing that the actionPerformed method is defined.

actionPerformed The actionPerformed method has the following signature: void actionPerformed(ActionEvent) The object of type ActionEvent passed to the event handler is used to query information about the event. Some common methods are: –getSource() object reference to component generating event –getActionCommand() some text associated with event (text on button, etc).

actionPerformed, cont. These methods are particularly useful when using one eventhandler for multiple components.

Simplest GUI import javax.swing.JFrame; class SimpleGUI extends JFrame{ SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }

Another Simple GUI import javax.swing.*; class SimpleGUI extends JFrame{ SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”); Container cp = getContentPane();//must do this cp.add(but1); show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Add Layout Manager import javax.swing.*; import java.awt.*; class SimpleGUI extends JFrame{ SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”); Container cp = getContentPane();//must do this cp.setLayout(new FlowLayout(FlowLayout.CENTER); cp.add(but1); show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Add call to event handler import javax.swing.*; import java.awt.*; class SimpleGUI extends JFrame{ SimpleGUI(){ setSize(400,400); //set frames size in pixels setDefaultCloseOperation(EXIT_ON_CLOSE); JButton but1 = new JButton(“Click me”); Container cp = getContentPane();//must do this cp.setLayout(new FlowLayout(FlowLayout.CENTER); but1.addActionListener(new MyActionListener()); cp.add(but1); show(); } public static void main(String[] args){ SimpleGUI gui = new SimpleGUI(); System.out.println(“main thread coninues”); }}

Event Handler Code class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(“I got clicked”, null); }

Add second button/event class SimpleGUI extends JFrame{ SimpleGUI(){ /*.... */ JButton but1 = new JButton(“Click me”); JButton but2 = new JButton(“exit”); MyActionListener al = new MyActionListener(); but1.addActionListener(al); but2.addActionListener(al); cp.add(but1); cp.add(but2); show(); }

How to distinguish events –Less good way class MyActionListener implents ActionListener{ public void actionPerformed(ActionEvent ae){ if (ae.getActionCommand().equals(“Exit”){ System.exit(1); } else if (ae.getActionCommand().equals(“Click me”){ JOptionPane.showMessageDialog(null, “I’m clicked”); }

Good way class MyActionListener implents ActionListener{ public void actionPerformed(ActionEvent ae){ if (ae.getSource() == but2){ System.exit(1); } else if (ae.getSource() == but1){ JOptionPane.showMessageDialog(null, “I’m clicked”); } Question: How are but1, but2 brought into scope to do this? Question: Why is this better?

Putting it all together See LoginForm.java example in class notes