Trying to create basic JFace application, not sure what issue is

I tried to add a composite element underneath the shell element, and now the button widgets are not being displayed. Am I missing something?

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class RadioButtonDemo {

    /**
     * Create radio buttons
     * 
     * @param parent
     *            Parent widget
     * @param labels
     *            Array of labels for the radio buttons
     * @param defaultSelection
     *            The default button to select
     * @return The newly created buttons complete with labels
     */
    public final Button[] getRadioButtons(final Composite parent,
            final String[] labels, int defaultSelection) {
        // some sanity stuff
        assert (defaultSelection < labels.length);
        assert (!parent.equals(null));

        final Button[] buttons = new Button[labels.length];
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new Button(parent, SWT.RADIO);
            buttons[i].setText(labels[i]);
            buttons[i].setSelection(defaultSelection == i);
        }

        return buttons;
    }

    public void showDemo() {
        // some setup
        final Display display = new Display();

        Shell shell = new Shell(display, SWT.DIALOG_TRIM);
        // shell.setLayout(new RowLayout());
        shell.setSize(new Point(200, 200)); // make it small for the demo
        shell.setText("Radio button demo");
        shell.setLayout(new FillLayout());

        Composite c = new Composite(shell, SWT.NONE);

        final String[] labels = new String[] {
                "Never delete code coverage launches from history",
                "Delete oldest code coverage launches from history" };
        final Button[] radioButtons = this.getRadioButtons(c, labels, 0);

        shell.pack(); // min-size...
        shell.open(); // open shell

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose(); // cleanup
    }

    public static void main(String[] args) {
        // new instance
        RadioButtonDemo instance = new RadioButtonDemo();
        instance.showDemo();

    }
}

Answer

You need to set a layout on c, e.g.

Composite c = new Composite(shell, SWT.NONE);
c.setLayout(new FillLayout());

Leave a Reply

Your email address will not be published. Required fields are marked *