Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    New to the CF scene
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think a KeyListener would do the job (http://docs.oracle.com/javase/1.4.2/...yListener.html), instead of the actionlistener.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It really is. Anyone know whats going wrong? And how to fix it?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is the text in the class always supposed to match the text entered in the text field?

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thats how I want it to work, yes.

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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #8
    New Coder
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •