Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4245401
Votes 2
Synopsis JTextPane with HTMLEditorKit causes strange editing for HEAD, BODY tag
Category java:classes_swing
Reported Against 1.1.6
Release Fixed 1.3(kestrel-beta)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs
Submit Date 10-JUN-1999
Description
The head is now represented as a block element. The view that is created for this will not display anything, so that the user esentially can't edit it.
 xxxxx@xxxxx  1999-06-21




With both Swing 1.1 and Swing 1.1.1 beta 2. Using JTextPane with HTMLEditorKit. Users are able to move the cursor to the beginning of the editor, and delete and spoil up HTML tags such as HEAD. Pressing enter key produces repeating HEAD tags. Also typing will be lost in those caret positions.

Example:

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class BugTest extends JFrame {


	static JTextPane ep = null;


	public BugTest () {

		//try {
		//	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		//} catch (Exception e ) {
		//	System.err.println(e);
		//}

		ep = new JTextPane();
		ep.setEditable(true);
		ep.setContentType("text/html");
		HTMLEditorKit kit = (HTMLEditorKit) ep.getEditorKit();
		ep.setEditorKit(kit);
		HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
		ep.setDocument(doc);

		try {
			String text = "HTML Test... Test is a test...";
			kit.read(new StringReader(text), doc, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.getContentPane().add(ep);

	}


	public static void main (String args[]) {

		BugTest bugTest = new BugTest();

		bugTest.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			System.exit(0);
		}});

		bugTest.setSize(400, 400);
		bugTest.setVisible(true);
	}

}


With this code:
1) Place the cursor at the "H" of the word "HTML" (Before the H).
2) Press cursor up once.
3) Press enter, extra HEAD tag can be reproduced.
4) Place the cursor at the "H" again.
5) Press backspace twice. HEAD tag can also be deleted.
(Review ID: 84177) 
======================================================================
Work Around




Checking caret position and prevent user from entering those position. (Monitor in CaretUpdate event, caret position seems to be 3 after BODY tag). Also backspace will need to be blocked to avoid deleting of tags (Blocking backspace in KeyPressed upon a KeyEvent).

The code will look something like:

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class BugTest extends JFrame implements CaretListener, KeyListener {


	static JTextPane ep = null;


	public BugTest () {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e ) {
			System.err.println(e);
		}

		ep = new JTextPane();
		ep.setEditable(true);
		ep.setContentType("text/html");
		HTMLEditorKit kit = (HTMLEditorKit) ep.getEditorKit();
		ep.setEditorKit(kit);
		HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
		ep.setDocument(doc);

		try {
			String text = "HTML Test... Test is a test...";
			kit.read(new StringReader(text), doc, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.getContentPane().add(ep);
		ep.addCaretListener(this);
		ep.addKeyListener(this);
		
	}


	public static void main (String args[]) {

		BugTest bugTest = new BugTest();

		bugTest.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			System.exit(0);
		}});

		bugTest.setSize(400, 400);
		bugTest.setVisible(true);
	}


	public void caretUpdate(CaretEvent e) {		
		if(e.getDot() < 3) {
			ep.setCaretPosition(3);
		}
	}

	
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
			if(ep.getCaretPosition() <= 3) {
				e.setKeyCode(KeyEvent.VK_UNDEFINED);
			}
		}
	}

	
	public void keyTyped(KeyEvent e) {
	}

	
	public void keyReleased(KeyEvent e) {
	}
	

}
======================================================================
Evaluation
The head is now represented as a block element. The view that is created for this will not display anything, so that the user esentially can't edit it.
 xxxxx@xxxxx  1999-06-21
Comments
  
  Include a link with my name & email   

Submitted On 10-DEC-1999
v2cib198
Why are the head tag blocks appearing in the JTextPane.  Is their a was to get
rid of them?
Thanks, Trevor Greene
trevor@ancept.com



PLEASE NOTE: JDK6 is formerly known as Project Mustang