Button Html Demo : Button « Swing JFC « Java






Button Html Demo

Button Html Demo
  
/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * ButtonHtmlDemo.java is a 1.4 application that uses the following files:
 * images/right.gif images/middle.gif images/left.gif
 */
public class ButtonHtmlDemo extends JPanel implements ActionListener {
  protected JButton b1, b2, b3;

  public ButtonHtmlDemo() {
    ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
    ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
    ImageIcon rightButtonIcon = createImageIcon("images/left.gif");

    b1 = new JButton("<html><center><b><u>D</u>isable</b><br>"
        + "<font color=#ffffdd>middle button</font>", leftButtonIcon);
    Font font = b1.getFont().deriveFont(Font.PLAIN);
    b1.setFont(font);
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for
                                // left-to-right
                                // locales
    b1.setMnemonic(KeyEvent.VK_D);
    b1.setActionCommand("disable");

    b2 = new JButton("middle button", middleButtonIcon);
    b2.setFont(font);
    b2.setForeground(new Color(0xffffdd));
    b2.setVerticalTextPosition(AbstractButton.BOTTOM);
    b2.setHorizontalTextPosition(AbstractButton.CENTER);
    b2.setMnemonic(KeyEvent.VK_M);

    b3 = new JButton("<html><center><b><u>E</u>nable</b><br>"
        + "<font color=#ffffdd>middle button</font>", rightButtonIcon);
    b3.setFont(font);
    //Use the default text position of CENTER, TRAILING (RIGHT).
    b3.setMnemonic(KeyEvent.VK_E);
    b3.setActionCommand("enable");
    b3.setEnabled(false);

    //Listen for actions on buttons 1 and 3.
    b1.addActionListener(this);
    b3.addActionListener(this);

    b1.setToolTipText("Click this button to disable the middle button.");
    b2.setToolTipText("This middle button does nothing when you click it.");
    b3.setToolTipText("Click this button to enable the middle button.");

    //Add Components to this container, using the default FlowLayout.
    add(b1);
    add(b2);
    add(b3);
  }

  public void actionPerformed(ActionEvent e) {
    if ("disable".equals(e.getActionCommand())) {
      b2.setEnabled(false);
      b1.setEnabled(false);
      b3.setEnabled(true);
    } else {
      b2.setEnabled(true);
      b1.setEnabled(true);
      b3.setEnabled(false);
    }
  }

  /** Returns an ImageIcon, or null if the path was invalid. */
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = ButtonHtmlDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("ButtonHtmlDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    ButtonHtmlDemo newContentPane = new ButtonHtmlDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}

           
         
    
  








Related examples in the same category

1.Creating a JButton Component from Action
2.Various Swing buttonsVarious Swing buttons
3.Responding to button pressesResponding to button presses
4.Putting buttons on an appletPutting buttons on an applet
5.Uses reflection to create groups of different types of AbstractButtonUses reflection to create groups of different types of AbstractButton
6.The Swing-ified button exampleThe Swing-ified button example
7.Create a JButton that does not show focusCreate a JButton that does not show focus
8.Demonstration of button events including Action, Item and Change event typesDemonstration of button events including Action, Item and Change event types
9.The event demonstration program for JToggleButtonThe event demonstration program for JToggleButton
10.An example of radio button menu items in actionAn example of radio button menu items in action
11.JToggleButton DemoJToggleButton Demo
12.A Selected ButtonA Selected Button
13.Button demo: Mnemonic, alignment and action command Button demo: Mnemonic, alignment and action command
14.Button action to change the panel backgroundButton action to change the panel background
15.HTML buttonHTML button
16.Creating a Multiline Label for a JButton Component
17.Lines are left justified. This label text will center the lines
18.Label text italicizes the second line
19.Changing the Label of a JButton Component
20.Ploygon ButtonPloygon Button
21.Simple Button UISimple Button UI
22.Displaying a Button with an Icon LabelDisplaying a Button with an Icon Label
23.Displaying a Button with a BorderDisplaying a Button with a Border
24.Displaying a Button with Varying IconsDisplaying a Button with Varying Icons
25.Displaying a Button with Various Label AlignmentsDisplaying a Button with Various Label Alignments
26.Working with Tooltip TextWorking with Tooltip Text
27.Sharing Models between Buttons
28.Sharing an Action between JButton ComponentsSharing an Action between JButton Components
29.Display Message
30.Swing Button DemoSwing Button Demo
31.LoadSave: Button actionLoadSave: Button action
32.Button with ICON and Multiline Button with ICON and Multiline
33.Action Button SampleAction Button Sample
34.Mnemonic SampleMnemonic Sample
35.Swing Default ButtonSwing Default Button
36.Button Action SampleButton Action Sample
37.Button icons, a default button, HTML in a button,and button mnemonics.
38.Adding a Rollover and Pressed Icon to a JButton Component
39.Adding a Disabled Icon to a JButton Component
40.Setting the Gap Size Between the Label and Icon in a JButton Component
41.Moving the Icon in a JButton Component
42.Adding an Icon to a JButton Component
43.Moving the Label/Icon Pair in a JButton Component
44.If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
45.Dynamically update the appearance of a component
46.Simple ToggleButton SampleSimple ToggleButton Sample