charle819 charle819 - 27 days ago 8
Java Question

Changing html page inside JEditorPane on button click

Any idea how to change html page viewed inside a JEditorPane on button click events in JFrame, sorry i am new to java so a basic explanation will be really appreciated.(need to chage the content where page 1 content here is written)

package test1;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class check1 extends JFrame implements ActionListener{
JEditorPane jep;
JScrollPane scroll;
JPanel p,p1;
JButton b,b1,b2,b3;
Dimension d;
String url;
public check1() {
d = new Dimension(500, 1500);

b = new JButton("Click ME");
b.addActionListener(this);

b1 = new JButton("Click ME");

b2 = new JButton("Click ME");
//b2.setBounds(0, 100, 70, 40);
b3 = new JButton("Click ME");
//b3.setBounds(0, 150, 70, 40);

p = new JPanel();
p.setLayout(new FlowLayout());
p.add(b);

p1 = new JPanel();
p1.setLayout(new GridLayout(4, 2, 1, 1));
p1.add(b1,0,0);
p1.add(b2,0,1);
p1.add(b3,2,0);
p1.setVisible(false);



try {
jep = new JEditorPane("file:///C:/Users/Chinmay/workspace/project1/src/page1.html");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jep.setEditable(true);



scroll = new JScrollPane(jep);
setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.WEST);
getContentPane().add(scroll,BorderLayout.CENTER);

setSize(1000, 800);
setVisible(true);
setLocationRelativeTo(null);

}
public static void main(String args[])
{
new check1().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b))
{

p1.setVisible(true);
}
else if(e.getSource().equals(b1))
{

url ="file:///C:/Users/Chinmay/workspace/project1/src/page.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource().equals(b2))
{
url ="file:///C:/Users/Chinmay/workspace/project1/src/page1.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource().equals(b3))
{
url ="file:///C:/Users/Chinmay/workspace/project1/src/page2.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}

}


https://i.stack.imgur.com/Iz7Jt.png

Answer

When things aren't working, the first thing to do is simplify the code down to the simplest that displays the error. There were a lot of errors in this code that I simply removed in the process of simplifying it. But start from this working code and change it, bit by bit, until it breaks again, then prepare an MCVE of the error and post it here.

Other tips before I get into a working example.

  1. Don't have four buttons with identical names! It is confusing when debugging, and confusing to the end user. If you cannot think of more useful names for them, use the same or similar text as the name of the attribute.
  2. The 'invisible / visible on button click' part was not relevant to the problem, so to simplify the code, remove that part and have the panel containing 3 buttons visible from the beginning.
  3. But having said that, it was only necessary to have 2 buttons in order to see it either work or fail.
  4. Don't use magic numbers in layout constraints! I doubt the buttons were ending up where you intended them to appear.
  5. Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

Having said all that, the remaining problem was summed up in:

url = "file:///C:/Users/Chinmay/workspace/project1/src/page1.html"‌​; 

The attribute is not a URL, but a String. Calling the attribute url will not change that. Then when jep.setPage(url); is called, the method will presume the String represents a File path and deal with it accordingly (or fail accordingly). Since that string does not represent a valid file path, it will fail.

Here is a working code using just 2 buttons, and actual URLs.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;

public class check1 extends JFrame implements ActionListener {

    JEditorPane jep;
    JScrollPane scroll;
    JPanel p, p1;
    JButton b1, b2;
    String url;

    public check1() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        b1 = new JButton("Button 1");
        b1.addActionListener(this);
        b2 = new JButton("Button 2");
        b2.addActionListener(this);

        p = new JPanel();
        p.setLayout(new FlowLayout());

        p1 = new JPanel();
        p1.setLayout(new GridLayout(4, 2, 1, 1));
        p1.add(b1);
        p1.add(b2);

        try {
            jep = new JEditorPane(
                    new URL("http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#constructor.summary"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        scroll = new JScrollPane(jep);
        setLayout(new BorderLayout());
        getContentPane().add(p, BorderLayout.NORTH);
        getContentPane().add(p1, BorderLayout.WEST);
        getContentPane().add(scroll, BorderLayout.CENTER);

        setSize(1000, 800);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        new check1().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(b1)) {
            url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html#constructor.summary";
        } else if (e.getSource().equals(b2)) {
            url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/JApplet.html#constructor.summary";
        }
        try {
            jep.setPage(new URL(url));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}