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.