Sean Fernandes
Sean Fernandes

Reputation: 11

Java Swing, setPage() does not change the JEditorPane

I have two classes, one for the buttons, text field and action listeners for the buttons, and one containing the JEditorPane and the methods the action listeners call.

The problem I am having is with the setPage method not actually setting the page, so the JEditorPane is not updated.

The scenario is as follows:

I would expect this to change the JEditorPane, but it doesn't. I have a working version but I am splitting the functionality and interface into different classes.

I have researched before, finding no real answers except using:

Document doc = jep.getDocument(); 

doc.putProperty(Document.StreamDescriptionProperty, null);

To summarize my problem is that the setPage(url) doesn't seem to change the JEditorPane. The following is the code for a example of the problem

ToolBar

public class ToolBar {

    private JToolBar toolBar;
    private JTextField field;
    private JButton go;
    private Editor editor = new Editor();
    
    ToolBar() {
        toolBar = new JToolBar();
        
        field = new JTextField(50);
        toolBar.add(field);

        go = new JButton("Go");
        toolBar.add(go);
        
        go.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    editor.visitPage(new URL(field.getText()));
                } catch(MalformedURLException murle) {
                    
                }
                
            }
            
        });
    }

    public JToolBar getToolBar() {
        return toolBar;
    }
}

Editor

public class Editor {

    private JEditorPane jep;
    private JScrollPane jsp;
    
    private static final int EDITOR_WIDTH = 500;
    private static final int EDITOR_HEIGHT = 500;
    
    Editor() {
        jep = new JEditorPane();
        jep.setEditable(false);
        jsp = new JScrollPane(jep);
        jep.setPreferredSize(new Dimension(EDITOR_WIDTH,EDITOR_HEIGHT));
        
        
    }
    
    public void visitPage(URL url) {
        try {
            System.out.println("Going to: "+url);
            jep.setPage(url);
        } catch(IOException ioe) {
            JOptionPane.showMessageDialog(null,ioe);
        }
    }
    
    public JScrollPane getJScrollPane() {
        return jsp;
    }
}

Window

public class Window extends JFrame{

    private JPanel topPanel;
    ToolBar toolBar = new ToolBar();
    Editor editor = new Editor();

    Window() {
        super("Web Browser");
        init();
    }

    private void init() {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());

        add(topPanel, BorderLayout.NORTH);
        getContentPane().add(editor.getJScrollPane());

        topPanel.add(toolBar.getToolBar());

        pack();
        setVisible(true);
    }
}

public class Main {

    public static void main(String[] args){
        Window webBrowser = new Window();
    }
}

Upvotes: 1

Views: 291

Answers (0)

Related Questions