Toolbars in Java Gnome

In this part of the Java Gnome programming tutorial, we will work with toolbars.

Menus group commands that we can use in application. Toolbars provide a quick access to the most frequently used commands.

Simple toolbar

Next we create a simple toolbar.

toolbar.java
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Orientation;
import org.gnome.gtk.SeparatorToolItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates a simple toolbar.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GToolbar extends Window {

    public GToolbar() {
        setTitle("Toolbar");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();
    }
    
    
    public void initUI() {

        Toolbar toolbar = new Toolbar();

        ToolButton newtb = new ToolButton(Stock.NEW);
        ToolButton opentb = new ToolButton(Stock.OPEN);
        ToolButton savetb = new ToolButton(Stock.SAVE);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quittb = new ToolButton(Stock.QUIT);

        toolbar.insert(newtb, 0);
        toolbar.insert(opentb, 1);
        toolbar.insert(savetb, 2);
        toolbar.insert(sep, 3);
        toolbar.insert(quittb, 4);
       
        quittb.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
         
        VBox vbox = new VBox(false, 2);
        vbox.packStart(toolbar, false, false, 0);
        
        add(vbox);
    }
    
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GToolbar();
        Gtk.main();
    }
}

The example shows a toolbar and four tool buttons.

Toolbar toolbar = new Toolbar();

A Toolbar widget is created.

ToolButton newtb = new ToolButton(Stock.NEW);

A ToolButton with an image from stock is created.

SeparatorToolItem sep = new SeparatorToolItem();

This is a separator. It can be used to separate toolbar buttons into logical groups.

toolbar.insert(newtb, 0);
toolbar.insert(opentb, 1);
...

Toolbar buttons are inserted into the toolbar widget.

Toolbar
Figure: Toolbar

Toolbars

In the second example, we show two toolbars. Many applications have more than one toolbar. We show, how we can do it in Java Gnome.

toolbars.java
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;


/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates two toolbar
 * instances.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */


public class GToolbars extends Window {

    public GToolbars() {
        setTitle("Toolbars");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();    
    }
    
    
    public void initUI() {
        
        Toolbar upper = new Toolbar();

        ToolButton newtb = new ToolButton(Stock.NEW);
        ToolButton opentb = new ToolButton(Stock.OPEN);
        ToolButton savetb = new ToolButton(Stock.SAVE);

        upper.insert(newtb, 0);
        upper.insert(opentb, 1);
        upper.insert(savetb, 2);

        Toolbar lower = new Toolbar();

        ToolButton quittb = new ToolButton(Stock.QUIT);
   
        quittb.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
        
        lower.insert(quittb, 0);

        VBox vbox = new VBox(false, 2);
        vbox.packStart(upper, false, false, 0);
        vbox.packStart(lower, false, false, 0);

        add(vbox);
    }
    
    
    public static void main(String[] args) {
        Gtk.init(args);
        new GToolbars();
        Gtk.main();
    }
}

The application shows two toolbars.

Toolbar upper = new Toolbar();
...
Toolbar lower = new Toolbar();

We create two Toolbar widgets.

upper.insert(newtb, 0);
...       
lower.insert(quittb, 0);

Each of them has its own tool buttons.

VBox vbox = new VBox(false, 2);
vbox.packStart(upper, false, false, 0);
vbox.packStart(lower, false, false, 0);

Toolbars are packed into the vertical box, one after the other.

Toolbars
Figure: Toolbars

Undo redo

The following example demonstrates, how we can inactivate toolbar buttons on the toolbar. It is a common practise in GUI programming. For example the save button. If we save all changes of our document to the disk, the save button is inactivated in most text editors. This way the application indicates to the user, that all changes are already saved.

undoredo.java
package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.SeparatorToolItem;
import org.gnome.gtk.Stock;
import org.gnome.gtk.ToolButton;
import org.gnome.gtk.Toolbar;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program creates an undo redo 
 * example. It shows how to enable/disable
 * tool buttons if they cannot be used no
 * more.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GUndoRedo extends Window {

    private int count = 2;
    private ToolButton undo;
    private ToolButton redo;

    public GUndoRedo() {
    
        setTitle("Undo redo");
        
        initUI();
        
        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });
        
        setDefaultSize(300, 250);
        setPosition(WindowPosition.CENTER);
        showAll();    
    }
    
    public void initUI() {
        Toolbar toolbar = new Toolbar();
        
        undo = new ToolButton(Stock.UNDO);
        redo = new ToolButton(Stock.REDO);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quit = new ToolButton(Stock.QUIT);

        toolbar.insert(undo, 0);
        toolbar.insert(redo, 1);
        toolbar.insert(sep, 2);
        toolbar.insert(quit, 3);

        undo.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                count -= 1;

                if (count <= 0) {
                    undo.setSensitive(false);
                    redo.setSensitive(true);
                }
            }
        });
        
        redo.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                count += 1;

                if (count >= 5) {
                    redo.setSensitive(false);
                    undo.setSensitive(true);
                }
            }
        });
        
        quit.connect(new ToolButton.Clicked() {

            public void onClicked(ToolButton toolButton) {
                Gtk.mainQuit();
            }
        });
         
        VBox vbox = new VBox(false, 2);
        vbox.packStart(toolbar, false, false, 0);
        vbox.packStart(new Label(""), false, false, 0);

        add(vbox);

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

Our example creates undo and redo buttons from the Java Gnome stock resources. After several clicks each of the buttons is inactivated. The buttons are grayed out.

private int count = 2;

The count variable decides, which button is activated and deactivated.

undo = new ToolButton(Stock.UNDO);
redo = new ToolButton(Stock.REDO);

We have two tool buttons. Undo and redo tool buttons. Images come from the stock resources.

undo.connect(new ToolButton.Clicked() {
    public void onClicked(ToolButton toolButton) {
         count -= 1;
         if (count <= 0) {
             undo.setSensitive(false);
             redo.setSensitive(true);
         }
     }
 });

We plug a method for the Clicked event for the undo tool button. To activate/deactivate a widget, we use the setSensitive() method.

Undo redo
Figure: Undo redo

In this chapter of the Java Gnome programming library, we mentioned toolbars.