?
Solved

Problems with JTextPane

Posted on 2000-02-06
9
Medium Priority
?
1,150 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:venkat2000120699
  • 7
  • 2
9 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 800 total points
ID: 2495789
1. JTextPane.updateUI();
2. Use HTML content in JTextPane, otherwise what you want is a little complicated.
0
 
LVL 2

Author Comment

by:venkat2000120699
ID: 2496102
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
 
LVL 9

Expert Comment

by:Ovi
ID: 2496216
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Author Comment

by:venkat2000120699
ID: 2499282
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
 
LVL 9

Expert Comment

by:Ovi
ID: 2499357
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
 
LVL 9

Expert Comment

by:Ovi
ID: 2499491
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
 
LVL 9

Expert Comment

by:Ovi
ID: 2499517
..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
 
LVL 9

Expert Comment

by:Ovi
ID: 2499555
..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
 
LVL 9

Expert Comment

by:Ovi
ID: 2499620
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month5 days, 9 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question