Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagemm 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(); } } } } }
Post Follow-up to this messageHi 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(); > } > } > } > > } > > }
Post Follow-up to this messageHi 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(); > } > } > } > > } > > }
Post Follow-up to this messagemm 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 - - - - - - - - - - - - - - - - - - - - - - - - -
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.