A Simple jTextPane Problem
5 Message(s) by 2 Author(s) originally posted in java beans
| From: mm |
Date: Tuesday, September 26, 2006
|
Hi all,
How do I
code a jTextPane so that when a user
click s 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/
sample s'd be greatly appreciated.
Thanks for your time.
MM
| From: Dan Andrews |
Date: Wednesday, September 27, 2006
|
wrote in message:
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'd 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.
Doc ument;
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();
}
}
}
}
}
| From: mm |
Date: Thursday, September 28, 2006
|
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've 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've the idea,
please tell.
Thanks for your time. :)
MM
wrote in message:
wrote in message:
> 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'd 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();
}
}
}
}
}
| From: mm |
Date: Thursday, September 28, 2006
|
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've 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've the idea,
please tell.
Thanks for your time. :)
MM
wrote in message:
wrote in message:
> 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'd 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();
}
}
}
}
}
| From: Dan Andrews |
Date: Thursday, September 28, 2006
|
wrote in message:
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've 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'll 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
- - - - - - - - - - - - - - - - - - - - - - - - -
Next Message: JSP and JAVABean problem
Blogs related to A Simple jTextPane Problem
A tale of two GUIs.
But is it as
simple as that? Logically it must be, because that is all a computer
... So why were web-based GUIs so
simple to test (with HTMLUnit, HTTPUnit, etc),
... The
problem is quite simply that the View and the Model of Swing's
...
Thread Checking Swing
I’m getting more and more the feeling that annotating the
Java API side, primarily Swing,
... JTextPane.insertLogicalStyle.
JTextPane.setCharacterAttributes
... To avoid the possibility of thread
problems, we recommend that you use
...
java
Java solved this
problem by using a virtual machine which is a microprocessor
... In order to make creating documentation for
Java programs as
simple,
... this class does is to instantiate a
JTextArea object on line 73 and initialize it
...
Swing and Roundabouts 1M: Emission DTs
This article might be debunked over time at this permalink on
java.net CVS.
... We implement a service to append info messages to a
JTextPane console as
... only
problem is that it hasn't turned out to be as minimal as i planned, D'oh!
...
Dev Shed Forums - JFrame JTextArea Population Problem
Date: August 16th, 2006 06:52 PM - gregt78 - Untitled Post: Thanks so much for your help. I got it working. It's not the prettiest, but it works. For reference I have included the working version below. Hope it can serve some benefit to
...
loan payments
balance (that's how it's done in Ruritania, you got a
problem with that?). Now you could apply another
... JTextArea: - How many payments it took to pay the loan
... import
java.text.*;// For Decimal Format Class public class loan
...