carriage return

Download Report

Transcript carriage return

CIS3931 - Intro to JAVA
Lecture Notes Set 13
19-July-05
GUI Programming –TextField Action
Listeners, JEditorPane action listeners, HTML
in a JEditorPane, JAR Files
Reminder…
• As stated in the syllabus, you must pass the final in order
to pass the class.
• Anyone who receives below a 70 on the final will fail the
class regardless of how high your other grades are.
• The majority of the final will be repeated questions from
Midterm 1 and 2. There will only be a few new questions
from the material we have covered since Midterm 2.
• A review for the final will take place on the 2nd of August.
Reminder…
• Additionally, some review material will be
posted online. Specifically, I will list the
things that you will need to know for the
final.
• As with Midterm 1 and 2, there will be
extra credit available. It will be very similar
to the extra credit that was given on
Midterm 2.
Reminder…
• If there are any problems with your
grades, you need to let me know
immediately. Grades will be submitted the
Monday after the last week of class. No
grade changes will be permitted after this
date.
Reminder …
• If you would like to see a copy of your
Midterm 1 or Midterm 2, please stop by my
office. It is recommended that you call
before you come over to be sure that I am
in the office (644-8562)
– This is a community office number … please
ask for Bobby when you call …
Adding actions to a TextField
• When creating the webbrowser, you need
to tell your program to “load” whatever is in
the textfield for your URL.
• Assign listener to the text field so when the
user hits “return”, the textfield performs an
action.
Adding actions to a TextField
//First, create the textField
JTextfield sampleTextField = new JTextField(“Enter text
here”);
//Assign an action listener to the textfield
sampleTextField.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//PUT YOUR ACTION CODE HERE
}
}});
Adding actions to a TextField
• Example : Creating a TextField that will
allow the user to change the text displayed
in an JEditorPane
• See textaction.java
Turning a JEditorPane into an
HTML viewer
• JEditorPane has a method called
setPage (String s)
• setPage will load an HTML page into the editor pane
• Pass the string containing the URL into the setPage
method
• Since you are dealing with I/O (loading a page from the
internet uses an input stream), you need to handle I/O
errors by using a try/catch block.
Example method for loading a URL
public void showURL(String urlString)
{
try
{
//Try to load the webpage
samplePane.setPage(urlString);
}
catch (Exception exception)
{
//If there is an error, inform the user
//by placing text on the EditorPane
samplePane.setText("Couldn't open the url :
" + urlString + "
Reason for error : " +
exception);
}
}
Using the showURL method
• showURL takes in a String as its parameter. It doesn’t return
anything.
• showURL is a very primitive way to load a URL … you will need to
expand upon it slightly when using it for your web browser.
• See htmleditorpane.java for example
• Note : This example only loads http://www.google.com into the
JEditorPane. Notice that the links on google’s page won’t work.
Making links work will come later in the slides.
Making the links work…
• As with everything in JAVA, there needs to be an action
listener associated with the links on webpages in order
to get them to work.
• Luckily we don’t have to write an action listener for every
link on every page (which would be impossible…)
• Simply assign a “catch all” action listener to the
JEditorPane to load pages when a the use clicks on the
link…
• Special actionListener called “HyperlinkListener” exists
to handle such a request …
Making the links work … the
HyperlinkListener
//Assuming the JEditorPane is called samplePane
samplePane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
//Check to see if the user clicked on a link
if (event.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED)
{
//Call our showURL function
//event.getURL() gets the URL from the link
showURL("" + event.getURL());
}
}
});
The back and forward buttons
• Count as 10 (of 100) points for Program 6.
• The back and forward buttons are not
easy to make …
• If you have trouble with the buttons, just
do one of the extra credit things instead
Creating the back and forward
buttons
• Can be done with an array
• Store each page you visit in the array while
keeping track of your “index” position in the
array.
• Hitting the forward button should load the next
URL in the array (and increment your current
index position by 1)
• Hitting back button should should load the
previous URL in the array (and decrement your
current index position by 1)
Creating the back and forward
buttons
• Check for duplicate URLs being added in
a row (example : hitting the “home” button
3 times in a row should not add the home
URL three times in a row to the array)
• Make sure you are checking and adding to
the array EVERY TIME you click on a link
or load a URL from the textfield
JAR Files
• JAVA Archive files – similar to the UNIX TAR file
(… actually, the exact same format).
• Used to contain multiple JAVA source and class
files in one file
• Can be used to create a packaged “executable”
version of your program
– Note : The computer executing the JAR file will still
need to have the JRE (JAVA Runtime Environment) or
the SDK (Software Development Kit) installed
JAR Files
• Must create a “Manifest” file
• Manifest file tells JAVA which class file
inside of your JAR file should be executed
• The format of the manifest file is very strict
Manifest File
• If your JAVA file was called Program6.java, your
main class file would be called Program6.class
• The associated manifest file would be :
Main-Class: Program6 <carriage return>
• It is very important that you hit the carriage
return at the end of the line!!! (don’t type the
word “carriage return” … this simple means to hit
“enter” on the keyboard)
JAR Files
• When your JAR file doesn’t work, 9 out of
10 times there is a problem with your
manifest file
• Delete your manifest file and try again …
• You can name your manifest file whatever
you want … I usually use manifest.txt
Creating the JAR file
• See
http://www.cs.fsu.edu/~cis3931/javajar.htm
l for detailed step-by-step instructions.
Thursday’s class…
• Turning a JAVA program into a webviewable applet
• A few more notes on program 6
• Notes on the “protected”, “private”,
“public”, “static”, and “final” modifiers.