Java-GNOME 2.10 GNOME Tutorial
<<< Previous | Layout Management | Next >>>

# Using Layout Managers

In this section we will expand our example by adding widgets using both the box and table layout managers. The example used in this section is Fourth.java and I recommend you run the program and look at the layout of the widgets as we discuss the code.

We add two tabs to our example, one to demonstrate the Box classes and one to demonstrate Table. The tabs layout the same set of widgets so you contrast the code necessary to build the window. Since our primary objective is to understand how to use the GNOME layout managers we have only fully implemented the first tab by connecting callbacks.

Unlike the previous chapters, we will only show the code that has changed in this version of the example. The first thing we do is declare a few variables that will be used in the example.

Example 1. Fourth.java - variable declaration

    	// widgets used for the boxes tab
    	private VScale vscale = null;
    	private HScale hscale = null;
    	private CheckButton button = null;
    	private Adjustment adj = null;
    	private ComboBox valuePosition = null;
    	private ComboBox updatePolicy = null;

    	// widgets used for the table tab
    	private VScale vscale2 = null;
    	private HScale hscale2 = null;
    	private CheckButton button2 = null;
    	private Adjustment adj2 = null;
    	private ComboBox valuePosition2 = null;
    	private ComboBox updatePolicy2 = null;

Next we add a call to createView() in our constructor.

Example 2. Fourth.java - constructor

        public Fourth() {
            createMainWindow();
            createMenusAndStatusbar();
            createToolbar();
            createView();
            app.showAll();
        }

The createView() method is quite simple. We just create a Notebook, add it to the App with the setContents() method and then add two pages.

Example 3. Fourth.java - createView()

     	private void createView() {
    		// Create a notebook to hold our two example pages
    		Notebook notebook = new Notebook();
    		notebook.setTabPosition(PositionType.TOP);
    		app.setContent(notebook);

    		notebook.appendPage(buildBoxPage(), new Label("GtkBox"));
    		notebook.appendPage(buildTablePage(), new Label("GtkTable"));
    	}

It is in the buildBoxPage() and buildTablePage() methods that the real work begins. Let's take a peak.

## Box

The buildBoxPage() method demonstrates the Box classes. The method creates a VBox object and proceeds to create a series of HBox objects to hold the widgets that represent a row and add it to the main vbox. The only exception to this rule is the first HBox which also contains a VBox to contain the two widgets in the upper right corner. Let's look at the code.

Example 4. Fourth.java - buildBoxPage()

    	private Widget buildBoxPage() {

    		VBox mainBox = new VBox(false, 0);

    		HBox hbox = new HBox(false, 10);
    		hbox.setBorderWidth(10);
    		mainBox.packStart(hbox, true, true, 0);

    		Adjustment adj1 = new Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
    		vscale = new VScale(adj1);
    		setDefaultValues(vscale);
    		hbox.packStart(vscale, true, true, 0);

    		VBox vbox = new VBox(false, 10);
    		hbox.packStart(vbox, true, true, 0);

    		hscale = new HScale(adj1);
    		hscale.setMinimumSize(200, 30);
    		setDefaultValues(hscale);
    		vbox.packStart(hscale, true, true, 0);

    		HScrollBar scrollbar = new HScrollBar(adj1);
    		scrollbar.setUpdatePolicy(UpdateType.CONTINUOUS);
    		vbox.packStart(scrollbar, true, true, 0);

    		hbox = new HBox(false, 10);
    		hbox.setBorderWidth(10);
    		mainBox.packStart(hbox, true, true, 0);

    		button = new CheckButton("Display value on scale widgets", false);
    		button.setState(true);
    		button.addListener(new ButtonListener() {
    			public void buttonEvent(ButtonEvent event) {
    				if (event.isOfType(ButtonEvent.Type.CLICK))
    					drawValue();
    			}
    		});
    		hbox.packStart(button, true, true, 0);

    		hbox = new HBox(false, 10);
    		hbox.setBorderWidth(10);

    		Label label = new Label("Scale Value Position:");
    		hbox.packStart(label, false, false, 0);

    		valuePosition = new ComboBox();
    		valuePosition.appendText("Top");
    		valuePosition.appendText("Bottom");
    		valuePosition.appendText("Left");
    		valuePosition.appendText("Right");
    		valuePosition.addListener(new ComboBoxListener() {
    			public void comboBoxEvent(ComboBoxEvent event) {
    				int index = valuePosition.getActive();
    				if (0 == index)
    					posMenuSelect(PositionType.TOP);
    				else if (1 == index)
    					posMenuSelect(PositionType.BOTTOM);
    				else if (2 == index)
    					posMenuSelect(PositionType.LEFT);
    				else
    					posMenuSelect(PositionType.RIGHT);
    			}
    		});
    		hbox.packStart(valuePosition, true, true, 0);

    		mainBox.packStart(hbox, true, true, 0);

    		hbox = new HBox(false, 10);
    		hbox.setBorderWidth(10);

    		label = new Label("Scale Update Policy:");
    		hbox.packStart(label, false, false, 0);

    		updatePolicy = new ComboBox();
    		updatePolicy.appendText("Continuous");
    		updatePolicy.appendText("Discontinuous");
    		updatePolicy.appendText("Delayed");
    		updatePolicy.addListener(new ComboBoxListener() {
    			public void comboBoxEvent(ComboBoxEvent event) {
    				int index = updatePolicy.getActive();
    				if (0 == index)
    					updateMenuSelect(UpdateType.CONTINUOUS);
    				else if (1 == index)
    					updateMenuSelect(UpdateType.DISCONTINUOUS);
    				else
    					updateMenuSelect(UpdateType.DELAYED);
    			}
    		});
    		hbox.packStart(updatePolicy, true, true, 0);

    		mainBox.packStart(hbox, true, true, 0);

    		hbox = new HBox(false, 10);
    		hbox.setBorderWidth(10);

    		label = new Label("Scale Digits:");
    		hbox.packStart(label, false, false, 0);

    		adj = new Adjustment(1.0, 0.0, 5.0, 1.0, 1.0, 0.0);
    		adj.addListener(new AdjustmentListener() {
    			public void adjustmentEvent(AdjustmentEvent event) {
    				digitsScale();
    			}
    		});
    		HScale scale = new HScale(adj);
    		scale.setDigits(0);
    		hbox.packStart(scale, true, true, 0);

    		mainBox.packStart(hbox, true, true, 0);

    		return mainBox;
    	}

