Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » GridData problem
GridData problem [message #456394] Thu, 02 June 2005 09:24 Go to next message
hortiz Mising name is currently offline hortiz Mising nameFriend
Messages: 96
Registered: July 2009
Member
Hi,

I have a problem with GridData / GridLayout configuration.
I use in my application (which runs on Linux) a general tool bar which
contains :
- a command line,

- a progress bar,

- and a set of buttons which is sometimes visible, sometimes not.


My problem occurs when hiding the parent composite of the buttons : all
others widgets are moved to the top of the parent composite, whereas they
were correctly vertical aligned before.

Here a snippet to show the problem : click on show/hide button, and you
will see what I mean.

Thanks,
Helene

// ----------------------------
// THE MAIN CLASS
// ----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 GridDataProblem {

private static Display display;
private static Composite mainComposite;
private static SampleComposite sampleComposite;
private static boolean show = false;

public static void main(String[] args) {

display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new GridLayout());

mainComposite = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
mainComposite.setLayoutData(gridData);
mainComposite.setLayout(new GridLayout());

sampleComposite = new SampleComposite(mainComposite, SWT.NONE);

Button hideShow = new Button(mainComposite, SWT.PUSH);
hideShow.setText("Hide/Show");

hideShow.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
sampleComposite.refresh(show);
show = !show;
}
});

shell.setSize(800,200);
shell.open();

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

// ----------------------------
// A SPECIFIC COMPOSITE
// ----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;

public final class SampleComposite extends Composite {

private Text commandLine;
private ProgressBar progressBar;
private Composite buttonsComposite;
private Button pauseButton;
private Button startStopButton;

public SampleComposite(Composite parent, int style) {
super(parent, style | SWT.NO_REDRAW_RESIZE);
this.setLayout(getGridLayout(4));
createContent();
setGridData();
this.setBackground(getDisplay().getSystemColor(SWT.COLOR_GRE EN));
}


public void refresh(boolean show) {
if (show) {
showButtons();
} else {
hideButtons();
}
this.layout();
}


private void showButtons() {
buttonsComposite.setVisible(true);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 1;
}


private void hideButtons() {
buttonsComposite.setVisible(false);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 2;
}


private void createContent() {
createCommandLine();
createStatusAreaProgressBar();
createButtons();
}


private void createCommandLine() {
Label commandLineLabel = new Label(this, SWT.NONE);
commandLineLabel.setText("Command :");
commandLineLabel.setLayoutData(getNewGridData(false));
this.commandLine = new Text(this, SWT.BORDER);
this.commandLine.setLayoutData(getNewGridData(true));
}


private void createStatusAreaProgressBar() {
this.progressBar = new ProgressBar(this, SWT.BORDER|SWT.SMOOTH);
this.progressBar.setMaximum(100);
this.progressBar.setMinimum(0);
this.progressBar.setLayoutData(getNewGridData(true));
}


private void createButtons() {
buttonsComposite = new Composite(this, SWT.NONE);
buttonsComposite.setLayout(getGridLayout(2));
buttonsComposite.setLayoutData(getNewGridData(false));

this.pauseButton = new Button(buttonsComposite, SWT.PUSH);
this.pauseButton.setText("Pause");
this.pauseButton.setLayoutData(getNewGridData(false));

this.startStopButton = new Button(buttonsComposite, SWT.PUSH);
this.startStopButton.setText("Start");
this.startStopButton.setLayoutData(getNewGridData(false));
}


private void setGridData() {
GridData statusGridData = new GridData();
statusGridData.grabExcessHorizontalSpace = true;
statusGridData.grabExcessVerticalSpace = false;
statusGridData.horizontalAlignment = GridData.FILL;
statusGridData.verticalAlignment = GridData.FILL;
this.setLayoutData(statusGridData);
}

private GridData getNewGridData(boolean grabExcessHorizontalSpace) {
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.CENTER;
return gridData;
}

public GridLayout getGridLayout(int nbColumns) {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = nbColumns;
gridLayout.horizontalSpacing = 8;
gridLayout.verticalSpacing = 8;
return gridLayout;
}
}
Re: GridData problem [message #456433 is a reply to message #456394] Fri, 03 June 2005 13:45 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
In your code you have:

private void showButtons() {
buttonsComposite.setVisible(true);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 1;
}


private void hideButtons() {
buttonsComposite.setVisible(false);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 2;
}

1) When you make the buttonsComposite not visible, this does not exclude it
from the GridLayout, in order to exclude it, you must use GridData.exclude =
true.
If you change refresh() to call this.getShell().layout(true, true) instead
of this.layout() you will see that the preferred height of SampleComposite
has actually grown to have two rows.

2) The height of the row is based on what is in the row. The buttons Pause
and Start happen to be the tallest items in the row. When you make the
progress bar span two columns, you push buttonsComposite to the next row.
As a result, the height of the row with the progress bar is reduced making
the progressbar etc move up.

In order to keep the vertical alignment of the progress bar etc, I would
recommend that you do not exclude them from the layout but rather modify
their width hint. Try:

private void showButtons() {
buttonsComposite.setVisible(true);
GridData gridData = (GridData)buttonsComposite.getLayoutData();
gridData.widthHint = SWT.DEFAULT;
}


private void hideButtons() {
buttonsComposite.setVisible(false);
GridData gridData = (GridData)buttonsComposite.getLayoutData();
gridData.widthHint = 0;
}

