Forums Register Login

inputverifier - work with JOptionPane?

+Pie Number of slices to send: Send
I am trying to get the inputverifier class to work with JOptionPane class. In the small pgm below,when I comment out the lines of JOptionPane, all works well. But when not, it goes whacky. Any suggestions would be appreciated.
Thank you.

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
class tstvrfy extends JFrame
{
JTextField tf1,tf2;
tstvrfy()
{
tf1 = new JTextField("Type \"pass\" here.");
getContentPane().add(tf1,BorderLayout.NORTH);
tf1.setInputVerifier(new passverifier());
tf2 = new JTextField("textfld2");
getContentPane().add(tf2,BorderLayout.SOUTH);
WindowListener l = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(l);
}
class passverifier extends InputVerifier
{
public boolean verify(JComponent input)
{
JTextField tf = (JTextField) input;
System.out.println("tf="+tf.getText());
tf2.setText("typed "+tf.getText());

if ("pass".equals(tf.getText())) return true;
else
{
JOptionPane.showMessageDialog(tstvrfy.this,"Not a Valid entry",
"Invalid entry",JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
public static void main (String[] args)
{
tstvrfy f = new tstvrfy();
f.pack();
f.setVisible(true);
}
}
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1097 times.
Similar Threads
Twins views
Is there a standard way of creating a text field that requires the input to be an integer?
need help in applying inputverifer to jtextfield
IntelliJ IDEA and setInputVerifier
JTextfield
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:30:44.