Problems with JTextPane

I am facing some problems with JTextPane.

1. I am able to insert components(say JLabels) into JTextPane using insertComponent() method.  When I press delete button or backspace button they are getting deleted.  But when I select a region on JTextPane which contains 3-4 JLabels, and press some key; the text in the selected region is deleted but not the labels.  How can I delete the JLabels in a selected region? How can I determine whether a component is within the selected region or not?

I have tried with setComponent() method also instead of insertComponent().  But the problem is the same.

Following is the code I wrote to remove components in selected region.  But it is not working?

public void keyPressed(KeyEvent e)
{
       char c = e.getKeyChar();
       
        if(c == KeyEvent.VK_BACK_SPACE)
        {
            int start = m_txpEditor.getSelectionStart();
            int end = m_txpEditor.getSelectionEnd();
            System.out.println("Selected area being read");
            System.out.println(start + ";" + end);
            if(start == end)
                return;
            for(int i=start;i<end;i++)
            {
                //System.out.println("in for " + i);
                m_txpEditor.setCaretPosition(i);
                AttributeSet as = m_txpEditor.getCharacterAttributes();
                JLabel comp=null;
                if((StyleConstants.getComponent(as)) != null)
                {
                   
                    comp = (JLabel)StyleConstants.getComponent(as);
                    m_txpEditor.removel(comp);
                    //m_txpEditor.remove(0);
                }//end if  
            }//end for
            m_txpEditor.replaceSelection("");
            m_txpEditor.repaint();
            //m_txpEditor.updateUI();
            return;
        }//end for
}//end of keyPressed()

2. How can I implement bulleted lists(ordered, unordered) using JTextPane as in MS-Word?

Please provide suitable examples in your answers.

Thanks in advance.
LVL 2
venkat2000120699Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OviCommented:
1. JTextPane.updateUI();
2. Use HTML content in JTextPane, otherwise what you want is a little complicated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
venkat2000120699Author Commented:
Hi Ovi,

1. As you can see, I have already tried with updateUI() but it didn't work(It is a comment in my example.)

2. I am giving you a little clarification about the second part of my question.  What I want is, I should have two buttons for bulleted lists(ordered and unordered) as in MS-Word and the content of JTextPane should behave just like MS-Word content on button clicks, key presses, mouse clicks, and mouse drags. I need more clarification on your answer.

If you can give me an example code, it will be helpful.

Thank you
0
OviCommented:
For problem 1 try this :


import javax.swing.*;
import javax.swing.text.*;

public class Pane extends JFrame {
      public JTextPane pane;
      public JToolBar tlb;
      
      public Pane(String title) {
            super(title);
            
            pane = new JTextPane();
            pane.setText("sdf sd f sdf sdaf asdf asdf asdf sad fs adf asdf asdf sd fasd f asdf sadf sadf sd f sdf"+
            "asdf sd fsd fasd f sad fasd f sd fasd f sd f asdf sd f sdf sd f sdf sd f sdf sd fs df sad fsad f sdf"+
            " uh hu jih uih iuhjiujfpsiojoi joi joi joisdjfoij oj oijos ijsdofijo jos joisdjfoij ojkm ojs odifjoij");
            pane.setCaretPosition(3);
            pane.insertComponent(new JButton("1"));
            pane.setCaretPosition(9);
            pane.insertComponent(new JButton("2"));
            pane.setCaretPosition(15);
            pane.insertComponent(new JButton("3"));
            pane.setCaretPosition(18);
            pane.insertComponent(new JButton("4"));
            pane.setCaretPosition(20);
            pane.insertComponent(new JButton("5"));
            pane.setCaretPosition(33);
            pane.insertComponent(new JButton("6"));
            JScrollPane sp = new JScrollPane(pane);
            this.getContentPane().add(sp);
            
      }

      public static void main(String arg[]) {
            Pane p = new Pane("Example");
            p.setSize(400, 400);
            p.setLocation(100, 100);
            p.setVisible(true);
      }
}

For problem 2: If you set your text pane to load (editables) html pages you can partial resolve the problem of lists using html lists, and finaly by inserting html content for adding new elements.

If you want to use StyledEditorKit for styled content, I think the best solution for you is to create a custom JPanel wich contains an icon(bullet), and a JTextField wich will contain the text. You will add this in your editor and also keep a Vector structure (optional) for keepink track of the new custom components. You must be careful about the length of text, and automatic insertion of CR characters in your text.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

venkat2000120699Author Commented:
1. updateUI() is working fine with a slight modification in my code.
Instead of m_txpEditor.replaceSelection(""), I have used m_txpEditor.replaceSelection(null);
m_txpEditor.repaint();
m_txpEditor.updateUI();

