Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4267432

JEditorPane switches editor from HTML to TEXT on applet reload

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.2.2
    • client-libs



      Name: skT88420 Date: 08/31/99


      Two versions of an applet are included; one for AppletViewer and one for Netscape (requires the 1.2.2 plugin).

      The applet creates a split pane, scrollable on the left with a JTree, and JEditorPane on the right.

      Clicking on a leaf loads an HTML document in the JEditorPane window.

      The applet works until the page is reloaded. Then the JEditor pane displays TEXT instead of interpreting the HTML.

      ***BEGIN CODE (refernced files not included)***

      import javax.swing.*;
      import javax.swing.JApplet;
      import javax.swing.text.EditorKit;
      import javax.swing.JTree;
      import javax.swing.tree.DefaultMutableTreeNode;
      import javax.swing.event.TreeSelectionListener;
      import javax.swing.event.TreeSelectionEvent;
      import javax.swing.tree.TreeSelectionModel;
      import java.net.URL;
      import java.net.MalformedURLException;
      import java.net.*;
      import java.io.*;
      import java.io.IOException;
      import javax.swing.JEditorPane;
      import javax.swing.JScrollPane;
      import javax.swing.JSplitPane;
      import javax.swing.JFrame;
      import java.awt.*;
      import java.awt.event.*;

      public class NetscapeOption extends JApplet {

        private JFrame frame;
        private JEditorPane htmlPane;
        private DefaultMutableTreeNode top;
        
        
        

      public void init() {

      // Force SwingApplet to come up in the System L&F
      String laf = UIManager.getSystemLookAndFeelClassName();

      try {
      UIManager.setLookAndFeel(laf);
      //If you want the Cross Platform L&F instead, comment out the above line and
      //uncomment the following:
      // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      } catch (UnsupportedLookAndFeelException exc) {
      System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
      } catch (Exception exc) {
      System.err.println("Error loading " + laf + ": " + exc);
      };
        
          frame = new JFrame("Netscape Options");
        
          //Create Nodes.
          top = new DefaultMutableTreeNode("Netscape Navigator");
        
        
          //Create a tree allowing one selection at a time
          final JTree tree = new JTree(top);
        
          //Create a scrollpane and add the tree to it
          JScrollPane optionView = new JScrollPane(tree);
          
          //Create the pane to view the HTML
          
          htmlPane = new JEditorPane();
          
          //htmlPane.setContentType("text/html");
          
          //setContentPane didn't work. Trying the hard way.
          EditorKit kit1 = htmlPane.getEditorKitForContentType("text/html");
          htmlPane.setEditorKit(kit1);
          
          
          
          
          htmlPane.setEditable(false);
        
          JScrollPane htmlView = new JScrollPane(htmlPane);
        
          //Add panes to a split pane
          JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
          splitPane.setTopComponent(optionView);
          splitPane.setBottomComponent(htmlPane);
          
          splitPane.setDividerLocation(150);
          splitPane.setPreferredSize(new Dimension(795, 475));
          
          //Add split pane to the frame
          getContentPane().add(splitPane, BorderLayout.CENTER);
          
          tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        //Listen for when selection changes.
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            
          public void valueChanged(TreeSelectionEvent e) {
            
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
              
            if (node == null) return;
              
            Object nodeInfo = node.getUserObject();
            
            System.out.println(nodeInfo);
              
            if (node.isLeaf()) {
            
              //System.out.println("You Clicked on a leaf!!!");
              BookInfo book = (BookInfo)nodeInfo;
              displayURL(book.bookURL);
                
              
                
            }
              
          }
        
        
        
        
        });
        
          frame.pack();
          frame.setVisible(true);
         }
         
         public void start() {
          
          createNodes(top);
          
          
        
              
      }




        
        private class BookInfo {
        
        public String bookName;
        public URL bookURL;
        //public String prefix = "file:" + System.getProperty("user.dir") + System.getProperty("file.separator");
          
        public BookInfo(String book, String filename) {
          
          //System.out.println(getCodeBase());
          
          bookName = book;
          try {
            
            bookURL = new URL(getCodeBase() + filename);
          } catch (java.net.MalformedURLException exc) {
            
            System.err.println("Attempted to create a record with a bad URL: " + bookURL);
            bookURL = null;
          }
        }
          
        public String toString() {
            
         return bookName;
        }
            
      }

        private void displayURL(URL url) {
        
          try {
            
            //Attempts to explicitly set editor
            htmlPane.setContentType("text/html");
            
            //Checks editor type ***NOTICE*** on reload, switches from
            //HTML to TEXT
            System.out.println(htmlPane.getContentType());
            
            htmlPane.setPage(url);
            
          } catch (IOException e) {
          
            System.err.println("Attempted to read a bad URL: " + url);
          }
        }
        
        private void createNodes(DefaultMutableTreeNode top) {
        
          DefaultMutableTreeNode category = null;
          DefaultMutableTreeNode subcategory = null;
          DefaultMutableTreeNode book = null;
          
          category = new DefaultMutableTreeNode("Navigator");
          top.add(category);
          
          subcategory = new DefaultMutableTreeNode("APPEARANCE");
          category.add(subcategory);
          
          book = new DefaultMutableTreeNode(new BookInfo("General", "1navappgen.html"));
          subcategory.add(book);
          
          book = new DefaultMutableTreeNode(new BookInfo("Fonts", "2navappfonts.html"));
          subcategory.add(book);
          
          book = new DefaultMutableTreeNode(new BookInfo("Colors", "3navappcolors.html"));
          subcategory.add(book);
          
          subcategory = new DefaultMutableTreeNode("NAVIGATOR");
          category.add(subcategory);
          
          book = new DefaultMutableTreeNode(new BookInfo("General", "4navnavgen.html"));
          subcategory.add(book);
          
        }
        

      }

      ***END CODE***

      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      java full version "JDK-1.2.2-W"
      (Review ID: 94670)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: