Re: Can JOptionPane use a MaskFormatter for data input



dcMan schrieb:
Michael Rauscher wrote:
You may use a JFormattedTextField (using a MaskFormatter) and you may
further use this text field as message parameter to call any of the
JOptionPane.showXXXDialog methods.

From what I understand about JOptionPane, the message parameter is only
for the labe text you want displayed to the operator, such as: "Please
enter the IP Address:".

I don't think the message parameter is for the data entry TextField.

Please read the API:

message
A descriptive message to be placed in the dialog box. In the most
common usage, message is just a String or String constant. However,
the type of this parameter is actually Object. Its interpretation
depends on its type:
...
Component
The Component is displayed in the dialog.

Here's a more "complicated" example where I use a panel as the message instead of the textfield - this is because I want to show some label in front of the input field.

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

public class Test {

public static final void main( String args[] ) throws Exception {
JPanel panel = new JPanel( new BorderLayout(5,0) );
panel.add( new JLabel("Input: "), BorderLayout.WEST );

MaskFormatter formatter = new MaskFormatter("###.###.###.###");
JFormattedTextField text = new JFormattedTextField(formatter);
panel.add( text );

JOptionPane.showMessageDialog( null, panel );
JOptionPane.showMessageDialog( null,
"You've entered \"" + text.getText() + "\"");
}
}

Again, instead of (ab)using JOptionPane you could create the same by using a JDialog.

Bye
Michael
.