Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2012, 02:25 AM   PM User | #1
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
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.
Atlan is offline   Reply With Quote
Old 09-19-2012, 05:58 AM   PM User | #2
RichardD
New to the CF scene

 
Join Date: Sep 2012
Location: Sweden
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
RichardD is an unknown quantity at this point
I think a KeyListener would do the job (http://docs.oracle.com/javase/1.4.2/...yListener.html), instead of the actionlistener.
RichardD is offline   Reply With Quote
Old 09-21-2012, 03:19 AM   PM User | #3
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
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 is offline   Reply With Quote
Old 09-21-2012, 04:26 AM   PM User | #4
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
It really is. Anyone know whats going wrong? And how to fix it?
Atlan is offline   Reply With Quote
Old 09-21-2012, 06:14 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is the text in the class always supposed to match the text entered in the text field?
Fou-Lu is offline   Reply With Quote
Old 09-21-2012, 08:36 PM   PM User | #6
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
Thats how I want it to work, yes.

Ideally, the contents of the JTextField will always, instantly, update the String I store it in.
Atlan is offline   Reply With Quote
Old 09-21-2012, 10:16 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 09-22-2012, 01:40 AM   PM User | #8
Atlan
New Coder

 
Join Date: Jul 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Atlan is an unknown quantity at this point
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?
Atlan is offline   Reply With Quote
Old 09-22-2012, 06:09 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
java, jpanel, jtextfield, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:30 PM.


Advertisement
Log in to turn off these ads.