Tuesday, March 31, 2009

Tuesday March 31: taste the HTML rainbow, part three

OK, so now we've got a program that can display a webpage in a JEditorPane, but its kind of like ropeless bungee jumping: it might be exciting, but only for a very brief time, then the thrill is gone. Soooooooooooooooooooooooooooooooooooooooooooo, let's make it so that you can change the webpage loaded into the JEditorPane by entering an address. Here's how I did it:



Declare these two components

JTextField WebAddy;
JButton ChangeAddy;



then add these two components to your constructor:

//create JTextField, set text to first webpage String
WebAddy = new JTextField(StartPage);
WebAddy.setBounds(345,0,200,20); //size and place it
WebAddy.addActionListener(this);
ProvingGround.add(WebAddy); //add to container


ChangeAddy = new JButton("Go There"); //create JButton
ChangeAddy.setBounds(180,0,165,35); //size and place it
ChangeAddy.addActionListener(this);
ProvingGround.add(ChangeAddy); //add to container


add an "else if" statement for the ChangeAddy JButton


else if (e.getSource() == ChangeAddy)
{
//StartPage = WebAddy.getText( );
try {
Webapalooza.setPage(new URL(WebAddy.getText()));
// urlField.setText(url);
} //end try

catch(IOException ioe)
{
System.out.println("crap!");
}//end catch
}//end else for ChangeAddy JButton



Save, compile and run; you should be able to load another website into your JEditorPane by typing the address in the JTextField then hitting the new JButton



Cheers,

Uncle Paulie

No comments:

Post a Comment