-
Bug
-
Resolution: Not an Issue
-
P5
-
None
-
5.0
-
x86
-
linux
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
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)
ADDITIONAL OS VERSION INFORMATION :
Linux micron.upmc.edu 2.4.20-20.9 #1 Mon Aug 18 11:45:58 EDT 2003 i686 i686 i386 GNU/Linux
Microsoft Windows 2000 [ Version 5.0.2195]
A DESCRIPTION OF THE PROBLEM :
You have "Hello World" string in JTextPane.
You use DefaultStyledDocument.setCharacterAttributes() to place a JLabel("Hello") on top of "Hello"
Then for some reason you want to use DefaultStyledDocument.setCharacterAttributes() to place JLabel("Hello World") on top of "Hello World".
Theoreticly if boolean 'replace' is true, that should work. Old component covering "Hello" should dissapear, and new component that covers "Hello World" should be at its place.
This doesn't happen. The new JLabel appears next to the previous one.
The result looks like this:
HelloHello World.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile the example I provided
run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When "Action" button is pressed. Hello World should turn into JLabel.
ACTUAL -
There are two JLabels
HelloHello World.
As a metter of fact, in java 1.4, when you try to delete the resulting strings, you get NoSuchChildExceptions
ERROR MESSAGES/STACK TRACES THAT OCCUR :
n/a
It is
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextEditTest extends JFrame {
private DefaultStyledDocument doc;
public TextEditTest(){
super("TextEditTest");
doc = new DefaultStyledDocument();
JTextPane text = new JTextPane();
text.setDocument(doc);
text.setText("hello world");
// highlight first word
doc.setCharacterAttributes(0,5,createLabelAttribute("hello"),true);
JButton button = new JButton("Action");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String text= doc.getText(0,11);
doc.setCharacterAttributes(0,11,createLabelAttribute("hello world"),true);
}catch(BadLocationException ex){
ex.printStackTrace();
}
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(text,BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.SOUTH);
pack();
}
private AttributeSet createLabelAttribute(String text){
JLabel lbl = new JLabel(text);
lbl.setToolTipText("This is the reason to put a label here");
lbl.setAlignmentY(.8f);
lbl.setMaximumSize(lbl.getPreferredSize());
lbl.setForeground(Color.red);
// add create attr set
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setComponent(attr,lbl);
//StyleConstants.setBold(attr,true);
return attr;
}
public static void main(String [] args){
(new TextEditTest()).setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The desired effect can be accomplished by using
doc.replace() method.
This is the workaround I am using now in my application.
###@###.### 2005-05-09 09:54:40 GMT
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
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)
ADDITIONAL OS VERSION INFORMATION :
Linux micron.upmc.edu 2.4.20-20.9 #1 Mon Aug 18 11:45:58 EDT 2003 i686 i686 i386 GNU/Linux
Microsoft Windows 2000 [ Version 5.0.2195]
A DESCRIPTION OF THE PROBLEM :
You have "Hello World" string in JTextPane.
You use DefaultStyledDocument.setCharacterAttributes() to place a JLabel("Hello") on top of "Hello"
Then for some reason you want to use DefaultStyledDocument.setCharacterAttributes() to place JLabel("Hello World") on top of "Hello World".
Theoreticly if boolean 'replace' is true, that should work. Old component covering "Hello" should dissapear, and new component that covers "Hello World" should be at its place.
This doesn't happen. The new JLabel appears next to the previous one.
The result looks like this:
HelloHello World.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile the example I provided
run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When "Action" button is pressed. Hello World should turn into JLabel.
ACTUAL -
There are two JLabels
HelloHello World.
As a metter of fact, in java 1.4, when you try to delete the resulting strings, you get NoSuchChildExceptions
ERROR MESSAGES/STACK TRACES THAT OCCUR :
n/a
It is
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextEditTest extends JFrame {
private DefaultStyledDocument doc;
public TextEditTest(){
super("TextEditTest");
doc = new DefaultStyledDocument();
JTextPane text = new JTextPane();
text.setDocument(doc);
text.setText("hello world");
// highlight first word
doc.setCharacterAttributes(0,5,createLabelAttribute("hello"),true);
JButton button = new JButton("Action");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String text= doc.getText(0,11);
doc.setCharacterAttributes(0,11,createLabelAttribute("hello world"),true);
}catch(BadLocationException ex){
ex.printStackTrace();
}
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(text,BorderLayout.CENTER);
getContentPane().add(button,BorderLayout.SOUTH);
pack();
}
private AttributeSet createLabelAttribute(String text){
JLabel lbl = new JLabel(text);
lbl.setToolTipText("This is the reason to put a label here");
lbl.setAlignmentY(.8f);
lbl.setMaximumSize(lbl.getPreferredSize());
lbl.setForeground(Color.red);
// add create attr set
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setComponent(attr,lbl);
//StyleConstants.setBold(attr,true);
return attr;
}
public static void main(String [] args){
(new TextEditTest()).setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The desired effect can be accomplished by using
doc.replace() method.
This is the workaround I am using now in my application.
###@###.### 2005-05-09 09:54:40 GMT