Working with Toggle Buttons : ToggleButton « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing JFC » ToggleButtonScreenshots 
Working with Toggle Buttons
Working with Toggle Buttons
  
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class ToggleTest {
  static class TriangleIcon implements Icon {
    String name;

    static class State {
      public static final int NORMAL = 0;

      public static final int PRESSED = 1;

      public static final int ROLLOVER = 2;

      public static final int SELECTED = 3;

      private State() {
      }
    }

    int state;

    Color color;

    public TriangleIcon(Color c, int state) {
      color = c;
      this.state = state;
    }

    public int getIconWidth() {
      return 20;
    }

    public int getIconHeight() {
      return 20;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.setColor(color);
      Polygon p = new Polygon();
      if (state == State.NORMAL) {
        p.addPoint(x + (getIconWidth() 2), y);
        p.addPoint(x, y + getIconHeight() 1);
        p.addPoint(x + getIconWidth() 1, y + getIconHeight() 1);
      else if (state == State.PRESSED) {
        p.addPoint(x, y);
        p.addPoint(x + getIconWidth() 1, y);
        p.addPoint(x + (getIconWidth() 2), y + getIconHeight() 1);
      else if (state == State.SELECTED) {
        p.addPoint(x + getIconWidth() 1, y);
        p.addPoint(x + getIconWidth() 1, y + getIconHeight() 1);
        p.addPoint(x, y + (getIconHeight() 2));
      else {
        p.addPoint(x, y);
        p.addPoint(x, y + getIconHeight() 1);
        p.addPoint(x + getIconWidth() 1, y + (getIconHeight() 2));
      }
      g.fillPolygon(p);
    }
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(01));
    Icon normalIcon = new TriangleIcon(Color.red, TriangleIcon.State.NORMAL);
    Icon pressedIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.PRESSED);
    Icon selectedIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.SELECTED);
    Icon rolloverIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.ROLLOVER);
    JToggleButton b = new JToggleButton("Initially UnSelected");
    contentPane.add(b);
    b = new JToggleButton("Initially Selected"true);
    contentPane.add(b);
    b = new JToggleButton(normalIcon);
    b.setPressedIcon(pressedIcon);
    b.setSelectedIcon(selectedIcon);
    b.setRolloverIcon(rolloverIcon);
    b.setRolloverEnabled(true);
    contentPane.add(b);
    frame.setSize(300150);
    frame.show();
  }
}


           
         
    
  
Related examples in the same category
1.JToggleButton is a button that has two states. Pressed and not pressed.
2.This class represents the toggle buttons used in toolbars.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.