C++ Ray Trait Architecture
C++ Ray Trait Architecture
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
Dita 2
Ju lutem të vlerësoni deri në çfarë shkalle ditari reflekton aktivitetet dhe shkathtësitë e fituara nga
studenti:
Nënshkrimi ______________________________
Pozita_____________________
Data______________________
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Begin Footnote: Perhaps you noted that, although JOptionPane is a class,
an showInputDialog message was sent to it---why did we not create an object from class
JOptionPane? The answer is elaborated in the next Chapter, where we learn
that showInputDialog is a static method, just like main is a static method, and there is no
need to create an object before sending a message to a static method. Again, Chapter 5 will
present the details. End Footnote
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.
where PROMPT is a string that you want displayed within the dialog. (For the above
example, the prompt would be "Type an integer Celsius temperature:".) The
message, showInputDialog(PROMPT), creates the dialog that accepts the user's input.
The text string that the user types into the dialog's field is returned as the result when
the user presses the dialog's OK button. In the above statement, the result is assigned
to variable input. (Like program arguments, inputs from dialogs are strings.)
Figure 2 shows how the temperature-conversion application is modified to use the dialog as
its input view.
FIGURE 2: Interactive input from a dialog===============================
import java.text.*;
import javax.swing.*;
/** CelsiusToFahrenheit2 converts an input Celsius value to Fahrenheit.
* input: the degrees Celsius, an integer read from a dialog
* output: the degrees Fahrenheit, a double */
public class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ String input =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(input).intValue(); // convert input into an int
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
System.out.println("For Celsius degrees " + c + ",");
System.out.println("Degrees Fahrenheit = " + formatter.format(f));
}
}
ENDFIGURE================================================================
As noted above, the input received by the dialog is a string, so we must convert the string
into an integer with a new Integer ``helper object'':
int c = new Integer(input).intValue();
The application's outputs are computed and are displayed in the command window, but later
in this chapter we learn how to display outputs in graphics windows of our own making.
Of course, an application can ask its user for as many inputs as it desires. For example, the
change-making application in Figure 3, Chapter 3, might be converted into an application
that asks its user for the dollars input and then the cents input---the first two statements in
Figure 3's main method are replaced by these, which construct two dialogs:
String d =
JOptionPane.showInputDialog("Type an integer dollars amount:");
int dollars = new Integer(d).intValue();
String c =
JOptionPane.showInputDialog("Type an integer cents amount:");
int cents = new Integer(c).intValue();
One obvious question remains: What happens when the user presses the
dialog's Cancel button instead of its OK button? If you try this, you will see that the program
prematurely halts due to an exception:
Exception in thread "main" java.lang.NumberFormatException: null
at CelsiusToFahrenheit2.main(CelsiusToFahrenheit2.java:11)
JOptionFrame's input dialog is written so that a press of its Cancel button causes it to return a
``no value'' string, called null. (This is different from the empty string, ""---null means ``no
string at all.'')
Although null is assigned to variable input, it is impossible for the new Integer helper
object to convert it into an integer, and the program halts at this point. Exceptions are studied
in more detail later.
Another odd behavior is seen when the application is used correctly and displays the
converted temperature: The application does not appear to terminate, even though all the
statements in its main method have completed. Alas, JOptionPane is the culprit---it is still
executing, unseen.
Begin Footnote: JOptionPane and the other graphics classes in this chapter generate
separate threads of execution, that is, they are separately executing objects. So, even though
the main method's ``thread'' terminates, the ``thread'' associated with JOptionPane is still
executing (perhaps ``idling'' is a better description), hence, the application never terminates.
Multiple threads of execution are valuable for programming animations and applications
where multiple computations must proceed in parallel. End Footnote
To terminate the application, use the Stop or Stop Program button on your IDE; if you use
the JDK, press the control and c keys simultaneously in the command window.