Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

A Simple jTextPane Problem
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


Report this thread to moderator Post Follow-up to this message
Old Post
mm
09-26-06 09:00 AM


Re: A Simple jTextPane Problem
mm wrote:
> 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

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();
}
}
}

}

}


Report this thread to moderator Post Follow-up to this message
Old Post
Dan Andrews
09-27-06 02:59 AM


Re: A Simple jTextPane Problem
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



Dan Andrews wrote:
> mm wrote: 
>
> 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();
>         }
>       }
>     }
>
>   }
>
> }


Report this thread to moderator Post Follow-up to this message
Old Post
mm
09-28-06 08:59 AM


Re: A Simple jTextPane Problem
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



Dan Andrews wrote:
> mm wrote: 
>
> 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();
>         }
>       }
>     }
>
>   }
>
> }


Report this thread to moderator Post Follow-up to this message
Old Post
mm
09-28-06 08:59 AM


Re: A Simple jTextPane Problem
mm wrote:
> 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.

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
- - - - - - - - - - - - - - - - - - - - - - - - -


Report this thread to moderator Post Follow-up to this message
Old Post
Dan Andrews
09-29-06 02:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Beans archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:15 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.