Solved

Getting one JtextPane to have same value as another

Posted on 2004-08-04
27
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 6
  • +2
27 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11713982
> 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
ID: 11714028
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
ID: 11714048
   textPane2.setDocument( textPane1.getDocument() );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 11714090
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
ID: 11714105
So, just put what you have in the keyReleased() function of a keylistener on pane1
0
 
LVL 20

Expert Comment

by:Venabili
ID: 11714106
zzynx ,

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

Expert Comment

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

Expert Comment

by:TimYates
ID: 11714119
what's wrong with my suggestion?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11714126
>> 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
ID: 11714138
Someone said there is something wrong:) In fact it looks the best for this case...
0
 
LVL 37

Expert Comment

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

Author Comment

by:drb2k2
ID: 11724012
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
ID: 11724024
Thanks for accepting
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11724054
> 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
ID: 11724079
>>Can you briefly explain why?
He did ;°)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11724098
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
ID: 11724139
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
ID: 11724152
:-)  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
ID: 11724176
>> 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
ID: 11724197
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
ID: 11724211
> 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
ID: 11724227
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
ID: 11724234
> Take a cup of coffee and stop grumbling :))

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

Expert Comment

by:zzynx
ID: 11724290
>>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
ID: 11724368
>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
ID: 11733734
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
ID: 11733764
> TimYates' I've already explained.

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

724 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