Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2006

Tip: Looking for answers? Try searching our database.

Can JOptionPane use a MaskFormatter for data input

Thread view: 
dcMan - 27 Sep 2006 14:33 GMT
I would like to have a dialog box that prompts the user for an IP
address. Is there a way to use JOptionPane, but have the data entry
text box restrict the input to be ###.###.###.###?

Thanks
Greg
Deniz Dogan - 27 Sep 2006 16:48 GMT
> I would like to have a dialog box that prompts the user for an IP
> address. Is there a way to use JOptionPane, but have the data entry
> text box restrict the input to be ###.###.###.###?
>
> Thanks
> Greg

I haven't got an answer, in fact I'm bumping this one, because I need to
know the same thing. There SHOULD be such a thing I reckon, this is Java
after all. ;-)
Michael Rauscher - 27 Sep 2006 17:21 GMT
Deniz Dogan schrieb:
>> I would like to have a dialog box that prompts the user for an IP
>> address. Is there a way to use JOptionPane, but have the data entry
[quoted text clipped - 6 lines]
> know the same thing. There SHOULD be such a thing I reckon, this is Java
> after all. ;-)

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.

Instead of using JOptionPane you can of course use a JDialog, too.

Bye
Michael
dcMan - 27 Sep 2006 18:51 GMT
> 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.
The data entry text field is where I want the formatting to take place.
I tried a quick and dirty version from what I understood you were
suggesting. But the text that was affected by the message parameter was
the Label text for the Dialog box.
try
       {
           JFormattedTextField ft = new JFormattedTextField();
           MaskFormatter formatter = new
MaskFormatter("###.###.###.###");
           formatter.install(ft);
           JOptionPane.showInputDialog(null ,formatter);
       } catch (Exception e)
       {

       }

If I'm wrong in how you ment to use the message parameter could you
please clarify. Thanks
Michael Rauscher - 27 Sep 2006 23:02 GMT
dcMan schrieb:
>> You may use a JFormattedTextField (using a MaskFormatter) and you may
>> further use this text field as message parameter to call any of the
[quoted text clipped - 5 lines]
>
> 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
dcMan - 28 Sep 2006 14:25 GMT
Thanks Michael,
I did try to go through the API. But I guess just starting out in Java
and Swing, I didn't fully understand what the documentation was
informing me of what I could do.

Thanks for your example also.
Greg
Michael Rauscher - 28 Sep 2006 15:49 GMT
dcMan schrieb:
> Thanks Michael,
> I did try to go through the API. But I guess just starting out in Java
> and Swing, I didn't fully understand what the documentation was
> informing me of what I could do.

In fact, this part (message parameter) of the API is a bit confusing.

One could interprete "The Component is displayed in the dialog" so that
the Component is just rendered. In that case the example wouldn't work.

That's why I've written "(ab)using".

Luckily the component is not just rendered. It is added to the message
area of the dialog.

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.