开发者

Java - Cannot load the same url and have it displayed in editor pane twice?

开发者 https://www.devze.com 2023-03-29 20:23 出处:网络
This is the code Ive used to display the contents of a url in an editor pane import javax.swing.*; import java.awt.event.*;

This is the code Ive used to display the contents of a url in an editor pane

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


    public class header extends JFrame {

        private void initComponents() {

            jTextField2 = new JTextField();
            jTextField3 = new JTextField();
            jButton1 = new JButton();
            jScrollPane1 = new JScrollPane();
            jTextArea1 = new JEditorPane();

            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            jButton1.setText("Load");
            jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            jTextArea1.setEditable(false);
            jScrollPane1.setViewportView(jTextArea1);
            setSize(418,300);
            setVisible(true);
            GroupLayout layout = new GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, GroupLayout.Align开发者_Python百科ment.LEADING, GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>

    private void jButton1ActionPerformed(ActionEvent evt) {
        try {
            URL header = new URL(jTextField2.getText());
                jTextArea1.setPage(header);
            }catch(MalformedURLException e){} catch(IOException e){}
        }


        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            header gui = new header();
            gui.initComponents();
        }
        // Variables declaration - do not modify
        private JButton jButton1;
        private JScrollPane jScrollPane1;
        private JEditorPane jTextArea1;
        private JTextField jTextField2;
        private JTextField jTextField3;
        // End of variables declaration
    }

I just havent been able to get it to refresh and display the contents of the same url twice. It loads the contents when I hit the load button but if I hit the load button again it doesnt load the url or display the contents.. What am I doing wrong? I am very new to java so sorry if it is a simple solution, if You could point me in the right direction to solving this issue it would be great, Thanks!


I vaguely seem to remember that you need to force the editor pane to reload the page.

Just guessing, but you can try:

setContentType("");

or

setDocument(null);
0

精彩评论

暂无评论...
验证码 换一张
取 消