-
Bug
-
Resolution: Fixed
-
P1
-
1.2.0
-
swing1.0fcs
-
sparc
-
solaris_2.5.1
> > Hi, I'm having trouble when deleting components from a JTextPane editor.
> > This is a bug reported against earlier versions of Swing and it is said
> > to be fixed after 0.5, but I don't think so. Try the code below and
> > you'll see that removing components doesn't work. (What I mean is
> > removing a component embedded in the text with the backspace or delete
> > key.)
> >
> > Environment:
> > Windows 95
> > Java VM version 1.1.5
> > Swing version 0.7
> >
> > Regards,
> >
> > Andreas Olsson, BTJ
> >
> > import com.sun.java.swing.*;
> > import com.sun.java.swing.basic.*;
> > import com.sun.java.swing.text.*;
> > import java.awt.*;
> > import java.awt.event.*;
> >
> > class TestPane2 extends JPanel {
> > JTextPane editor;
> > StyledDocument document;
> > JToolBar toolbar;
> >
> > public TestPane2(){
> > super(new BorderLayout());
> > editor=new JTextPane();
> > StyledEditorKit editKit = new StyledEditorKit();
> > document = (StyledDocument)editKit.createDefaultDocument();
> > editor.setStyledDocument(document);
> > editor.setText("Here's some text. The toolbar contains two
> > buttons.\n"+
> > "Both insert a JTextField, one of them using
> > the\n"+
> > "insertComponent(...) method in JTextPane and
> > the\n"+
> > "other using the insertString(...) method in
> > Document.\n"+
> > "Both methods inserts the component correctly,
> > but you\n"+
> > "can't remove it! (The placeholding space is
> > removed and\n"+
> > "the component seems to be removed from the
> > model,\n"+
> > "but not from the view.)\n");
> > toolbar=new JToolBar();
> > ((BasicToolBarUI)toolbar.getUI()).setFloatable(false);
> > JButton butt=new JButton("insertComponent");
> > butt.addMouseListener(new MouseAdapter(){
> > public void mouseClicked(MouseEvent e){
> > addComponents();
> > }
> > });
> > JButton butt2=new JButton("insertString");
> > butt2.addMouseListener(new MouseAdapter(){
> > public void mouseClicked(MouseEvent e){
> > addString();
> > }
> > });
> > toolbar.add(butt);
> > toolbar.add(butt2);
> > this.add(toolbar, BorderLayout.NORTH);
> > this.add(editor, BorderLayout.CENTER);
> >
> > }
> >
> > public void addComponents(){
> > editor.insertComponent(new JTextField(20));
> > editor.validate();
> > editor.repaint();
> >
> > }
> >
> > public void addString(){
> > MutableAttributeSet a=new SimpleAttributeSet();
> > StyleConstants.setComponent(a, new JTextField(20));
> > try {
> > document.insertString(editor.getCaretPosition(), " ", a);
> > } catch (Exception ex){
> > ex.printStackTrace();
> > }
> > editor.validate();
> > editor.repaint();
> > }
> >
> > public static void main(String[] args){
> > JFrame frame=new JFrame();
> > frame.setSize(400, 400);
> > frame.getContentPane().add(new TestPane2());
> > frame.setVisible(true);
> > }
> > }
> > This is a bug reported against earlier versions of Swing and it is said
> > to be fixed after 0.5, but I don't think so. Try the code below and
> > you'll see that removing components doesn't work. (What I mean is
> > removing a component embedded in the text with the backspace or delete
> > key.)
> >
> > Environment:
> > Windows 95
> > Java VM version 1.1.5
> > Swing version 0.7
> >
> > Regards,
> >
> > Andreas Olsson, BTJ
> >
> > import com.sun.java.swing.*;
> > import com.sun.java.swing.basic.*;
> > import com.sun.java.swing.text.*;
> > import java.awt.*;
> > import java.awt.event.*;
> >
> > class TestPane2 extends JPanel {
> > JTextPane editor;
> > StyledDocument document;
> > JToolBar toolbar;
> >
> > public TestPane2(){
> > super(new BorderLayout());
> > editor=new JTextPane();
> > StyledEditorKit editKit = new StyledEditorKit();
> > document = (StyledDocument)editKit.createDefaultDocument();
> > editor.setStyledDocument(document);
> > editor.setText("Here's some text. The toolbar contains two
> > buttons.\n"+
> > "Both insert a JTextField, one of them using
> > the\n"+
> > "insertComponent(...) method in JTextPane and
> > the\n"+
> > "other using the insertString(...) method in
> > Document.\n"+
> > "Both methods inserts the component correctly,
> > but you\n"+
> > "can't remove it! (The placeholding space is
> > removed and\n"+
> > "the component seems to be removed from the
> > model,\n"+
> > "but not from the view.)\n");
> > toolbar=new JToolBar();
> > ((BasicToolBarUI)toolbar.getUI()).setFloatable(false);
> > JButton butt=new JButton("insertComponent");
> > butt.addMouseListener(new MouseAdapter(){
> > public void mouseClicked(MouseEvent e){
> > addComponents();
> > }
> > });
> > JButton butt2=new JButton("insertString");
> > butt2.addMouseListener(new MouseAdapter(){
> > public void mouseClicked(MouseEvent e){
> > addString();
> > }
> > });
> > toolbar.add(butt);
> > toolbar.add(butt2);
> > this.add(toolbar, BorderLayout.NORTH);
> > this.add(editor, BorderLayout.CENTER);
> >
> > }
> >
> > public void addComponents(){
> > editor.insertComponent(new JTextField(20));
> > editor.validate();
> > editor.repaint();
> >
> > }
> >
> > public void addString(){
> > MutableAttributeSet a=new SimpleAttributeSet();
> > StyleConstants.setComponent(a, new JTextField(20));
> > try {
> > document.insertString(editor.getCaretPosition(), " ", a);
> > } catch (Exception ex){
> > ex.printStackTrace();
> > }
> > editor.validate();
> > editor.repaint();
> > }
> >
> > public static void main(String[] args){
> > JFrame frame=new JFrame();
> > frame.setSize(400, 400);
> > frame.getContentPane().add(new TestPane2());
> > frame.setVisible(true);
> > }
> > }