"hortiz" <hortiz@xxxxx.xxx> wrote in message
news:d344d75946be7927919a544a23b7b955$1@www.eclipse.org...
> Hi,
>
> I have a problem with GridData / GridLayout configuration.
> I use in my application (which runs on Linux) a general tool bar which
> contains :
> - a command line,
>
> - a progress bar,
>
> - and a set of buttons which is sometimes visible, sometimes not.
>
>
> My problem occurs when hiding the parent composite of the buttons : all
> others widgets are moved to the top of the parent composite, whereas they
> were correctly vertical aligned before.
>
> Here a snippet to show the problem : click on show/hide button, and you
> will see what I mean.
>
> Thanks,
> Helene
>
> // ----------------------------
> // THE MAIN CLASS
> // ----------------------------
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> 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 GridDataProblem {
> private static Display display;
> private static Composite mainComposite;
> private static SampleComposite sampleComposite;
> private static boolean show = false;
>
> public static void main(String[] args) {
> display = new Display();
> Shell shell = new Shell(display);
>
> shell.setLayout(new GridLayout());
>
> mainComposite = new Composite(shell, SWT.BORDER);
> GridData gridData = new GridData();
> gridData.grabExcessHorizontalSpace = true;
> gridData.grabExcessVerticalSpace = false;
> gridData.horizontalAlignment = GridData.FILL;
> gridData.verticalAlignment = GridData.FILL;
> mainComposite.setLayoutData(gridData);
> mainComposite.setLayout(new GridLayout());
>
> sampleComposite = new SampleComposite(mainComposite, SWT.NONE);
>
> Button hideShow = new Button(mainComposite, SWT.PUSH);
> hideShow.setText("Hide/Show");
>
> hideShow.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected(SelectionEvent arg0) {
> sampleComposite.refresh(show);
> show = !show;
> }
> });
>
> shell.setSize(800,200);
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
> }
>
> // ----------------------------
> // A SPECIFIC COMPOSITE
> // ----------------------------
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.ProgressBar;
> import org.eclipse.swt.widgets.Text;
>
> public final class SampleComposite extends Composite {
> private Text commandLine;
> private ProgressBar progressBar;
> private Composite buttonsComposite;
> private Button pauseButton;
> private Button startStopButton;
> public SampleComposite(Composite parent, int style) {
> super(parent, style | SWT.NO_REDRAW_RESIZE);
> this.setLayout(getGridLayout(4));
> createContent();
> setGridData();
> this.setBackground(getDisplay().getSystemColor(SWT.COLOR_GRE EN));
> }
>
> public void refresh(boolean show) {
> if (show) {
> showButtons();
> } else {
> hideButtons();
> }
> this.layout();
> }
>
> private void showButtons() {
> buttonsComposite.setVisible(true);
> GridData gridData = (GridData)progressBar.getLayoutData();
> gridData.horizontalSpan = 1;
> }
> private void hideButtons() {
> buttonsComposite.setVisible(false);
> GridData gridData = (GridData)progressBar.getLayoutData();
> gridData.horizontalSpan = 2;
> }
>
> private void createContent() {
> createCommandLine();
> createStatusAreaProgressBar();
> createButtons();
> }
> private void createCommandLine() {
> Label commandLineLabel = new Label(this, SWT.NONE);
> commandLineLabel.setText("Command :");
> commandLineLabel.setLayoutData(getNewGridData(false));
> this.commandLine = new Text(this, SWT.BORDER);
> this.commandLine.setLayoutData(getNewGridData(true));
> }
> private void createStatusAreaProgressBar() {
> this.progressBar = new ProgressBar(this, SWT.BORDER|SWT.SMOOTH);
> this.progressBar.setMaximum(100);
> this.progressBar.setMinimum(0);
> this.progressBar.setLayoutData(getNewGridData(true));
> }
> private void createButtons() {
> buttonsComposite = new Composite(this, SWT.NONE);
> buttonsComposite.setLayout(getGridLayout(2));
> buttonsComposite.setLayoutData(getNewGridData(false));
> this.pauseButton = new Button(buttonsComposite, SWT.PUSH);
> this.pauseButton.setText("Pause");
> this.pauseButton.setLayoutData(getNewGridData(false));
> this.startStopButton = new Button(buttonsComposite, SWT.PUSH);
> this.startStopButton.setText("Start");
> this.startStopButton.setLayoutData(getNewGridData(false));
> }
> private void setGridData() {
> GridData statusGridData = new GridData();
> statusGridData.grabExcessHorizontalSpace = true;
> statusGridData.grabExcessVerticalSpace = false;
> statusGridData.horizontalAlignment = GridData.FILL;
> statusGridData.verticalAlignment = GridData.FILL;
> this.setLayoutData(statusGridData);
> }
> private GridData getNewGridData(boolean grabExcessHorizontalSpace) {
> GridData gridData = new GridData();
> gridData.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
> gridData.grabExcessVerticalSpace = false;
> gridData.horizontalAlignment = GridData.FILL;
> gridData.verticalAlignment = GridData.CENTER;
> return gridData; }
> public GridLayout getGridLayout(int nbColumns) {
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = nbColumns;
> gridLayout.horizontalSpacing = 8;
> gridLayout.verticalSpacing = 8;
> return gridLayout;
> }
> }
>
Previous Topic:Eclipse Forms Problem
Next Topic:list box filled with data
Goto Forum:
  


Current Time: Wed Apr 24 17:27:10 GMT 2024

Powered by FUDForum. Page generated in 0.02498 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top