aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem creating a dynamic form with SWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem creating a dynamic form with SWT" Watch "Problem creating a dynamic form with SWT" New topic
Author

Problem creating a dynamic form with SWT

Anket Dhulekar
Greenhorn

Joined: Jun 26, 2009
Posts: 5
Hi Everyone,

I am facing a problem while making a dynamic form.

What I am trying to do is to make a part of the form dynamic, i.e. it can have different controls depending upon the content available.

If an image is available, then we have to display the image and the associated information. If the image is not available, then we have to display only the information, and no image will be displayed.

I am using the following code as my basis :

mport org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Test {

static int pageNum = 0;
static Composite pageComposite;

public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Push");
pageComposite = new Composite(shell, SWT.NONE);
pageComposite.setLayout(new GridLayout());
pageComposite.setLayoutData(new GridData());

button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if ((pageComposite != null) && (!pageComposite.isDisposed())) {
pageComposite.dispose();
}
pageComposite = new Composite(shell, SWT.NONE);
pageComposite.setLayout(new GridLayout());
pageComposite.setLayoutData(new GridData());
if (pageNum++ % 2 == 0) {
Table table = new Table(pageComposite, SWT.BORDER);
table.setLayoutData(new GridData());
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.NONE).setText("table item "
+ i);
}
} else {
new Button(pageComposite, SWT.RADIO).setText("radio");
}
shell.layout(true);
}
});

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

which displays contents correctly, while in my example :

--------testArea = new Composite(shell, SWT.BORDER);
--------testArea.setLayout(new GridLayout());
--------GridData testAreaData = new GridData(GridData.FILL_BOTH);
--------testAreaData.grabExcessVerticalSpace = true;
--------testAreaData.grabExcessHorizontalSpace = true;
--------testAreaData.horizontalSpan = 9;
--------testArea.setLayoutData(testAreaData);

Here's the code for the button which when clicked should generate the dynamic content.

--------startTestButton.addSelectionListener(new SelectionListener(){

------------@Override
------------public void widgetDefaultSelected(SelectionEvent e) {
----------------// TODO Auto-generated method stub
----------------
------------}

------------@Override
------------public void widgetSelected(SelectionEvent e) {
----------------Control children[] = testArea.getChildren();
------------------------for(Control c:children){
---------------------------c.dispose();
------------------------}
----------------Button b = new Button(testArea, SWT.RADIO);
----------------b.setText("radio");
----------------shell.layout(true);
------------}
------------
--------});

I don't know what I am doing wrong here, but I am not able to see the dynamic button on the screen.

SCJP 6.0, SCWCD 5.0, IBM Certified Database Associate for DB2 9
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem creating a dynamic form with SWT