HELP for writing a Programme that displays text.

Check the API for methods in JEditorPane. Then write and run a program that uses a JEditorPane to just display text, just text.

What should I add or delete in the following code so that it just displays TEXT.

My code is -->

WEBBROWSER>

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowser extends JFrame {

private WebToolBar toolBar;

private WebBrowserPane browserPane;

// WebBrowser constructor

public WebBrowser()

{

super( "Deitel Web Browser" );

// create WebBrowserPane and WebToolBar for navigation

browserPane = new WebBrowserPane();

toolBar = new WebToolBar( browserPane );

// lay out WebBrowser components

Container contentPane = getContentPane();

contentPane.add( toolBar, BorderLayout.NORTH );

contentPane.add( new JScrollPane( browserPane ),

BorderLayout.CENTER );

}

// execute application

public static void main( String args[] )

{

WebBrowser browser = new WebBrowser();

browser.setDefaultCloseOperation( EXIT_ON_CLOSE );

browser.setSize( 640, 480 );

browser.setVisible( true );

} // end main

} // end class WebBrowser

WebPane.java

// WebBrowserPane.java

// WebBrowserPane is a simple Web-browsing component that

// extends JEditorPane and maintains a history of visited URLs.

package browser;

// Java core packages

import java.util.*;

import java.net.*;