Any way, I can say that your first answer is correct.

2. I am trying to develop MS-Word kind of thing using JTextPane.  I have almost implemented the remianing functionalities except bulleted lists.  Could you please provide an example implementing those functionalities using JTextPane.

How is HTML related to my problem?

Thank you.
0
OviCommented:
As you know, JTextPane extends JEditorPane, which by default knows how to handle HTML content. The ideea is that you can use HTML bullet lists instead of text lists, of course only if you think this is a good choice for you. You will be able to create on WYSWYG html editor....

Regarding customs bullet lists I don't have any code for this, the solution gived by me is only a solution to your purpose. But I will try to provide to you soon some code. My point is to create a custom object (panel) which will store an element of your list.
0
OviCommented:
Here is an short example of what I meen, using custom components :


////////////////////////////////////////

import javax.swing.*;
import javax.swing.text.*;

public class Pane extends JFrame {
      public JTextPane pane;
      public JToolBar tlb;
      
      public Pane(String title) {
            super(title);
            
            pane = new JTextPane();
            pane.setText("This is an example of an simple implementation of a bullet list like MSOffice jlskdjflsdfklj"+
            "sjadhfjksd fkjsdk jhskdjflksdjlkfjlsdkj ldkjslkfjkldsjflkjsdl kjlkjdflkjsdlkfjlksdjflkdnsvcn knlklk"+
            " uh hu jih uih iuhjiujfpsiojoi joi joi joisdjfoij oj oijos ijsdofijo jos joisdjfoij ojkm ojs odifjoij");
            insertListElement(3);
            insertListElement(6);
            insertListElement(9);
            insertListElement(12);
            insertListElement(15);
            JScrollPane sp = new JScrollPane(pane);
            this.getContentPane().add(sp);
            
      }

      private void insertListElement(int pos) {
            String tmp = "";
            pane.setCaretPosition(pos);
            pane.select(pos, pos+1);
            tmp = pane.getSelectedText();
            pane.replaceSelection(tmp+"\n");
            pane.setCaretPosition(pos+2);
            pane.insertComponent(new BulletElement());
            pane.setCaretPosition(pos+3);
            pane.select(pos+3, pos+4);
            tmp = pane.getSelectedText();
            pane.replaceSelection(tmp+"\n");
            pane.setCaretPosition(pos+4);
      }
      
      public static void main(String arg[]) {
            Pane p = new Pane("Example");
            p.setSize(800, 400);
            p.setLocation(100, 100);
            p.setVisible(true);
      }
}
/////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////

import java.awt.Dimension;
import java.awt.Color;
import javax.swing.*;

public class BulletElement extends JPanel {
      JRadioButton rb = new JRadioButton("", true);
      JTextField tf = new JTextField("                                                        ");
      public BulletElement() {
            super();
            setBackground(Color.white);
            rb.setBackground(Color.white);
            rb.setEnabled(false);
            add(rb);
            add(tf);
      }
}

////////////////////////////

0
OviCommented:
..and here is the HTML example, more concludent :

////////////////////////////////////////

import javax.swing.*;
import javax.swing.text.*;

public class Pane extends JFrame {
      public JTextPane pane;
      public JToolBar tlb;
      
      public Pane(String title) {
            super(title);
            
            pane = new JTextPane();
            try {
                  pane.setPage("http://povi/ex.html");
            }catch(Exception e) {
                  System.out.println("Page not found");
            }
            JScrollPane sp = new JScrollPane(pane);
            this.getContentPane().add(sp);
            
      }
      
      public static void main(String arg[]) {
            Pane p = new Pane("Example");
            p.setSize(800, 400);
            p.setLocation(100, 100);
            p.setVisible(true);
      }
}

////////////////////////////////

///////////////////////ex.html/////////////////////////////

<html>
<body>

Here is your HTML example.
The following is an ordered list:
<OL>
<LI> Item number one.
<LI> Item number two.
<LI> Item number three.
</OL>

And here's an unordered list:
<UL>
<LI> First item.
<LI> Second item.
<LI> Third Item.
</UL>
</body>
</html>

///////////////////////////////////////////

Be sure to update before compiling the line : pane.setPage("http://povi/ex.html") to your path to ex.html
Note :

If you are using html content, you benefit also for facilities offered by styled text classes.

Enjoy...
0
OviCommented:
..and to be more clear, you don't have a default solution provided by Swing to implement such list in styled text, only html lists in html content
0
OviCommented:
If you have problems with editing html content (in my example is not the case) add pane.setEditable(true); after you load the page.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.