Getting one JtextPane to have same value as another

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
drb2k2Asked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
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
 
girionisCommented:
> 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
 
VenabiliCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
TimYatesCommented:
   textPane2.setDocument( textPane1.getDocument() );
0
 
zzynxSoftware engineerCommented:
So, just put what you have in the keyReleased() function of a keylistener on pane1
0
 
VenabiliCommented:
zzynx ,

Actually you just implement what I already posted?
0
 
zzynxSoftware engineerCommented:
If you put it in keyTyped() or keyPressed() you're one character too slow indeed
0
 
TimYatesCommented:
what's wrong with my suggestion?
0
 
zzynxSoftware engineerCommented:
>> 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
 
VenabiliCommented:
Someone said there is something wrong:) In fact it looks the best for this case...
0
 
zzynxSoftware engineerCommented:
>> what's wrong with my suggestion?
Nothing, it just works too. ;°)
0
 
drb2k2Author Commented:
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
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
TimYatesCommented:
> TimYates' suggestion was good but didn't quite work in the context I had.

Didn't it?  Can you briefly explain why?

Tim
0
 
zzynxSoftware engineerCommented:
>>Can you briefly explain why?
He did ;°)
0
 
TimYatesCommented:
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
 
VenabiliCommented:
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
 
TimYatesCommented:
:-)  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
 
zzynxSoftware engineerCommented:
>> 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
 
girionisCommented:
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
 
girionisCommented:
> 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
 
VenabiliCommented:
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
 
TimYatesCommented:
> Take a cup of coffee and stop grumbling :))

:-D  :-D  :-D
0
 
zzynxSoftware engineerCommented:
>>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
 
VenabiliCommented:
>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
 
drb2k2Author Commented:
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
 
TimYatesCommented:
> TimYates' I've already explained.

*sigh* so close, except for swapping two variable names... ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.