hhheng 0 Newbie Poster

Newbie questions. I laid out the controls in shell by GridLayout(2, false). And when user click a checkbox, I wantto to hide one or several lines. I tried to set the exclude to true, and set the control in that line GridData(0, 0) to hide those lines. However there were many problems. Code as below:

BTW, are there any other ways to hide and show controls while using SWT?

Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Just for testing");
		GridLayout layout = new GridLayout(2, false);
		shell.setLayout(layout);
		
		Label label = new Label(shell, SWT.None);
		label.setText("User:");
		GridData data1 = new GridData(80, SWT.DEFAULT);
		label.setLayoutData(data1);
		Text text = new Text(shell, SWT.SINGLE);
		GridData data2 = new GridData(150, SWT.DEFAULT);
		data2.horizontalAlignment = GridData.BEGINNING;
		text.setLayoutData(data2);
		
		new Label(shell, SWT.None).setText("");
		Button b = new Button(shell, SWT.CHECK);
		b.setText("Add more strings???????????");
		
		final Label dlabel = new Label(shell, SWT.None);    //Trying to hide this line
		dlabel.setText("Description:");
		final Text dtext = new Text(shell, SWT.MULTI);
		final GridData hide = new GridData(0, 0);
		hide.exclude = true;
		dtext.setLayoutData(hide);
		dlabel.setLayoutData(hide);
		
		label = new Label(shell, SWT.None);
		label.setText("User:");
		data1 = new GridData(80, SWT.DEFAULT);
		label.setLayoutData(data1);
		text = new Text(shell, SWT.SINGLE);
		data2 = new GridData(150, SWT.DEFAULT);
		data2.horizontalAlignment = GridData.BEGINNING;
		text.setLayoutData(data2);	
		
		final GridData ldata = new GridData(80, SWT.DEFAULT);
		final GridData rdata = new GridData(150, 50);
		
		b.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e){
				if(dtext.getLayoutData() == hide){
					dlabel.setLayoutData(ldata);
					dtext.setLayoutData(rdata);
					dlabel.setVisible(true);
					dtext.setVisible(true);
				}else{
					dlabel.setLayoutData(hide);
					dtext.setLayoutData(hide);
					dlabel.setVisible(false);
					dtext.setVisible(false);
				}
				
				shell.layout();
			}
		});
		
		shell.setSize(300, 400);
		shell.open();
		while(!shell.isDisposed()){
			if(display.readAndDispatch()) display.sleep();
		}

Help please, thanks a lot.

Dani AI

Generated

— common causes when hiding rows with GridLayout are sharing the same GridData instance between controls, not excluding every control in that row (including any empty/placeholder Labels), and not forcing a relayout or pack after the change. A reliable pattern is: save each control's original GridData, replace it with a new GridData whose exclude flag is true (and call setVisible(false) if you want the control not to accept focus/events), then call the parent composite's layout (or shell.pack() if you want the window to shrink). Restore the saved GridData and call layout to show it again.

Example toggle (do not reuse the same GridData object for multiple controls):

GridData original = (GridData) control.getLayoutData();
GridData hidden = new GridData();
hidden.exclude = true;
control.setLayoutData(hidden);
control.setVisible(false);
parent.layout(true, true);

// to restore
control.setLayoutData(original);
control.setVisible(true);
parent.layout(true, true);

For hiding whole "rows" more cleanly, put the row's widgets inside a small Composite (one composite per row). Then toggle the composite’s GridData.exclude and setVisible. That keeps layout bookkeeping simple and avoids missing a placeholder cell.

If toggling still leaves gaps: check for any placeholder labels or controls that weren’t excluded; avoid fixing the shell size with setSize() (use pack() or let layout calculate sizes); for multi-line Text controls restore a sensible heightHint or compute the preferred size with computeSize(...) before restoring.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.