Solved

Getting one JtextPane to have same value as another

Posted on 2004-08-04
27
237 Views
Last Modified: 2012-05-05
Hi all,
I have two textPanes, one is editable, the other is not. When a user types in one pane, I want it to appear in the other textpane aswell. I have tried pane2.setText(pane1.getText()) but this is one character too slow. I have also written a custom class for pane2 that makes the processKeyEvent public, I was hoping to just pass the pane1 event onto it. This has no effect however, presumably because there is some kind of filtering going on.
Can someone give me some clues as to how to overcome this problem.

Cheers
DRB2k2
0
Comment
Question by:drb2k2
  • 9
  • 7
  • 6
  • +2
27 Comments
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> tried pane2.setText(pane1.getText()) but this is one character too slow

Do you mean it copies all the text apart from the last character?
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Add a KeyListener to the first pane and on every typed character, do the same in the other pane. It will have some if-elsese - for the back spaces and so on but it should work
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
   textPane2.setDocument( textPane1.getDocument() );
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
Comment Utility
Simply this works:

        pane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pane2.setText(pane1.getText());
            }
        });


Here's a demo:

/*
 * JPaneDemo.java
 *
 */

/**
 *
 * @author  zzynx
 */
public class JPaneDemo extends javax.swing.JFrame {
   
    public JPaneDemo() {
        initComponents();

        pane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pane2.setText(pane1.getText());
            }
        });
    }
   
    private void initComponents() {
        pane1 = new javax.swing.JTextPane();
        pane2 = new javax.swing.JTextPane();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        getContentPane().add(pane1, java.awt.BorderLayout.NORTH);

        pane2.setEditable(false);
        getContentPane().add(pane2, java.awt.BorderLayout.SOUTH);

        pack();
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new JPaneDemo().show();
    }
   
    private javax.swing.JTextPane pane1;
    private javax.swing.JTextPane pane2;
}
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
So, just put what you have in the keyReleased() function of a keylistener on pane1
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
zzynx ,

Actually you just implement what I already posted?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
If you put it in keyTyped() or keyPressed() you're one character too slow indeed
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
what's wrong with my suggestion?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Actually you just implement what I already posted?
Not exactly

>> on every typed character
it doesn't work in keyTyped() or keyPressed()

>> It will have some if-elsese - for the back spaces and so on but it should work
no if-elses needed

But you gave a very good hint, that's true.
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Someone said there is something wrong:) In fact it looks the best for this case...
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> what's wrong with my suggestion?
Nothing, it just works too. ;°)
0
 

Author Comment

by:drb2k2
Comment Utility
Thank you very much zzynx, that worked like a charm.

TimYates' suggestion was good but didn't quite work in the context I had. Its difficult to explain without making it long winded, but basically your suggestion tied the two panes together, so that a change in pane1 would reflect in pane2 which is what I didn't want. only the other way around.

Many thanks to you all though

Drb2k2
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks for accepting
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> TimYates' suggestion was good but didn't quite work in the context I had.

Didn't it?  Can you briefly explain why?

Tim
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>>Can you briefly explain why?
He did ;°)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Ahhh

> so that a change in pane1 would reflect in pane2 which is what I didn't want. only the other way around.

So;

pane2.setDocument( pane1.getDocument() ) ;

didn't work either? :-(
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Tim,

yep... he just wanted A->B and not B->A :(

zzynx ,

>>But you gave a very good hint, that's true.
You may want to give credit to the experts that had posted befor you before they actually tell you :) By the way - your code is good and your explanation which method of the KeyListener to use also is good..

PS: This have nothing to do with points. Just a little respect to all the participants :) It takes a second to be written but... well... let's say it make this place better:)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
:-)  I wasn't having a big moan, just a little one that I got no points at all for what is a one line solution :-(

I agree that zzynx's code is cool :-)

Its the morning...  I always complain in the morning...  Hehehe... ;-)

Tim
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> You may want to give credit to the experts that had posted befor you before they actually tell you :)
Could have done that indeed. But you remarked it without me saying it. I guess others do to ;°)

