Java-GNOME 2.10 GNOME Tutorial
<<< Previous | The Widgets | Next >>>

# CheckButton

A CheckButton widget displays a small button with a label next to it. The button can be toggled on or off by the mouse, and will retain its state until it is toggled again. This widget fires a ToggleEvent when it's state changes.

The following is an example that demonstrates how to create and handle the events for a CheckButton

Example 2. CheckButtonExample.java - CheckButton

    import org.gnu.gtk.CheckButton;
    import org.gnu.gtk.Gtk;
    import org.gnu.gtk.VBox;
    import org.gnu.gtk.Window;
    import org.gnu.gtk.WindowType;
    import org.gnu.gtk.event.LifeCycleEvent;
    import org.gnu.gtk.event.LifeCycleListener;
    import org.gnu.gtk.event.ToggleEvent;
    import org.gnu.gtk.event.ToggleListener;

    public class CheckButtonExample implements LifeCycleListener {
    	CheckButton checkButton1;
    	CheckButton checkButton2;
    	CheckButton checkButton3;

    	public CheckButtonExample() {

    		Window window = new Window(WindowType.TOPLEVEL);
    		window.setTitle("CheckButton Example");
    		window.addListener(this);
    		window.setBorderWidth(20);

    		VBox vbox = new VBox(false, 10);
    		window.add(vbox);

    		checkButton1 = new CheckButton("Disable Button 3", false);
    		checkButton1.addListener(new ToggleListener() {
    			public void toggleEvent(ToggleEvent event) {
    				if (checkButton1.getState())
    					checkButton3.setSensitive(false);
    				else
    					checkButton3.setSensitive(true);
    			}
    		});
    		vbox.packStart(checkButton1, true, true, 0);

    		checkButton2 = new CheckButton("Disable Button 1", false);
    		checkButton2.addListener(new ToggleListener() {
    			public void toggleEvent(ToggleEvent event) {
    				if (checkButton2.getState())
    					checkButton1.setSensitive(false);
    				else
    					checkButton1.setSensitive(true);
    			}
    		});
    		vbox.packStart(checkButton2, true, true, 0);

    		checkButton3 = new CheckButton("Say Hello", false);
    		checkButton3.addListener(new ToggleListener() {
    			public void toggleEvent(ToggleEvent event) {
    				if (checkButton2.getState())
    					System.out.println("Hello");
    			}
    		});
    		vbox.packStart(checkButton3, true, true, 0);

    		window.showAll();
    	}

    	public void lifeCycleEvent(LifeCycleEvent event) {
    		if (event.isOfType(LifeCycleEvent.Type.DESTROY) ||
    			event.isOfType(LifeCycleEvent.Type.DELETE)) {
    			Gtk.mainQuit();
    		}
    	}

    	public static void main(String[] args) {
    		Gtk.init(args);
    		CheckButtonExample cb = new CheckButtonExample();
    		Gtk.main();
    	}
    }

<<< Previous | Home | Next >>>
The Widgets | Up | Label