1 Windows, Networking and other Tidbits Chapter Fourteen.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Graphical User Interfaces (Part IV)
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Jan AWT Widgets Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Io package as Java’s basic I/O system continue’d.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Applets, AWTS CompSci 230 Software Construction.
Using Dreamweaver. Slide 1 Dreamweaver has 2 screens that do different things The Document window where you create your WebPages The Site window where.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
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.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
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.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Csc Basic Graphical User Interface (GUI) Components.
Chapter 12: Accessing the Web URL (Uniform Resource Locator) class Applet methods –for audio clips –for images –context interface.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
CSC 205 Programming II Lecture 5 AWT - I.
Advanced GUIs II CS Lecture
“Form Ever Follows Function” Louis Henri Sullivan
AWT Components and Containers
UNIT-5.
11.1 Applets & graphics.
Menu Bars and Menus.
GUI building with the AWT
Uniform Resource Locator: URL
Enabling Application Delivery Via the Web
Advanced GUIs II CS Lecture
Presentation transcript:

1 Windows, Networking and other Tidbits Chapter Fourteen

2 Topics: l Windows, menus and dialog boxes –allow real pop up windows now from Java applets l Networking - load HTML from browser, retrieve files from web sites work with generic sockets l Extra stuff

3 Windows, menus and dialog boxes l Frames AWT Window class provides for windows that are independent (own titles, resize handels, menu bars) l Subclasses: l Frame - fully functioning window with menubar l Dialog - more limited

4 To create a frame l new Frame( ) // = no title l new Frame(String) has given title l Frames are containers “like panels” l default layout is BorderLayout –win = new Frame(“My Way Cool Window”); –win.setLayout(new Borderlayout(10,20)); –win.add(“North”, new Button(“Start”)); –win.add(“Center”, new Button(“Move”));

5 Sizes moves location show hide l resize( ) to set size l move( ) to set location l location( ) can tell the applet window is on screen l win.resize(100, 200); l Dimension d = location( ); l win.move(d.width + 50, d.height + 50);

6 l When you create a window it is invisable l show( ); // to make it appear l hide( ); // to make it disappear

7 A popup window 14-1 l public class GUI extends java.applet.Applet { Frame window; l public void init( ) { –add(new Button(“Open Window”)); –add(new Button(“Close Window”)); –window = new MyFrame(“A Popup Window”); –window.resize(150, 150); window.show( );

8 l public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { l String label = (String)arg; l if (label.equals(“Open Window”)) { l if (!window.isShowing( ) ) –window.show( );} l else if (label = = “Close Window”) { l if (window.isShowing( ) ) –window.hide( ); } l return true; }

9 List 14.1 contd. l else return false; } } l class MyFrame extends Frame { l Label l // lowercase L is variable l MyFarame(String title) { l Super(title); l setLayout(new GridLayout(1,1)); //ones l l = new Label(“This is a Window”, Label.center); add(l); } // l is lower L

10 Programming hint: Do Not use lowercase letter L’s they look too much like the digit One.... I keep telling publishers that and have done it myself - choose something else. Old time keypunch operators got sheets in the writers hand Z vs. 2, l vs. 1 This is not a new concept...

11 Menus and Menubars l To create: –MenuBar mb = new MenuBar( ); l Set as default: –window.setMenuBar(mb); l Add individual menus (File, Edit etc.) l menu m = new menu(“file”); l mb.add(m); // Some systems NOT All l // would allow mb.setHelpMenu(m);

12 Menu Items l Instances of the class MenuItem –Menu m = new Menu(“Tools”); m.add(new MenuItem(“Info”)); l Instances of the class CheckBoxMenuItem l Other menus with their own items l Seperators = lines that divide groups

13 Submenus l To create: l Menu sb = new Menu (“sizes”); l m.add(sb) // sub to m in previous slide l sb.add(new MenuItem(“Small”)); l medium and large etc. can also be added

14 CheckboxMenuItem l A menu item with a checkbox l CheckboxMenuItem coords = new CheckBoxMenuItem(“Show Coordinates”); l m.add(coords); l Again m from previous menu l Any menu can be enable( ) disable( )

15 Menu Actions l public boolean action(Event evt, Object arg) { if (evt.target instanceof MenuItem) { l String label = (String)arg; l if(label.equals(“Show Coordinates”)) toggleCoords( ); l else if (label.equals(“Fill”)) fillcurrentArea( ); return true; } else return false; }

16 l Add a menu: Window from class MyFrame l MyFrame(String title) { –Super(title); –MenuBar mb = new MenuBar( ); –Menu m = new Menu(“Colors”); –m.add(new MenuItem(“Red”)); –m.add(new MenuItem(“Blue”)); –m.add(new MenuItem(“Green”)); –m.add(new MenuItem(“-”)); –m.add(new CheckboxMenuItem(“Reverse Text”)); mb.add(m); mb.setHelpMenu(m); –setMenuBar(mb); //... }

17 l To run the last slide you need an action( ) method: l public boolean action(Event evt, Object arg) { String label = (String)arg; l if (evt.target instanceof MenuItem) { l if (label.equals (“Red”)) setBackground(Color.red); l else if (label.equals(“Blue”)) setBackgroundColor.blue); l else if (label.equals(“Green”)) setBackgroundColor.green); return true; }

18 l if (evt.target instanceof CheckboxMenuItem) { l if (getForeground( ) = = Color.balck) l setForeground(Color.white); l else setForeground(Color.black); l return true; } l return false; }

