Course Outcomes of Advanced Java Programming AJP (17625, C603)

Slides:



Advertisements
Similar presentations
Drawing in a frame – Java GUI
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.
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 Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Lecture 19 Graphics User Interfaces (GUIs)
Lesson 5 Miscellaneous language features Swing Programming.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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 
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.
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
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.
עקרונות תכנות מונחה עצמים תרגול 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,
– 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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
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.
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.
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.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
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.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Multiple buttons and action calls
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Graphical User Interfaces
Graphical User Interfaces
Web Design & Development Lecture 11
Swing JComponents.
A First Look at GUI Applications
Modern Programming Language Java
Review: Java GUI Programming
Graphical User Interface (pronounced "gooey")
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Course Outcomes of Advanced Java Programming AJP (17625, C603) Design GUI using AWT and Swing . C603.2 Develop program using event handling. C603.3 Use network concepts (client/server, socket) in the program. C603.4 Develop program using JDBC connectivity to access data from database and execute different queries to get required result. C603.5 Develop web based program using servlet and JSP Visit for more Learning Resources

Java GUI Libraries Swing Programming

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 Window JPanel 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 For more detail contact us