Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


where x and y specify the drawing origin; the drawing itself happens in Graphics g. You can use the Component to get properties such as the foreground or background color—in practice, it’s usually ignored.


NOTE:  In theory, any JComponent can contain either AWT or Swing components. Unfortunately, the words “in theory” often translate into “not really.” Many programmers report that they have had difficulty mixing classic AWT components with Swing components. If you want to try, go ahead—you’ve been warned.

Swing provides one Icon class for you—ImageIcon, used for displaying Images. Listing 38.8 is an example of an Icon.

Listing 38.8 TBigBlackDot.javaYou Can Add This Icon to Swing Buttons and Labels


public class TBigBlackDot implements javax.swing.Icon {
  public void paintIcon(java.awt.Component c, 
                        java.awt.Graphics g, int x, int y) {
    g.setColor(java.awt.Color.black);
    g.fillOval(x, y, getIconWidth(), getIconHeight());
  }
  public int getIconWidth() {
    return 100;
  }
  public int getIconHeight() {
    return 100;
  }
}

Adding an Instance of JLabel

A label enables you to place static text onto the screen. We took our first look at Label when we discussed the AWT. The Swing JLabel improves upon java.awt.Label by enabling you to add an Icon and giving you better control over the position of the text. Listing 38.9 shows a JLabel in action on a JPanel.

Listing 38.9 TLabelPanel.javaThis Panel Contains a Label with a TBigBlackDot Icon


public class TLabelPanel extends javax.swing.JPanel {
  public TLabelPanel() {
    javax.swing.JLabel theLabel = 
      new com.sun.java.swing.JLabel(“Example of TBigBlackDot”);

    // we don’t have to settle for plain vanilla text
    java.awt.Font theBigBoldFont = 
      new java.awt.Font(“Serif”, 
                        java.awt.Font.BOLD, 
                        32);
    theLabel.setFont(theBigBoldFont);

    // now add an icon
    javax.swing.Icon aDot = new TBigBlackDot();
    theLabel.setIcon(aDot);
    theLabel.setPreferredSize(new 
      java.awt.Dimension(600, 150));

    // place the text to the right of the icon
    theLabel.setHorizontalAlignment(javax.swing.JLabel.RIGHT);

    // and add the whole thing to the panel
    add(theLabel);
  }
}


FIGURE 38.19  You can add icons to a JLabel and control the text’s position and appearance.

Using JButton

JButton behaves much like Button; you add it to a JPanel and listen for its Action with an ActionListener.


By default, a new JButton has the same background color as the container. Some developers don’t like this design and prefer that it stand out more. Consider adding a line such as
theButton.setBackground(SystemColor.control );
to make the button highly visible.

As with JLabel, you can add an Icon to JButton by calling the setIcon() method.


Several Swing components, including JButton, are derived from AbstractButton. Review the documentation for AbstractButton—you’ll find methods that enable you to enable and disable the button, methods that enable you to control the internal alignment, and ways to associate an accelerator key with the button.

Adding an Instance of JCheckBox

Recall from the earlier section of this chapter, “Using the Abstract Windowing Toolkit,” that you can implement check boxes with java.awt.Checkbox. You implemented radio buttons by placing the Checkbox into a CheckboxGroup. In Swing, the concept of a radio button in handled explicitly—Swing has its own JRadioButton class with an associated ButtonGroup. You use JCheckBox just to implement check boxes.


NOTE:  JCheckBox has its own icons to signify the selected and unselected states. If you prefer, you can make your own Icons and use them in setIcon() and setSelectedIcon().

Using JRadioButton

To make a group of radio buttons, make instances of JRadioButton and add them to a ButtonGroup. Listing 38.10 shows an example of some radio buttons.

Listing 38.10 TDoseNotGivenPanel.javaThis Panel Contains a Group of Radio Buttons


public class TDoseNotGivenPanel extends javax.swing.JPanel {
  public TDoseNotGivenPanel() {

    // make room for a label and four buttons
    setLayout(new java.awt.GridLayout(5, 1));

    javax.swing.ButtonGroup aReason = 
      new javax.swing.ButtonGroup();
    javax.swing.JLabel theLabel = 
      new javax.swing.JLabel(“Dose not given because”);
    theLabel.setFont(new java.awt.Font(“SansSrif”, 
                                        java.awt.Font.BOLD, 14));
    add(theLabel);

    javax.swing.JRadioButton thePatientNotAvailableButton =
      new javax.swing.JRadioButton(“Patient not available”);
    thePatientNotAvailableButton.setHorizontalAlignment(
      javax.swing.AbstractButton.LEFT);
    thePatientNotAvailableButton.setKeyAccelerator(‘A’);
    thePatientNotAvailableButton.setSelected(true); //default
    add(thePatientNotAvailableButton);
    aReason.add(thePatientNotAvailableButton);

    javax.swing.JRadioButton thePatientOffWardButton =
      new javax.swing.JRadioButton(“Patient off ward”);
    thePatientOffWardButton.setHorizontalAlignment(
      javax.swing.AbstractButton.LEFT);
    thePatientNotAvailableButton.setKeyAccelerator(‘O’);
    add (thePatientOffWardButton);
    aReason.add(thePatientOffWardButton);

    javax.swing.JRadioButton thePatientRefusedButton =
      new javax.swing.JRadioButton(“Patient refused dose”);
    thePatientRefusedButton.setHorizontalAlignment(
      javax.swing.AbstractButton.LEFT);
    thePatientNotAvailableButton.setKeyAccelerator(‘R’);
    add (thePatientRefusedButton);
    aReason.add(thePatientRefusedButton);

    javax.swing.JRadioButton thePatientExpelledDoseButton =
      new javax.swing.JRadioButton(“Patient expelled dose”);
    thePatientExpelledDoseButton.setHorizontalAlignment(
      javax.swing.AbstractButton.LEFT);
    thePatientNotAvailableButton.setKeyAccelerator(‘X’);
    add (thePatientExpelledDoseButton);
    aReason.add(thePatientExpelledDoseButton);
  }
}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.