19 Dialog Boxes l transient windows - popup with warnings ask for info. etc. l Two types in AWT: –Dialog class = generic –FileDialog = platform specific (save/open)files l To create: l Dialog(Frame, boolean) initial invisable –true = modal false not modal

20 l Dialog(Frame, String, boolean) same as last slide BUT has titlebar and title l can be show( ) hide( ) just like Frames l To add: l m.add(new MenuItem(“Set Text...”)); l dl = new Dialog(this, “Enter Text”, true); l dl.setLAyout(new GridLayout(2, 1, 30, 30)); l tf = new TextField(l.getText( ), 20); l dl.add(tf); dl.add(new Button(“OK”)); l dl/resize(150, 75); // Choose OK = dismiss

21 File Dialogs - Can’t access or Severe restrictions on local system Really just for stand alone l To create: FileDialog (Frame, String) l FileDialog(Frame, String, int) // (load/save) –FileDialog.SAVE. OR FileDialog.LOAD l FIleDialog fd = new FileDialog(this, “FileDialog”); l fd.show( );

22 Window Events l WINDOW-DESTROY l WINDOW-EXPOSE // brought forward l WINDOW-ICONIFY l WINDOW-DEICONIFY l WINDOW-MOVED l Can test for all of these in the Event class

23 AWT in Stand Alone Applications l Can use all the applet stuff: l Can use Graphics etc.

24 Networking in Java l ShowDocument( ) // Load - link to other web page l openStream( ) open connect to URL l Socket classes: Socket and ServerSocket open standard socket connections (read/write to them)

25 Create Links inside Applets l URL class To create a new URL l URL(String, String, int, String) –http ftp gopher file –host name ( ftp.netcom.com) –a port number (80 for http) l URL(String, String, String) Same as above minus port number l URL(String) String should include “All”

26 Srting url = “http// l try ( theURL = new URL(url); { l catch (MAlformedURLException e) { –System.out.println(“Bad URL: “ + theURL); –} l After you have the URL Link it: l getAppletContext( ).showDocument(theURL):

