Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java swing jtextfield modifiying input

Reply
Thread Tools

java swing jtextfield modifiying input

 
 
brf
Guest
Posts: n/a
 
      12-04-2006
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 [i]);
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?

 
Reply With Quote
 
 
 
 
brf
Guest
Posts: n/a
 
      12-04-2006

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 [i]);
> 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?


 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      12-04-2006
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 [i]);
> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Customising Swing JTextField David Java 3 11-12-2006 03:48 AM
Modifiying array elements with references Anonymous user Perl Misc 3 01-25-2006 02:06 PM
Can I use a swing Component (e.g. JTextField) in a Java Applet Prashanth Ravi Java 2 10-07-2004 09:58 AM
Can I use a swing Component (e.g. JTextField) in a Java Applet Prashanth Ravi Java 1 10-07-2004 09:30 AM
EXE tools for viewing/modifiying Acess2000 data on remote server Mike Collier ASP General 2 01-20-2004 03:23 PM



Advertisments