## Table

The buildTablePage() method demonstrates the Table class. The method builds a 6 X 3 table and proceeds to add the widgets to the grid.

Example 5. Fourth.java - buildBoxPage()

    	private Widget buildTablePage() {

    		Table table = new Table(6, 3, false);

    		Adjustment adj1 = new Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
    		vscale2 = new VScale(adj1);
    		setDefaultValues(vscale2);
    		table.attach(vscale2, 0, 1, 0, 2, AttachOptions.FILL, AttachOptions.FILL, 2, 10);

    		hscale2 = new HScale(adj1);
    		hscale2.setMinimumSize(200, 30);
    		setDefaultValues(hscale2);
    		table.attach(hscale2, 1, 3, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 5, 5);

    		HScrollBar scrollbar = new HScrollBar(adj1);
    		scrollbar.setUpdatePolicy(UpdateType.CONTINUOUS);
    		table.attach(scrollbar, 1, 3, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 5, 5);

    		button2 = new CheckButton("Display value on scale widgets", false);
    		button2.setState(true);
    		table.attach(button2, 0, 3, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 5, 10);

    		Label label = new Label("Scale Value Position:");
    		table.attach(label, 0, 2, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 2, 10);

    		valuePosition2 = new ComboBox();
    		valuePosition2.appendText("Top");
    		valuePosition2.appendText("Bottom");
    		valuePosition2.appendText("Left");
    		valuePosition2.appendText("Right");
    		valuePosition2.addListener(new ComboBoxListener() {
    			public void comboBoxEvent(ComboBoxEvent event) {
    				int index = valuePosition2.getActive();
    				if (0 == index)
    					posMenuSelect(PositionType.TOP);
    				else if (1 == index)
    					posMenuSelect(PositionType.BOTTOM);
    				else if (2 == index)
    					posMenuSelect(PositionType.LEFT);
    				else
    					posMenuSelect(PositionType.RIGHT);
    			}
    		});
    		table.attach(valuePosition2, 2, 3, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 2, 10);

    		label = new Label("Scale Update Policy:");
    		table.attach(label, 0, 2, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 2, 10);

    		updatePolicy2 = new ComboBox();
    		updatePolicy2.appendText("Continuous");
    		updatePolicy2.appendText("Discontinuous");
    		updatePolicy2.appendText("Delayed");
    		updatePolicy2.addListener(new ComboBoxListener() {
    			public void comboBoxEvent(ComboBoxEvent event) {
    				int index = updatePolicy2.getActive();
    				if (0 == index)
    					updateMenuSelect(UpdateType.CONTINUOUS);
    				else if (1 == index)
    					updateMenuSelect(UpdateType.DISCONTINUOUS);
    				else
    					updateMenuSelect(UpdateType.DELAYED);
    			}
    		});
    		table.attach(updatePolicy2, 2, 3, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 2, 10);

    		label = new Label("Scale Digits:");
    		table.attach(label, 0, 1, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 2, 5);

    		adj2 = new Adjustment(1.0, 0.0, 5.0, 1.0, 1.0, 0.0);
    		HScale scale = new HScale(adj2);
    		scale.setDigits(0);
    		table.attach(scale, 1, 3, 5, 6, AttachOptions.FILL, AttachOptions.FILL, 3, 5);
    		return table;
    	}

Laying out widgets for a window is an art that requires a lot of practice. Quite often to achieve the best effect you will want to use a combination of boxes and tables.

<<< Previous | Home | Next >>>
Tables | Up | Containers