tags:

views:

161

answers:

5

Hi guys,

Bit of a strange one.

I want to have a JTextField, in which the user will type a string. While typing, however, I'd like that text to automatically print to another JTextField in real time.

I'm not sure if this is possible because I can't recall seeing any application do it.

Anyone even seen anything like this before?

Actually, now that I open my eyes a bit, I see that stackoverflow does it.

Are there any known ways of implementing in Java?

+8  A: 

You might be able to give the fields the same document instance. For the document you could use one of the classes that swing provides or you could extend your own. The document is the model of the text field.

Alternatively you could use listeners to do the updating. There are many things you can listen and it depends on your needs what suits best. You can listen the document, you can listen keyboard and mouse events, you can listen for action events. (Action events happen in this kind of fields when pressing enter or focus is lost.)

iny
Do you have any code sample for the "same document" instance? :)
OscarRyz
Document doc = new PlainDocument(); JTextField a = new JTextField() {{ setDocument(doc); }}; JTextField b = new JTextField() {{ setDocument(doc); }};
Tom Hawtin - tackline
(Better add a final onto that doc declaration.)
Tom Hawtin - tackline
A: 

You could use the KeyListener interface, and on each keyTyped event you copy the text to the "duplicate" field.

Tomas Lycken
+1  A: 

You could add an action listener for the jTextField's key released action. eg:

  jTextField1.addKeyListener(new java.awt.event.KeyAdapter() 
    {
      public void keyReleased(java.awt.event.KeyEvent evt) 
      {
       jTextField1KeyReleased(evt);
      }
     });

  private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) 
   {       
    jTextField2.setText(jTextField1.getText());
   }
instanceofTom
+3  A: 

Add an ActionListener, as this will respond for any action changing the text (not just key presses, but also mouse-driven cut-paste). Code not tested...

// changing textField1 updates textField2
textField1.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        textField2.setText(textField1.getText());
    }
});
Dan Vinton
AFAIK, This won't refresh field2 dynamically but only when user types ENTER.
jfpoilpret
@jfpoilpret - this may be Java version specific, but I used this method under Java 6 on Linux and I think that events were raised every time the text changed (although memory isn't what it was...)
Dan Vinton
+6  A: 

The "same document" approach is the way to go.

Here's some sample code in Groovy (translation to Java is left as an exercise to the reader):

import javax.swing.*
import java.awt.FlowLayout

f = new JFrame("foo")
t1 = new JTextField(10)
t2 = new JTextField(10)
t2.document = t1.document
f.contentPane.layout=new FlowLayout()
f.contentPane.add(t1)
f.contentPane.add(t2)
f.pack()
f.show()
Joachim Sauer
The "same instance" approach works in other GUI areas too, such as getting two lists/tables to scroll together in a `JScrollPane`
oxbow_lakes