CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   JTextField problem (http://www.codingforums.com/showthread.php?t=273518)

Atlan 09-19-2012 02:25 AM

JTextField problem
 
Hi everyone

I'm making a small program using Java. As part of this, the user is able to enter text in a JTextField (in a JPanel).

My problem is, the only way that the actionlistener will activate is if a person entering text presses 'enter' on their keyboard. This works for testing, but for the actual program, it's counter-intuitive.

Does anyone know a better way of entering text? Ideally it would update everytime the user enters even a single letter.

RichardD 09-19-2012 05:58 AM

I think a KeyListener would do the job (http://docs.oracle.com/javase/1.4.2/...yListener.html), instead of the actionlistener.

Atlan 09-21-2012 03:19 AM

You were right about the KeyListener, it solved my problems.

Unfortunately, it also created one. The Backspace command it wonky.

Here's how it works:
(text in text box) (text stored in the class)
name name
nam nam (I typed in 'backspace')
namw nam (I typed in 'w', but it didn't get stored in the class)
namwg namw (I typed in 'g', and the 'w' appeared, but not the 'g')

For some reason, after I type in backspace, the text box permanently loses a character when the KeyListener activates.

Atlan 09-21-2012 04:26 AM

It really is. Anyone know whats going wrong? And how to fix it?

Fou-Lu 09-21-2012 06:14 AM

Is the text in the class always supposed to match the text entered in the text field?

Atlan 09-21-2012 08:36 PM

Thats how I want it to work, yes.

Ideally, the contents of the JTextField will always, instantly, update the String I store it in.

Fou-Lu 09-21-2012 10:16 PM

Then the best approach to this is to share the model used for both of the text fields. This is a simple Document type, which can be prepopulated with data if desired. Since the Document will register the document listeners itself, you don't need to handle any events at all.
See this example (untested, but if necessary I can verify after):
PHP Code:

public static void main(String[] argv)
{
    
SwingUtilities.invokeLater(new Runnable()
    {
        public 
void run()
        {
            
JFrame jf = new JFrame();
            
Document d = new PlainDocument();
            
JTextField jf1 = new JTextField();
            
JTextField jf2 = new JTextField();

            
jf1.setDocument(d);
            
jf2.setDocument(d);
            
jf2.setEditable(false);
            
jf.add(jf1);
            
jf.add(jf2);
            
jf.setLayout(new GridLayout(21));
            
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
jf.pack();
            
jf.setVisible(true);
        }
    });


Something like that to see what I mean. You should find everything you need in java.awt, javax.swing and javax.swing.text. If it works as I expect, you'll have two fields, the bottom one disabled and as you type in the text area it automatically fills in the second.

Atlan 09-22-2012 01:40 AM

Ok... I'm man enough to admit when I'm stumped.

I want to store the text (about 20 characters worth at maximum), in a string, from which I'll send it to various other classes. Why do I need a Document instead of a String? And why do I need 2 JTextFields?

Fou-Lu 09-22-2012 06:09 AM

You don't need two text fields; that's simply what you specified. Or did I simply misunderstand the purpose of the example you have?
Going back through this, I believe that I misinterpreted your requirements. Instead of two inputs, you have a class you are intending to link to a text field. This is fine, but since strings are immutable in java, you can only trigger the update with a required string type using a listener of some sorts. I still like working with models directly, so I'd go with the DocumentListener. Sadly I don't believe that it has a corresponding abstract.
PHP Code:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;

public class 
SharedDocumentTest implements DocumentListener
{
    private 
String str;
    
    public 
String getString()
    {
        return 
this.str;
    }
    
    @
Override
    
public void changedUpdate(DocumentEvent e)
    {
        
// not needed
    
}

    @
Override
    
public void insertUpdate(DocumentEvent e)
    {        
        try
        {
            
Document d e.getDocument();
            
this.str d.getText(0d.getLength());
        }
        catch (
BadLocationException ex)
        {
            
ex.printStackTrace();
        }
    }

    @
Override
    
public void removeUpdate(DocumentEvent e)
    {
        try
        {
            
Document d e.getDocument();
            
this.str d.getText(0d.getLength());
        }
        catch (
BadLocationException ex)
        {
            
ex.printStackTrace();
        }
    }
    
    public static 
void main(String[] argv)
    {
        
SwingUtilities.invokeLater(new Runnable()
        {
            public 
void run()
            {
                
JFrame jf = new JFrame();
                final 
SharedDocumentTest t = new SharedDocumentTest();

                
JTextField jf1 = new JTextField();
                
JButton jb = new JButton("Shows it.");
                
jb.addActionListener(new ActionListener()
                {
                    
                    @
Override
                    
public void actionPerformed(ActionEvent e)
                    {
                        
JOptionPane.showMessageDialog(nullt.getString());
                    }
                });
                
                
jf1.getDocument().addDocumentListener(t);
                
jf.add(jf1);
                
jf.add(jb);
                
jf.setLayout(new GridLayout(21));
                
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
jf.pack();
                
jf.setVisible(true);
            }
        });
    }


As you type or remove it will automatically update the underlying object model.


All times are GMT +1. The time now is 05:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.