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 / March 2004

Tip: Looking for answers? Try searching our database.

Can't get a dialog box to work

Thread view: 
Steven M. Britton - 14 Mar 2004 20:00 GMT
lblEmpty = new JLabel("Aircraft Empty Weight",JLabel.LEFT);
        panel.add(lblEmpty);
           
    txtEmpty = new JTextField("0",5);
        txtEmpty.setSize(50,30); // Width, Heigth
        panel.add(txtEmpty);

        dblEmpty = Double.parseDouble(txtEmpty.getText());

            if (dblEmpty = null)
            JOptionPane.showMessageDialog(frame, "Empty Required");

I get the following error, what can I do to correct it?

incompatible types
found : double
required : boolean
Lee Weiner - 14 Mar 2004 20:29 GMT
>lblEmpty = new JLabel("Aircraft Empty Weight",JLabel.LEFT);
>         panel.add(lblEmpty);
[quoted text clipped - 13 lines]
>found : double
>required : boolean

You've got two errors in the "if" statement.  First, the equality operator is
a double equal sign (==).  You've got an assignment operator (=).  Second, a
primitive variable cannot be null.  What you have to do is test the length of
the String in the textfield before you parseDouble() it into the double
variable.
       if( txtEmpty.getText().length() == 0 )
           JOptionPane.showMessageDialog( blah, blah);
       else
           dblEmpty = Double.parseDouble( txtEmpty.getText() );

Lee Weiner
lee AT leeweiner DOT org
Jon A. Cruz - 14 Mar 2004 20:45 GMT
>         if( txtEmpty.getText().length() == 0 )
>             JOptionPane.showMessageDialog( blah, blah);
>         else
>             dblEmpty = Double.parseDouble( txtEmpty.getText() );

To clean it up to be generally useful you can cache and check the text
differently.

String txt = txtEmpty.getText();

if ( txt == null || "".equals(txt) )
{
    JOptionPane.showMessageDialog( blah, blah);
}
else
{
    dblEmpty = Double.parseDouble( txt );
}

Although in this case you can count on getText() not being null, doing
the "if" like that makes it unified and easier when doing a cut-n-paste
to copy elsewhere.
Jon A. Cruz - 14 Mar 2004 20:47 GMT
>          txtEmpty.setSize(50,30); // Width, Heigth

Usually you don't want to do that.

Instead of trying to place each component manually, use a Layout Manager
to control the layout.


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.