27 Bookmark buttons l import java.awt.*; l import java.net.URL; l import java.net.MalformedURLExceptions; l public class ButtonLink extends java.applet.Applet { l Bookmark bmlist[] = new Bookmark[3];

28 l public void init( ) { l bmlist[0] = new Bookmark(“Laura’s Home Page”, “ l bmlist[1] = new Bookmark(“Yahoo”, “ l bmlist[2] = new Bookmark(“Java Home Page”, “ l setLayout(new Bookmark(“GridLayout(bmlist.length,1, 10,10)); for(int i=0; i< bmlist.length; i++); l add(new Button(bmlist[i].name)); } }

29 l public boolean action(Event evt, Object arg) { l if(evt.target instanceof Button) { l linkto((String) arg); l return true } l else return false; } l Void LinkTo(String name) { l URL theURL = null; for (int i=0; i < bmlist.length; i++) { l if (name.equals(bmlist[i].name)) theURL = bmlist[i].url; }

30 l if (theURL != null) getAppletContext( ).showDocument(theURL); } } l class Bookmark { String name; URL url; l Bookmark(String name, String theURL) { l this.name = name; l try { this URL = new URL(theURL); } l catch (MalformedURLException e) { l System.out.println(“Bad URL: “ + theURL); } } }

31 Opening Web Connections openStream( ) l To open a net connection - given URL l try { inputStream in = theURL.openStream( ); l DataInputStream data new DataInputStream((new BufferedInputStream(in); String line; l while (( line = data.readLine( )) != null) { l System.out.println(line); } }

32 l catch (IOException e) { l System.out.Pintln(“IO Error: “ + e.getMessage( )); }

33 The Get (Poe’s) Raven class l Page complete text l Note the following: l All the imports - more than ever before (7) l public class GetRaven extends java.applet.Applet l implements Runnable { l URL theURL; Thread runner; l TextArea ta = new Text(“Getting text...”,30,70);

34 l public void init( ) { l String url = “ l try { this.theURL = new URL(url); } l catch (MalformedURLException e) { –System.out.println(“Bad URL: “ + theURL); –} add(ta); } l NOT going to review insets( ) –start( ) stop( )

35 l public void run( ) { InputStream conn; l data = new DataInputStream(new BufferedInputStream(conn)); l while((line = data.readLine( )) != null) { –buf.append(line + “\n”); } –ta.setText(buf.toString( )); } –catch (IOException e) { System.out.println(“IO Error:” + e.getMessage( )); } } }

36 URLconnection Class l openStream( ) simple for l URLconnection class.URLconnection –A way to retrieve files

37 Sockets l Java provides socket and ServerSocket l They provide for networking applications beyond what URL and URLconnection classes offer l Socket connection = new Socket(hostname, portnum);

38 l DataInputStream in = new DataINputStream( new BufferedINputStream(connection.getIn putStream( ))); l DataoutputStream out = new DataOutputStream( new BufferedOutputStream(connection.getO utputStream( ))); l connection.close( ) // when done & hide it l ServerSocket sconnection = new ServerSocket(8888); sconnection.accept( ); l See java.net for more info java sockets

39 Other Applet Hints l showStatus( ) // use print error etc. msgs: l getAppletContext( ).showStatus(“Change the color); // access browser features l Applet Information: –public String getAppletInfo( ) { –return “GetRaven copyright 1995 Laura Lemay;’ }

40 Communication Between Applets l for (Enumeration e = getAppletContext( ).getApplets( ); l e.hasMoreElements( );) { l Applet current = (Applet)(e.nextElement( )); l current.sendMessage } –//getApplets( ) returns Enumeration Object –A list of Applets on the page

41 l to call specific applet: l This applet sends information: l <APPLET CODE=“MyApplet.class” WIDTH=100 HEIGHT=150 l NAME = “sender””> l This applet receives information from the sender: l <APPLET CODE=“MyApplet.class” WIDTH=100 HEIGHT=150 l NMAE=“receiver”>

42 Finally: l //get ahold of the receiver applet l Applet receiver = getAppletContext( ).getApplet(“receiver”); l //tell it to update itself. –RECEIVER.UPDATE(TEXT, VALUE);