java swing jtextfield modifiying input

Discussion in 'Java' started by brf, Dec 4, 2006.

  1. brf

    brf Guest

    To make simple autocorrections in an JTextField, receiving textinput, I
    tried to change the text after caretlistener events. But I only get
    exceptions

    pf, touched: - 0 - 0
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
    Attempt
    to mutate in notification
    ....

    main creates a frame with
    JFrame f = new JFrame ("Test");
    Container inhalt = f.getContentPane();

    PhraseField pf = new PhraseField (30);
    inhalt.add (pf );
    pf.addActionListener (new TxtFldAct ());
    pf.addCaretListener (new TxtFldCar ());
    f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    f.setSize(700,500);
    f.setVisible(true);

    and the Listeners try to modify the textField

    public class TxtFldCar implements CaretListener
    {
    public void caretUpdate (CaretEvent e)
    {
    PhraseField tf = (PhraseField) e.getSource();
    tf.touched ();
    }
    }

    via the used subclass of JTextField

    public class PhraseField extends JTextField
    {
    public PhraseField (int columns) { this ("", columns); }
    public PhraseField (String text, int columns)
    {
    super (text, columns);
    this.constant = true;
    this.value = "";
    }
    public void touched ()
    {
    this.setText ("some corrected String"); }
    }
    }

    Is this the right solution and if so what is my error?

    If this is not a good solution, what is a besser one?
     
    brf, Dec 4, 2006
    #1
    1. Advertising

  2. brf

    brf Guest

    brf schrieb:

    > To make simple autocorrections in an JTextField, receiving textinput, I
    > tried to change the text after caretlistener events. But I only get
    > exceptions
    >
    > pf, touched: - 0 - 0
    > Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
    > Attempt
    > to mutate in notification
    > ...
    >
    > main creates a frame with
    > JFrame f = new JFrame ("Test");
    > Container inhalt = f.getContentPane();
    >
    > PhraseField pf = new PhraseField (30);
    > inhalt.add (pf );
    > pf.addActionListener (new TxtFldAct ());
    > pf.addCaretListener (new TxtFldCar ());
    > f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    > f.setSize(700,500);
    > f.setVisible(true);
    >
    > and the Listeners try to modify the textField
    >
    > public class TxtFldCar implements CaretListener
    > {
    > public void caretUpdate (CaretEvent e)
    > {
    > PhraseField tf = (PhraseField) e.getSource();
    > tf.touched ();
    > }
    > }
    >
    > via the used subclass of JTextField
    >
    > public class PhraseField extends JTextField
    > {
    > public PhraseField (int columns) { this ("", columns); }
    > public PhraseField (String text, int columns)
    > {
    > super (text, columns);
    > this.constant = true;
    > this.value = "";
    > }
    > public void touched ()
    > {
    > this.setText ("some corrected String"); }
    > }
    > }
    >
    > Is this the right solution and if so what is my error?
    >
    > If this is not a good solution, what is a better one?
     
    brf, Dec 4, 2006
    #2
    1. Advertising

  3. brf wrote:
    > To make simple autocorrections in an JTextField, receiving textinput, I
    > tried to change the text after caretlistener events. But I only get
    > exceptions
    >
    > pf, touched: - 0 - 0
    > Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
    > Attempt
    > to mutate in notification
    > ...
    >
    > main creates a frame with
    > JFrame f = new JFrame ("Test");
    > Container inhalt = f.getContentPane();
    >
    > PhraseField pf = new PhraseField (30);
    > inhalt.add (pf );
    > pf.addActionListener (new TxtFldAct ());
    > pf.addCaretListener (new TxtFldCar ());
    > f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
    > f.setSize(700,500);
    > f.setVisible(true);
    >
    > and the Listeners try to modify the textField
    >
    > public class TxtFldCar implements CaretListener
    > {
    > public void caretUpdate (CaretEvent e)
    > {
    > PhraseField tf = (PhraseField) e.getSource();
    > tf.touched ();
    > }
    > }
    >
    > via the used subclass of JTextField
    >
    > public class PhraseField extends JTextField
    > {
    > public PhraseField (int columns) { this ("", columns); }
    > public PhraseField (String text, int columns)
    > {
    > super (text, columns);
    > this.constant = true;
    > this.value = "";
    > }
    > public void touched ()
    > {
    > this.setText ("some corrected String"); }
    > }
    > }
    >
    > Is this the right solution and if so what is my error?


    Probably not.

    > If this is not a good solution, what is a besser one?
    >


    Yes. Look at the interface javax.swing.text.Document. You can do some
    amazing things with the Document.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Dec 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prashanth Ravi
    Replies:
    2
    Views:
    533
    Andrew Thompson
    Oct 7, 2004
  2. Prashanth Ravi
    Replies:
    1
    Views:
    432
    Andrew Thompson
    Oct 7, 2004
  3. David
    Replies:
    3
    Views:
    529
    Kenneth P. Turvey
    Nov 12, 2006
  4. Mike Collier
    Replies:
    2
    Views:
    136
    Mike Collier
    Jan 20, 2004
  5. Anonymous user

    Modifiying array elements with references

    Anonymous user, Jan 25, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    111
    Anno Siegel
    Jan 25, 2006
Loading...

Share This Page