|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
CelsiusConverter
Topics illustrated in this example: Our next example,CelsiusConverter, does something that's somewhat useful: It is a simple conversion tool. The user enters a temperature in degrees Celsius and clicks the
Convert...button, and a label displays the equivalent in degrees Fahrenheit.Let's examine the code to see how CelsiusConverterparses the number entered in theJTextField. First, here's the code that sets up theJTextField:The integer argument passed in theJTextField tempCelsius = null; ... tempCelsius = new JTextField(5);JTextFieldconstructor,5in the example, indicates the number of columns in the field. This number is used along with metrics provided by the current font to calculate the field's preferred width. This number does not limit how many character the user can enter.We want to handle the button-click event, so we add an event listener to the button.
TheJButton convertTemp; ... convertTemp.addActionListener(this); ... public void actionPerformed(ActionEvent event) { // Parse degrees Celsius as a double and convert to Fahrenheit. int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); }getTextmethod is called on the text field,tempCelsius, to retrieve the data within it. Next, theparseDoublemethod parses the text as a double before converting the temperature and casting the result to an integer. Finally, the setText method is called on thefahrenheitLabelto display the converted temperature. All this code is found in the event handler for the button, as the conversion happens only once the button is clicked.
Note: You can make aJButtonbe the default button. At most one button in a top-level container can be the default button. The default button typically has a highlighted appearance and acts clicked whenever the top-level container has the keyboard focus and the user presses theReturnorEnterkey. The exact implementation depends on the look and feel. You set the default button by invoking thesetDefaultButtonmethod on a top-level container's root pane://In the constructor for a JDialog subclass: getRootPane().setDefaultButton(setButton);
You can use HTML to specify the text on some Swing components, such as buttons and labels. We can spice up theCelsiusConverterprogram by adding HTML text to thefahrenheitLabeland adding an image to theconvertTempbutton. The revised program isCelsiusConverter2.
First, let's look at how we specify the HTML tags for the fahrenheitLabel. As you can see from this code, the temperature (tempFahr) is displayed one of three different colors, depending on how hot or cold the converted temperature is:To add HTML code to the label, simply put the// Set fahrenheitLabel to new value and font color based on temperature. if (tempFahr <= 32) { fahrenheitLabel.setText("<html><font color=blue>" + tempFahr + "° Fahrenheit </font></html>"); } else if (tempFahr <= 80) { fahrenheitLabel.setText("<html><font color=green>" + tempFahr + "° Fahrenheit </font></html>"); } else { fahrenheitLabel.setText("<html><font color=red>" + tempFahr + "° Fahrenheit </font></html>"); }<HTML>tag at the beginning of a string, and then use any valid HTML code in the remainder of the string. Using HTML can be useful for varying the text font or color within a button and for adding line breaks. To display the degree symbol, we use the HTML code°.
Note: If the string is to be all one size and color, you don't have to use HTML. You can call thesetFontmethod to specify the font of any component.
Warning: Don't use HTML in buttons unless you're absolutely sure that the program is running in a release that supports this feature. In releases that don't support HTML text, such as Swing 1.1, putting HTML in a button results in one ugly-looking button whose label starts (not surprisingly) with<HTML>.
Some Swing components can be decorated with an icon--a fixed-size image. A Swing icon is an object that adheres to theIconinterface. Swing provides a particularly useful implementation of theIconinterface:ImageIcon.ImageIconpaints an icon from a GIF or a JPEG image. Here's the code that adds the arrow graphic to theconvertTempbutton:The first argument of theImageIcon icon = new ImageIcon("images/convert.gif", "Convert temperature"); ... convertTemp = new JButton(icon);ImageIconconstructor specifies the file to load, relative to the directory containing the application's class file. The second argument provides a description of the icon that assistive technologies can use.
 
|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.