JTextField setText() does nothing

J

jonasmin

I have problem with setting the JTextField text. here you see some of
my code:

if (in1.getText().length() > 10){
JOptionPane.showMessageDialog(null, "Error", "Error",
JOptionPane.WARNING_MESSAGE);
in1.setText("");
}


in1 - JTextField. But when i axecute the program it does nothing. It
shows the WARNING_MESSAGE, but it does nothing with text. where can be
the problem?
 
K

Knute Johnson

I have problem with setting the JTextField text. here you see some of
my code:

if (in1.getText().length() > 10){
JOptionPane.showMessageDialog(null, "Error", "Error",
JOptionPane.WARNING_MESSAGE);
in1.setText("");
}


in1 - JTextField. But when i axecute the program it does nothing. It
shows the WARNING_MESSAGE, but it does nothing with text. where can be
the problem?

JOptionPane.showMessageDialog is modal so the in1.setText() won't get
executed until the dialog is closed.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField tf = new JTextField("Hello World");
f.add(tf,BorderLayout.CENTER);
JButton b = new JButton("Erase The Text");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(
null,"Error","Error",
JOptionPane.WARNING_MESSAGE);
tf.setText("");
}
});
f.add(b,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
});
}
}
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top