home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / RadioButtonPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  9.8 KB  |  303 lines

  1. /*
  2.  * @(#)RadioButtonPanel.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.text.*;
  17. import javax.swing.border.*;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.util.*;
  22.  
  23.  
  24. /**
  25.  * RadioButtons!
  26.  *
  27.  * @version 1.9 08/26/98
  28.  * @author Jeff Dinkins
  29.  */
  30. public class RadioButtonPanel extends JPanel 
  31. {
  32.     // The Frame
  33.     SwingSet swing;
  34.  
  35.     ImageIcon radio = SwingSet.sharedInstance().loadImageIcon("images/WebSpice/radio.gif","Grey circle with blue triangle inside");
  36.     ImageIcon radioSelected = SwingSet.sharedInstance().loadImageIcon("images/WebSpice/radioSelected.gif","Grey circle with green triangle inside");
  37.     ImageIcon radioPressed = SwingSet.sharedInstance().loadImageIcon("images/WebSpice/radioPressed.gif","Grey circle with purple triangle inside");
  38.  
  39.     public RadioButtonPanel(SwingSet swing) {
  40.     this.swing = swing;
  41.  
  42.     ButtonGroup group;
  43.  
  44.     setBorder(swing.emptyBorder5);
  45.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  46.  
  47.     // *************** radio buttons ****************
  48.     // text buttons
  49.     JPanel textButtons = SwingSet.createHorizontalPanel(false);
  50.     textButtons.setAlignmentX(LEFT_ALIGNMENT);
  51.     Border buttonBorder = new TitledBorder(null, "Text RadioButtons", 
  52.                            TitledBorder.LEFT, TitledBorder.TOP,
  53.                            swing.boldFont);
  54.  
  55.     Border emptyBorder = new EmptyBorder(5,5,5,5);
  56.     Border compoundBorder = new CompoundBorder( buttonBorder, emptyBorder);
  57.     textButtons.setBorder(compoundBorder);
  58.  
  59.     group = new ButtonGroup();
  60.     JRadioButton button;
  61.     button = new JRadioButton("One", true);
  62.     button.setToolTipText("This is a RadioButton with Text");
  63.     group.add(button);
  64.         button.setMnemonic('o');
  65.     swing.radioButtons.addElement(button);
  66.     textButtons.add(button);
  67.     textButtons.add(Box.createRigidArea(swing.hpad10));
  68.     
  69.     button = new JRadioButton("Two");
  70.     group.add(button);
  71.         button.setMnemonic('t');
  72.     button.setToolTipText("This is a RadioButton with Text");
  73.     swing.radioButtons.addElement(button);
  74.     textButtons.add(button);
  75.     textButtons.add(Box.createRigidArea(swing.hpad10));
  76.  
  77.     button = new JRadioButton("Three");
  78.     group.add(button);
  79.         button.setMnemonic('h');
  80.     button.setToolTipText("This is a RadioButton with Text");
  81.     swing.radioButtons.addElement(button);
  82.     textButtons.add(button);
  83.  
  84.  
  85.     // image buttons
  86.     group = new ButtonGroup();
  87.  
  88.     JPanel imageButtons = SwingSet.createHorizontalPanel(false);
  89.     imageButtons.setAlignmentX(LEFT_ALIGNMENT);
  90.     buttonBorder = new TitledBorder(null, "Image RadioButtons", 
  91.                            TitledBorder.LEFT, TitledBorder.TOP,
  92.                            swing.boldFont);
  93.     compoundBorder = new CompoundBorder(buttonBorder, emptyBorder);
  94.     imageButtons.setBorder(compoundBorder);
  95.  
  96.     // 1 image
  97.     button = new JRadioButton(swing.duke2);
  98.     group.add(button);
  99.     button.setSelectedIcon(swing.dukeWave);
  100.     button.setPressedIcon(swing.dukeWaveRed);
  101.     button.setRolloverIcon(swing.dukeWaveRed);
  102.     button.setSelected(true);
  103.     button.setToolTipText("This is a RadioButton with a Icon");
  104.     button.getAccessibleContext().setAccessibleName("Duke as a radio button");
  105.     swing.radioButtons.addElement(button);
  106.     imageButtons.add(button);
  107.     imageButtons.add(Box.createRigidArea(swing.hpad10));
  108.     
  109.     // 2 images
  110.     button = new JRadioButton(swing.duke2);
  111.     group.add(button);
  112.     swing.radioButtons.addElement(button);
  113.     button.setSelectedIcon(swing.dukeWave);
  114.     button.setPressedIcon(swing.dukeWaveRed);
  115.     button.setRolloverIcon(swing.dukeWaveRed);
  116.     button.setToolTipText("This is a RadioButton with a Icon");
  117.     button.getAccessibleContext().setAccessibleName("Duke as a radio button");
  118.     imageButtons.add(button);
  119.     imageButtons.add(Box.createRigidArea(swing.hpad10));
  120.  
  121.     // 3 images
  122.     button = new JRadioButton(swing.duke2);
  123.     group.add(button);
  124.     button.setSelectedIcon(swing.dukeWave);
  125.     button.setPressedIcon(swing.dukeWaveRed);
  126.     button.setRolloverIcon(swing.dukeWaveRed);
  127.     button.setToolTipText("This is a RadioButton with a Icon");
  128.     button.getAccessibleContext().setAccessibleName("Duke as a radio button");
  129.     swing.radioButtons.addElement(button);
  130.     imageButtons.add(button);
  131.  
  132.     // text&image buttons
  133.     group = new ButtonGroup();
  134.  
  135.     JPanel tiButtons = SwingSet.createHorizontalPanel(false);
  136.     tiButtons.setAlignmentX(LEFT_ALIGNMENT);
  137.     buttonBorder = new TitledBorder(null, "Image & Text RadioButtons", 
  138.                            TitledBorder.LEFT, TitledBorder.TOP,
  139.                            swing.boldFont);
  140.     compoundBorder = new CompoundBorder(buttonBorder, emptyBorder);
  141.     tiButtons.setBorder(compoundBorder);
  142.  
  143.     button = new JRadioButton("Left", radio);
  144.     group.add(button);
  145.     button.setToolTipText("This is a RadioButton with a Icon and Text");
  146.     button.setSelected(true);
  147.     button.setSelectedIcon(radioSelected);
  148.     button.setPressedIcon(radioPressed);
  149.     swing.radioButtons.addElement(button);
  150.     tiButtons.add(button);
  151.     tiButtons.add(Box.createRigidArea(swing.hpad10));
  152.  
  153.     button = new JRadioButton("Center", radio);
  154.     group.add(button);
  155.     swing.radioButtons.addElement(button);
  156.     button.setToolTipText("This is a RadioButton with a Icon and Text");
  157.     button.setSelectedIcon(radioSelected);
  158.     button.setPressedIcon(radioPressed);
  159.     tiButtons.add(button);
  160.     tiButtons.add(Box.createRigidArea(swing.hpad10));
  161.  
  162.     button = new JRadioButton("Right", radio);
  163.     group.add(button);
  164.     swing.radioButtons.addElement(button);
  165.     button.setToolTipText("This is a RadioButton with a Icon and Text");
  166.     button.setSelectedIcon(radioSelected);
  167.     button.setPressedIcon(radioPressed);
  168.     tiButtons.add(button);
  169.     tiButtons.add(Box.createHorizontalBox());
  170.  
  171.     // Add button panels to buttonPanel
  172.     JPanel buttonPanel = SwingSet.createVerticalPanel(true);
  173.     buttonPanel.setAlignmentX(LEFT_ALIGNMENT);
  174.     buttonPanel.setAlignmentY(TOP_ALIGNMENT);
  175.  
  176.     buttonPanel.add(textButtons);
  177.  
  178.     buttonPanel.add(Box.createVerticalStrut(10));
  179.  
  180.  
  181.     buttonPanel.add(imageButtons);
  182.  
  183.     buttonPanel.add(Box.createVerticalStrut(10));
  184.  
  185.     buttonPanel.add(tiButtons);
  186.     buttonPanel.add(tiButtons);
  187.     buttonPanel.add(Box.createGlue());
  188.  
  189.  
  190.     // *************** Create the button controls ****************
  191.     JPanel controls = new JPanel() {
  192.         public Dimension getMaximumSize() {
  193.         return new Dimension(300, super.getMaximumSize().height);
  194.         }
  195.     };
  196.     controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
  197.     controls.setAlignmentY(TOP_ALIGNMENT);
  198.     controls.setAlignmentX(LEFT_ALIGNMENT);
  199.  
  200.     JPanel buttonControls = SwingSet.createHorizontalPanel(true);
  201.     buttonControls.setAlignmentY(TOP_ALIGNMENT);
  202.     buttonControls.setAlignmentX(LEFT_ALIGNMENT);
  203.  
  204.     JPanel leftColumn = SwingSet.createVerticalPanel(false);
  205.     leftColumn.setAlignmentX(LEFT_ALIGNMENT);
  206.     leftColumn.setAlignmentY(TOP_ALIGNMENT);
  207.  
  208.     JPanel rightColumn = SwingSet.createVerticalPanel(false);
  209.     rightColumn.setAlignmentX(LEFT_ALIGNMENT);
  210.     rightColumn.setAlignmentY(TOP_ALIGNMENT);
  211.  
  212.     buttonControls.add(leftColumn);
  213.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  214.     buttonControls.add(rightColumn);
  215.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  216.  
  217.     controls.add(buttonControls);
  218.  
  219.     // Display Options
  220.     JLabel l = new JLabel("Display Options:");
  221.     leftColumn.add(l);
  222.     l.setFont(swing.boldFont);
  223.  
  224.      JCheckBox bordered = new JCheckBox("Paint Border");
  225.     bordered.setToolTipText("Click here to turn border painting on or off.");
  226.         bordered.setMnemonic('b');
  227.      bordered.addItemListener(swing.buttonDisplayListener);
  228.      leftColumn.add(bordered);
  229.  
  230.      JCheckBox focused = new JCheckBox("Paint Focus");
  231.     focused.setToolTipText("Click here to turn focus painting on or off.");
  232.         focused.setMnemonic('f');
  233.      focused.setSelected(true);
  234.      focused.addItemListener(swing.buttonDisplayListener);
  235.      leftColumn.add(focused);
  236.  
  237.     JCheckBox enabled = new JCheckBox("Enabled");
  238.     enabled.setSelected(true);
  239.     enabled.setToolTipText("Click here to enable or disable the radio buttons.");
  240.         enabled.setMnemonic('e');
  241.     enabled.addItemListener(swing.buttonDisplayListener);
  242.     leftColumn.add(enabled);
  243.  
  244.  
  245.     leftColumn.add(Box.createRigidArea(swing.vpad20));
  246.  
  247.     
  248.     l = new JLabel("Pad Amount:");
  249.     leftColumn.add(l);
  250.     l.setFont(swing.boldFont);
  251.     
  252.     group = new ButtonGroup();
  253.     JRadioButton defaultPad = new JRadioButton("Default");
  254.         defaultPad.setMnemonic('d');
  255.     defaultPad.setToolTipText("Uses the default padding between the border and label.");
  256.     group.add(defaultPad);
  257.     defaultPad.setSelected(true);
  258.      defaultPad.addItemListener(swing.buttonPadListener);
  259.     leftColumn.add(defaultPad);
  260.  
  261.     JRadioButton zeroPad = new JRadioButton("0");
  262.         zeroPad.setMnemonic('0');
  263.     group.add(zeroPad);
  264.     zeroPad.setToolTipText("Uses no padding between the border and label.");
  265.      zeroPad.addItemListener(swing.buttonPadListener);
  266.     leftColumn.add(zeroPad);
  267.  
  268.     JRadioButton tenPad = new JRadioButton("10");
  269.         tenPad.setMnemonic('1');
  270.     tenPad.setToolTipText("Uses a 10 pixel pad between the border and label.");
  271.     group.add(tenPad);
  272.      tenPad.addItemListener(swing.buttonPadListener);
  273.     leftColumn.add(tenPad);
  274.     
  275.     leftColumn.add(Box.createRigidArea(swing.vpad20));
  276.  
  277.     // *************** Create the layout controls ****************
  278.     // Create Text Position Layout control
  279.     JPanel textPosition = DirectionButton.createDirectionPanel(true, "E", swing.textPositionListener);
  280.     JPanel labelAlignment = DirectionButton.createDirectionPanel(true, "C", swing.labelAlignmentListener);
  281.  
  282.     l = new JLabel("Text Position:");
  283.     rightColumn.add(l);
  284.     l.setFont(swing.boldFont);
  285.      rightColumn.add(textPosition);
  286.  
  287.      rightColumn.add(Box.createRigidArea(swing.vpad20));
  288.  
  289.     l = new JLabel("Content Alignment:");
  290.     rightColumn.add(l);
  291.     l.setFont(swing.boldFont);
  292.      rightColumn.add(labelAlignment);
  293.  
  294.      rightColumn.add(Box.createGlue());
  295.  
  296.     add(buttonPanel);
  297.     add(Box.createRigidArea(swing.hpad10));
  298.      add(controls);
  299.     }
  300.  
  301.     
  302. }
  303.