import java.io.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowserPane extends JEditorPane {

private List history = new ArrayList();

private int historyIndex;

// WebBrowserPane constructor

public WebBrowserPane()

{

// disable editing to enable hyperlinks

setEditable( false );

}

// display given URL and add it to history

public void goToURL( URL url )

{

displayPage( url );

history.add( url );

historyIndex = history.size() - 1;

}

// display next history URL in editorPane

public URL forward()

{

historyIndex++;

// do not go past end of history

if ( historyIndex >= history.size() )

historyIndex = history.size() - 1;

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display previous history URL in editorPane

public URL back()

{

historyIndex--;

// do not go past beginning of history

if ( historyIndex < 0 )

historyIndex = 0;

// display previous URL

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display given URL in JEditorPane

private void displayPage( URL pageURL )

{

// display URL

try {

setPage( pageURL );

}

// handle exception reading from URL

catch ( IOException ioException ) {

ioException.printStackTrace();

}

}

}

WebPAne.java

// WebToolBar.java

// WebToolBar is a JToolBar subclass that contains components

// for navigating a WebBrowserPane. WebToolBar includes back

// and forward buttons and a text field for entering URLs.

package browser;

// Java core packages

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebToolBar extends JToolBar

implements HyperlinkListener {

private WebBrowserPane webBrowserPane;

private JButton backButton;

private JButton forwardButton;

private JTextField urlTextField;

// WebToolBar constructor

public WebToolBar( WebBrowserPane browser )

{

super( "Web Navigation" );

// register for HyperlinkEvents

webBrowserPane = browser;

webBrowserPane.addHyperlinkListener( this );

// create JTextField for entering URLs

urlTextField = new JTextField( 25 );

urlTextField.addActionListener(

new ActionListener() {

// navigate webBrowser to user-entered URL

public void actionPerformed( ActionEvent event )

{

// attempt to load URL in webBrowserPane

try {

URL url = new URL( urlTextField.getText() );

webBrowserPane.goToURL( url );

}

// handle invalid URL

catch ( MalformedURLException urlException ) {

urlException.printStackTrace();

}

}

}

);

// create JButton for navigating to previous history URL

backButton = new JButton( new ImageIcon(

getClass().getResource( "images/back.gif" ) ) );

//backButton = new JButton( "Back");

backButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to previous URL

URL url = webBrowserPane.back();

// display URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// create JButton for navigating to next history URL

forwardButton = new JButton( new ImageIcon(

getClass().getResource( "images/forward.gif" ) ) );

//forwardButton = new JButton("Fwd");

forwardButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to next URL

URL url = webBrowserPane.forward();

// display new URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// add JButtons and JTextField to WebToolBar

add( backButton );

add( forwardButton );

add( urlTextField );

} // end WebToolBar constructor

// listen for HyperlinkEvents in WebBrowserPane

public void hyperlinkUpdate( HyperlinkEvent event )

{

// if hyperlink was activated, go to hyperlink's URL

if ( event.getEventType() ==

HyperlinkEvent.EventType.ACTIVATED ) {

// get URL from HyperlinkEvent

URL url = event.getURL();

// navigate to URL and display URL in urlTextField

webBrowserPane.goToURL( url );

urlTextField.setText( url.toString() );

}

}

}

-

Please REPLY.

[6543 byte] By [taniataniaa] at [2007-11-26 18:13:41]
# 1

WEBBROWSER>

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowser extends JFrame {

private WebToolBar toolBar;

private WebBrowserPane browserPane;

// WebBrowser constructor

public WebBrowser()

{

super( "Deitel Web Browser" );

// create WebBrowserPane and WebToolBar for navigation

browserPane = new WebBrowserPane();

toolBar = new WebToolBar( browserPane );

// lay out WebBrowser components

Container contentPane = getContentPane();

contentPane.add( toolBar, BorderLayout.NORTH );

contentPane.add( new JScrollPane( browserPane ),

BorderLayout.CENTER );

}

// execute application

public static void main( String args[] )

{

WebBrowser browser = new WebBrowser();

browser.setDefaultCloseOperation( EXIT_ON_CLOSE );

browser.setSize( 640, 480 );

browser.setVisible( true );

} // end main

} // end class WebBrowser

WebPane.java

// WebBrowserPane.java

// WebBrowserPane is a simple Web-browsing component that

// extends JEditorPane and maintains a history of visited URLs.

package browser;

// Java core packages

import java.util.*;

import java.net.*;

import java.io.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowserPane extends JEditorPane {

private List history = new ArrayList();

private int historyIndex;

// WebBrowserPane constructor

public WebBrowserPane()

{

// disable editing to enable hyperlinks

setEditable( false );

}

// display given URL and add it to history

public void goToURL( URL url )

{

displayPage( url );

history.add( url );

historyIndex = history.size() - 1;

}

// display next history URL in editorPane

public URL forward()

{

historyIndex++;

// do not go past end of history

if ( historyIndex >= history.size() )

historyIndex = history.size() - 1;

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display previous history URL in editorPane

public URL back()

{

historyIndex--;

// do not go past beginning of history

if ( historyIndex < 0 )

historyIndex = 0;

// display previous URL

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display given URL in JEditorPane

private void displayPage( URL pageURL )

{

// display URL

try {

setPage( pageURL );

}

// handle exception reading from URL

catch ( IOException ioException ) {

ioException.printStackTrace();

}

}

}

WebPAne.java

// WebToolBar.java

// WebToolBar is a JToolBar subclass that contains components

// for navigating a WebBrowserPane. WebToolBar includes back

// and forward buttons and a text field for entering URLs.

package browser;

// Java core packages

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebToolBar extends JToolBar

implements HyperlinkListener {

private WebBrowserPane webBrowserPane;

private JButton backButton;

private JButton forwardButton;

private JTextField urlTextField;

// WebToolBar constructor

public WebToolBar( WebBrowserPane browser )

{

super( "Web Navigation" );

// register for HyperlinkEvents

webBrowserPane = browser;

webBrowserPane.addHyperlinkListener( this );

// create JTextField for entering URLs

urlTextField = new JTextField( 25 );

urlTextField.addActionListener(

new ActionListener() {

// navigate webBrowser to user-entered URL

public void actionPerformed( ActionEvent event )

{

// attempt to load URL in webBrowserPane

try {

URL url = new URL( urlTextField.getText() );

webBrowserPane.goToURL( url );

}

// handle invalid URL

catch ( MalformedURLException urlException ) {

urlException.printStackTrace();

}

}

}

);

// create JButton for navigating to previous history URL

backButton = new JButton( new ImageIcon(

getClass().getResource( "images/back.gif" ) ) );

//backButton = new JButton( "Back");

backButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to previous URL

URL url = webBrowserPane.back();

// display URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// create JButton for navigating to next history URL

forwardButton = new JButton( new ImageIcon(

getClass().getResource( "images/forward.gif" ) ) );

//forwardButton = new JButton("Fwd");

forwardButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to next URL

URL url = webBrowserPane.forward();

// display new URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// add JButtons and JTextField to WebToolBar

add( backButton );

add( forwardButton );

add( urlTextField );

} // end WebToolBar constructor

// listen for HyperlinkEvents in WebBrowserPane

public void hyperlinkUpdate( HyperlinkEvent event )

{

// if hyperlink was activated, go to hyperlink's URL

if ( event.getEventType() ==

HyperlinkEvent.EventType.ACTIVATED ) {

// get URL from HyperlinkEvent

URL url = event.getURL();

// navigate to URL and display URL in urlTextField

webBrowserPane.goToURL( url );

urlTextField.setText( url.toString() );

}

}

}

taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 2
PLEASE HELP ME IN THIS TOPIC
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 3
> What should I add or delete in the following code so> that it just displays TEXT.USE THE SETTEXT() METHOD.
CaptainMorgan08a at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 4
But where to use settext() method ?which methods in the api should I delete.
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 5
I'd strongly advise against deleting methods from the API.
floundera at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 6
What should I do inorder to solve my problem?The PROBLEM is to Check the API for methods in JEditorPane. Then write and run a program that uses a JEditorPane to just display text, just text.
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 7

> What should I do inorder to solve my problem?The

> PROBLEM is to Check the API for methods in

> JEditorPane. Then write and run a program that uses

> a JEditorPane to just display text, just text.

So why don't you check the API and search through the methods like the assignment says?

CaptainMorgan08a at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 8
Please guide me ,I am checking for the methods in the API.Some DIECTION got to be there.
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 9
> Some DIECTION got to be there.I already told you to look at the setText() method.
CaptainMorgan08a at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 10
By setText() method you mean I should add this method in the JEditorPane.Also I shouldnot delete any method from the API rather I shud add this method setText().
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 11

> By setText() method you mean I should add this method

> in the JEditorPane.Also I shouldnot delete any method

> from the API rather I shud add this method setText().

setText() is an existing method in the JEditorPane class. You do not need to create any new methods.

CaptainMorgan08a at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 12
I am aware of the setText () method .By adding the new setText() method I mean that should i make another setText() method and add some text in this method and display the text.Please let me know the answer.
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 13
Please let me know I really need some guidance to complete this programme.
taniataniaa at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 14

> The PROBLEM is to Check the API for methods in JEditorPane. Then

> write and run a program that uses a JEditorPane to just display text, just text.

Then read the API. In the API description you will find a link to the Swing tutorial on "Using Text Components" which has a working example of how to use a JEditorPane.

Get rid of all the code related to next, previous functionality. Get the basicis working first and then add in additional functionality. You don't write the whole program first when you don't even know hot to do the basics. So go back to the drawing board and create a SSCCE. You should be able to do what you want in about 10 lines of code or less.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 5:46:51 > top of Java-index,Java Essentials,Java Programming...
# 15
My problem is that I am not getting to the point that how can I simply display the TEXT from the WEBPAGE nothing else.That is why I am confused what should I do in the programme.Please if you could guide me some way I will be thankful to you
taniataniaa at 2007-7-21 17:18:55 > top of Java-index,Java Essentials,Java Programming...
# 16
> how can I simply display the TEXT from the WEBPAGE nothing else.I guess you are asking how to parse out the text and ignore all the HTML tags. If so the see this posting: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=637059
camickra at 2007-7-21 17:18:55 > top of Java-index,Java Essentials,Java Programming...
# 17

Actually I have to just print the text of the website as I have already created the webbrowser.The thing is which method should I omit so that only TEXT will be displayed.If you can have a look plzz help me.

WEBBROWSER>

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowser extends JFrame {

private WebToolBar toolBar;

private WebBrowserPane browserPane;

// WebBrowser constructor

public WebBrowser()

{

super( "Deitel Web Browser" );

// create WebBrowserPane and WebToolBar for navigation

browserPane = new WebBrowserPane();

toolBar = new WebToolBar( browserPane );

// lay out WebBrowser components

Container contentPane = getContentPane();

contentPane.add( toolBar, BorderLayout.NORTH );

contentPane.add( new JScrollPane( browserPane ),

BorderLayout.CENTER );

}

// execute application

public static void main( String args[] )

{

WebBrowser browser = new WebBrowser();

browser.setDefaultCloseOperation( EXIT_ON_CLOSE );

browser.setSize( 640, 480 );

browser.setVisible( true );

} // end main

} // end class WebBrowser

WebPane.java

// WebBrowserPane.java

// WebBrowserPane is a simple Web-browsing component that

// extends JEditorPane and maintains a history of visited URLs.

package browser;

// Java core packages

import java.util.*;

import java.net.*;

import java.io.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebBrowserPane extends JEditorPane {

private List history = new ArrayList();

private int historyIndex;

// WebBrowserPane constructor

public WebBrowserPane()

{

// disable editing to enable hyperlinks

setEditable( false );

}

// display given URL and add it to history

public void goToURL( URL url )

{

displayPage( url );

history.add( url );

historyIndex = history.size() - 1;

}

// display next history URL in editorPane

public URL forward()

{

historyIndex++;

// do not go past end of history

if ( historyIndex >= history.size() )

historyIndex = history.size() - 1;

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display previous history URL in editorPane

public URL back()

{

historyIndex--;

// do not go past beginning of history

if ( historyIndex < 0 )

historyIndex = 0;

// display previous URL

URL url = ( URL ) history.get( historyIndex );

displayPage( url );

return url;

}

// display given URL in JEditorPane

private void displayPage( URL pageURL )

{

// display URL

try {

setPage( pageURL );

}

// handle exception reading from URL

catch ( IOException ioException ) {

ioException.printStackTrace();

}

}

}

WebPAne.java

// WebToolBar.java

// WebToolBar is a JToolBar subclass that contains components

// for navigating a WebBrowserPane. WebToolBar includes back

// and forward buttons and a text field for entering URLs.

package browser;

// Java core packages

import java.awt.*;

import java.awt.event.*;

import java.net.*;

// Java extension packages

import javax.swing.*;

import javax.swing.event.*;

public class WebToolBar extends JToolBar

implements HyperlinkListener {

private WebBrowserPane webBrowserPane;

private JButton backButton;

private JButton forwardButton;

private JTextField urlTextField;

// WebToolBar constructor

public WebToolBar( WebBrowserPane browser )

{

super( "Web Navigation" );

// register for HyperlinkEvents

webBrowserPane = browser;

webBrowserPane.addHyperlinkListener( this );

// create JTextField for entering URLs

urlTextField = new JTextField( 25 );

urlTextField.addActionListener(

new ActionListener() {

// navigate webBrowser to user-entered URL

public void actionPerformed( ActionEvent event )

{

// attempt to load URL in webBrowserPane

try {

URL url = new URL( urlTextField.getText() );

webBrowserPane.goToURL( url );

}

// handle invalid URL

catch ( MalformedURLException urlException ) {

urlException.printStackTrace();

}

}

}

);

// create JButton for navigating to previous history URL

backButton = new JButton( new ImageIcon(

getClass().getResource( "images/back.gif" ) ) );

//backButton = new JButton( "Back");

backButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to previous URL

URL url = webBrowserPane.back();

// display URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// create JButton for navigating to next history URL

forwardButton = new JButton( new ImageIcon(

getClass().getResource( "images/forward.gif" ) ) );

//forwardButton = new JButton("Fwd");

forwardButton.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )

{

// navigate to next URL

URL url = webBrowserPane.forward();

// display new URL in urlTextField

urlTextField.setText( url.toString() );

}

}

);

// add JButtons and JTextField to WebToolBar

add( backButton );

add( forwardButton );

add( urlTextField );

} // end WebToolBar constructor

// listen for HyperlinkEvents in WebBrowserPane

public void hyperlinkUpdate( HyperlinkEvent event )

{

// if hyperlink was activated, go to hyperlink's URL

if ( event.getEventType() ==

HyperlinkEvent.EventType.ACTIVATED ) {

// get URL from HyperlinkEvent

URL url = event.getURL();

// navigate to URL and display URL in urlTextField

webBrowserPane.goToURL( url );

urlTextField.setText( url.toString() );

}

}

}

taniataniaa at 2007-7-21 17:18:55 > top of Java-index,Java Essentials,Java Programming...
# 18

I showed you how to parse the text out of the webpage.

I suggested how to create a simple program to read a file into the editor pane.

I'm not going to read through hundreds of line of unnecessary code, since I obvously don't understand the question and you aren't making an effor to create a SSCCE.

camickra at 2007-7-21 17:18:55 > top of Java-index,Java Essentials,Java Programming...