United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 5087208 String contents of JEditorPane disappear when changing Look and Feel
5087208 : String contents of JEditorPane disappear when changing Look and Feel

Details
Type:
Bug
Submit Date:
2004-08-16
Status:
Resolved
Updated Date:
2004-11-09
Project Name:
JDK
Resolved Date:
2004-11-01
Component:
client-libs
OS:
windows_2000
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
6

Related Reports
Backport:

Sub Tasks

Description
Name: dk106046			Date: 08/16/2004

OPERATING SYSTEM(S):
Windows 2000 SP4

FULL JDK VERSION(S):
java version "1.5.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b57)
Java HotSpot(TM) Client VM (build 1.5.0-beta3-b57, mixed mode)

and 

java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)


1. Compile and run the sample code.

     javac JEditorPaneTest.java
     java JEditorPaneTest

  Then a JFrame opens. This frame has a text field and text area in the JEditorPane.

2. Type "aaa" in the text field at the tail of the existing text "default".

3. Type "aaa" in the text area at the tail of the existing text "default".
   
4. Change Look & Feel by selecting Look&Feel Menubar.
   Then the typed string "aaa" in the text field disappears. <---- PROBLEM!!
   On the other hand the "aaa" in the text area remains. 


---------- test.html -----------

<html>
<body>
<form>
<h4>input tag</h4>
<input name="text1" value="default" size="80">
<h4>textarea tag</h4>
<textarea name="text2" rows="10" cols="80">default</textarea>
</form>
</body>
</html>

---------- test.html -----------

---------- JEditorPaneTest.java -----------

import javax.swing.*;
import javax.swing.UIManager;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class JEditorPaneTest extends JFrame{
 private JMenuBar menubar;
 private ButtonGroup lafMenuGroup = new ButtonGroup();

 static{
  UIManager.installLookAndFeel("GTK look and feel",
     "com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
 }
 public static void main(String [] args){
  (new JEditorPaneTest()).show();
 }

 public JEditorPaneTest() {
  super("JEditorPaneTest");
  menubar = new JMenuBar();
  createUISelectMenu();
  this.setJMenuBar(menubar);
  getContentPane().setLayout(new BorderLayout());
  getContentPane().add(new JScrollPane(createJEditorPane()), BorderLayout.CENTER);
  setSize(400,500); 
 }
 
 private Component createJEditorPane(){
  File file = new File("test.html");
  JEditorPane ep = new JEditorPane();
  ep.setContentType("html/text");
  
  try {
   ep.setPage("file:" + file.getAbsolutePath());
  } catch (IOException e) {
   e.printStackTrace();
  }
  return ep;
 }

 public void createUISelectMenu() {
  JMenu menu = new JMenu("Look&Feel");
  JMenuItem item;
  LookAndFeel currentLAF=UIManager.getLookAndFeel();
  String currentLAFname=null;

  menubar.add(menu);
  if (currentLAF != null) currentLAFname=currentLAF.getName();
  UIManager.LookAndFeelInfo[] lafInfo=UIManager.getInstalledLookAndFeels();
  for (int i = 0; i < lafInfo.length; i++){
   item=createLAFMenuItem(menu,lafInfo[i].getName(),lafInfo[i].getClassName());
   if (lafInfo[i].getName().compareTo(currentLAFname) == 0) item.setSelected(true);
  }
  System.out.println("Current Look&Feel: "+ currentLAF.getClass().getName());
 }
 public JMenuItem createLAFMenuItem(JMenu menu, String label, String laf) {
  JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(label));
  lafMenuGroup.add(mi);
  mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
  mi.setEnabled(isAvailableLookAndFeel(laf));

  return mi;
 }
 protected boolean isAvailableLookAndFeel(String laf) {
   try {
    Class lafClass = Class.forName(laf);
    LookAndFeel newLAF = (LookAndFeel)(lafClass.newInstance());
    return newLAF.isSupportedLookAndFeel();
   } catch(Exception e) { // If ANYTHING weird happens, return false
    return false;
   }
  }

 public void setLookAndFeel(String laf) {
  try {
   UIManager.setLookAndFeel(laf);
   SwingUtilities.updateComponentTreeUI(this);
  } catch (Exception ex) {
   System.out.println("Failed loading L&F: " + laf);
   System.out.println(ex);
  }
 }
 class ChangeLookAndFeelAction extends AbstractAction {
  JEditorPaneTest app;
  String laf;
  protected ChangeLookAndFeelAction(JEditorPaneTest app, String laf) {
   this.app = app;
   this.laf = laf;
  }
  public void actionPerformed(ActionEvent e) {
   app.setLookAndFeel(laf);
   System.out.println("Current Look&Feel: "+ laf);
  }
 }

}

---------- JEditorPaneTest.java -----------

======================================================================
###@###.### 10/21/04 17:47 GMT

                                    

Comments
EVALUATION

Name: sh120115			Date: 08/16/2004

This test shows a JEditorPane showing an HTML form. When the user has typed content into a text input area in that form, changing the Look and Feel causes the text to be lost. This should not happen.
###@###.### 2004-08-16

======================================================================
will work on this bug for the next release
###@###.### 2004-08-16

Name: ap153225			Date: 08/19/2004

Text is lost because during recreation of the view sructure
default values for inner HTML components like text, radio and checkboxes
are restored. To manage this behaviour a special property to the HTML 
document
can be added indicating whether the default values to the components 
will be set or not.


======================================================================
###@###.### 2004-11-09 16:22:24 GMT
                                     
2004-11-09



Hardware and Software, Engineered to Work Together