CIS3931 Intro to JAVA - PowerPoint PPT Presentation

About This Presentation
Title:

CIS3931 Intro to JAVA

Description:

Reminder... As stated in the syllabus, you must pass the final in order to pass the class. ... Reminder... Additionally, some review material ... Reminder ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 23
Provided by: UniformOfD
Learn more at: http://www.cs.fsu.edu
Category:
Tags: java | cis3931 | intro | reminder

less

Transcript and Presenter's Notes

Title: CIS3931 Intro to JAVA


1
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

2
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.

3
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.

4
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.

5
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

6
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.

7
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
  • )

8
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

9
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.

10
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)

11
Using the showURL method
  • showURL takes in a String as its parameter. It
    doesnt 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.c
    om into the JEditorPane. Notice that the links on
    googles page wont work. Making links work will
    come later in the slides.

12
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 dont 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

13
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())
  • )

14
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

15
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)

16
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

17
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

18
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

19
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 ltcarriage returngt
  • It is very important that you hit the carriage
    return at the end of the line!!! (dont type the
    word carriage return this simple means to hit
    enter on the keyboard)

20
JAR Files
  • When your JAR file doesnt 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

21
Creating the JAR file
  • See http//www.cs.fsu.edu/cis3931/javajar.html
    for detailed step-by-step instructions.

22
Thursdays class
  • Turning a JAVA program into a web-viewable applet
  • A few more notes on program 6
  • Notes on the protected, private, public,
    static, and final modifiers.
Write a Comment
User Comments (0)
About PowerShow.com