>> Just a little respect to all the participants :)
Fully agree. Well, not all. [ Although you and Tim are certainly on that list. ;°) ]
E.g. People like objects who doesn't show any respect to me, shouldn't count on my respect for them.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Points could easily have been split between Venabili, Tim and zzynx, Venabili for providing info on how it can be done, Tim also provided info (different one) and zzynx put it all together.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> E.g. People like objects who doesn't show any respect to me, shouldn't count on my respect for them.

I do not think objects has ever done that, apart from being a top expert he is also a top bloke too. Thing is, if you show respect you will receive respect back, and I honestly believe that talking about a person who is not here to take part in the conversion is not fair.
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
girionis ,

It's ok. I am not after points (heh - see I am here from  more than a year and I have less than 50 000 :))) Cleanup and the PE things are more interesting:)

zzynx,
>>But you remarked it without me saying it.
Hm... better not to need to... after all we all work here to help the Askers... and if we respect each other, they will start respect us hopefully (and we will have less abandoned quetsions for example)

>>Although you and Tim are certainly on that list. ;°)
Huh.. thanks :)

>>People like objects who doesn't show any respect to me, shouldn't count on my respect for them.
I have a proposal... let's try to be polite and respectful to everyone and I will try to convince all to be so... I mean... someone should do the first step... let's try to make this area better. Wanna try? ;)

Tim,

Take a cup of coffee and stop grumbling :))

everyone,

Just a side note: Please answer my cleanup pings - If I ask for opinions and had not posted a recommendation but ping, I need such :))

Venabili
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> Take a cup of coffee and stop grumbling :))

:-D  :-D  :-D
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>>I do not think objects has ever done that
That's your experience against mine of course ;°)
>>apart from being a top expert he is also a top bloke too.
For the last part, cf. above. ;°)
>>Thing is, if you show respect you will receive respect back
I fully agree. But somewhere it went wrong between us.
Like it goes sometimes in real life too. (^shoulders and hands up^)

>>I honestly believe that talking about a person who is not here to take part in the conversion is not fair.
Right. I guess it's because I'm just quarreling all morning now with him in different Q's ;°)
But I would have said the same if he was involved here, you know. He knows how I'm thinking about him. ;°)

This said, case closed.

>> they will start respect us hopefully
I hope too. Some do already, some will never. That's life. ;°)

>> let's try to make this area better. Wanna try?
Count me in. (e.g. I'll try to keep quiet when that's better to do)
 ;°)

>>Please answer my cleanup pings
That's what most of the times I do. Making this a better place, remember ;°)
Aaah, I like to see all those abandoned questions getting closed.
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
>I guess it's because I'm just quarreling all morning now with him in different Q's ;°)
Just stop... help the Askers and do not quarrel with experts :) You have no idea how much questions remain unanswered because the experts are too busy to explain each other why a line of code should be different and in this time the poor Asker just disappears...

OK... we started to chat over here:)) So.. back to work...
0
 

Author Comment

by:drb2k2
Comment Utility
Oh my oh my,
what have I started, Ok I feel i'd better clear the air as to why zzynx got all the points.

Venabil
>>Add a KeyListener to the first pane and on every typed character, do the same in the other pane. It will have some if-elsese - for the back spaces and so on but it should work

I had already tired this route
>>I have also written a custom class for pane2 that makes the processKeyEvent public, I was hoping to just pass the pane1 event onto it. This has no effect however, presumably because there is some kind of filtering going on.

but couldn't get it to work.

TimYates' I've already explained.

Zzynx was the first to suggest that I use the keyreleased function. Which was the clincher.
Venabil if you had said use keyreleased you'd have got the points.

But as I mentioned at the bottom of my acceptance post, I am grateful to every post that is made that offers some advice. It can just be a tiny nudge in the right direction that sparks off me finding my own solution. So thank you all, I salute you.

Drb2k2
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> TimYates' I've already explained.

*sigh* so close, except for swapping two variable names... ;-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now