java swing jtextfield modifiying input

B

brf

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?
 
B

brf

brf said:
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?
 
K

Knute Johnson

brf said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top