Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / September 2006

Tip: Looking for answers? Try searching our database.

A Simple jTextPane Problem

Thread view: 
mm - 26 Sep 2006 08:33 GMT
Hi all,

How do I code a jTextPane so that when a user clicks on (let say the
content of the jTextPane is "How are you?") the word "How" (only), that
word will appear in another jTextPane?

Any helps/samples would be greatly appreciated.

Thanks for your time.

MM
Dan Andrews - 27 Sep 2006 05:00 GMT
> Hi all,
>
[quoted text clipped - 7 lines]
>
> MM

Hi MM,

Stop the hate (from your blog) - right on! I put a little quote on my
gmail profile that I you might enjoy. Hope this sample (below) is
useful.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class TextPanesFrame extends JFrame {

 public TextPanesFrame() {
   super("Test Text Panes");
   layoutComponent();
 }

 private void layoutComponent() {
   JPanel panel = new JPanel(new GridLayout(1, 2));
   JTextPane textPane1 = new JTextPane();
   textPane1.setText("Click me");
   textPane1.setPreferredSize(new Dimension(300, 200));
   JTextPane textPane2 = new JTextPane();
   panel.add(new JScrollPane(textPane1));
   panel.add(new JScrollPane(textPane2));
   textPane1.addMouseListener(new ClickedPaneListener(
       textPane1, textPane2));
   textPane2.addMouseListener(new ClickedPaneListener(
       textPane2, textPane1));

   getContentPane().add(panel, BorderLayout.CENTER);
 }

 public static void main(final String[] args)
     throws Exception {
   JFrame frame = new TextPanesFrame();
   frame.pack();
   frame.setVisible(true);
 }

 class ClickedPaneListener extends MouseAdapter {

   private JTextPane fromPane;

   private JTextPane toPane;

   ClickedPaneListener(JTextPane fromPane, JTextPane toPane) {
     this.fromPane = fromPane;
     this.toPane = toPane;
   }

   public void mouseClicked(MouseEvent e) {
     String str = fromPane.getText();
     StringTokenizer tokens = new StringTokenizer(str);
     if (tokens.hasMoreTokens()) {
       String nextString = tokens.nextToken();
       int start = str.indexOf(nextString);
       int end = start + nextString.length();
       if (tokens.hasMoreTokens()) {
         String secondNextString = tokens.nextToken();
         end = start + str.indexOf(secondNextString);
       }
       String insertionString = str.substring(start, end);
       System.out.println("'" + insertionString + "'");
       try {
         Document doc = toPane.getDocument();
         doc.insertString(doc.getLength(),
             insertionString, null);
         doc = fromPane.getDocument();
         doc.remove(0, end);
       } catch (BadLocationException e1) {
         e1.printStackTrace();
       }
     }
   }

 }

}
mm - 28 Sep 2006 05:08 GMT
Hi Dan,

Courage Dan..and yes, it's indeed not too late to build a better
world..:)

Thank you for the sample. Appreciate it. But I think I have missed
something in the first post though. You did answer my question anyway.
Here they are:

1. Let say the content of the jTextPane is "Click Me" (like in your
sample). Now when we click at the word Click only (instead of the whole
pane like the sample), the word Click will appear in the other
jTextPane.
-It may have something to do with hyperlink, I guess.

2. The word Click that we click wont disappear, instead both jTextPanes
will have the word Click.

I'm currently modifying the sample you gave, but if you have the idea,
please tell.

Thanks for your time. :)

MM

> > Hi all,
> >
[quoted text clipped - 104 lines]
>
> }
Dan Andrews - 29 Sep 2006 02:31 GMT
> Hi Dan,
>
[quoted text clipped - 10 lines]
> jTextPane.
> -It may have something to do with hyperlink, I guess.

Have a look at the getCaret(). On a click you will likely want to see
if "dot" changed and is in the middle of a word.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -
mm - 28 Sep 2006 05:13 GMT
Hi Dan,

Courage Dan..and yes, it's indeed not too late to build a better
world..:)

Thank you for the sample. Appreciate it. But I think I have missed
something in the first post though. You did answer my question anyway.
Here they are:

1. Let say the content of the jTextPane is "Click Me" (like in your
sample). Now when we click at the word Click only (instead of the whole
pane like the sample), the word Click will appear in the other
jTextPane.
-It may have something to do with hyperlink, I guess.

2. The word Click that we click wont disappear, instead both jTextPanes
will have the word Click.

I'm currently modifying the sample you gave, but if you have the idea,
please tell.

Thanks for your time. :)

MM

> > Hi all,
> >
[quoted text clipped - 104 lines]
>
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.