Reputation: 51039
While Swing
architecture supposes that widgets are "added" one into another, SWT
uses another paradigm: each widget is created having parent set in the constructor.
I think former approach is better, because I can isolate a code of creating each control.
For example
// creating button 1
JButton button1 = new JButton("Button 1");
// creating button 2
JButton button2 = new JButton("Button 2");
// creating button 3
JButton button3 = new JButton("Button 3");
// creating panel
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);
// creating frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
We can have three widgets here and each one is created in it's own block of code.
If I want to add one more framing level, I just add new block and one small fix
// creating button 1
JButton button1 = new JButton("Button 1");
// creating button 2
JButton button2 = new JButton("Button 2");
// creating button 3
JButton button3 = new JButton("Button 3");
// creating panel
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel.add(button3);
// creating another panel
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(panel);
// creating frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.add(panel);
frame.add(panel2); // one fix
frame.pack();
frame.setVisible(true);
Not so in SWT
. I have to make many fixes to reassign all child creation code to new parent.
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.HORIZONTAL));
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new RowLayout(SWT.HORIZONTAL));
Composite composite2 = new Composite(composite, SWT.NONE);
composite2.setLayout(new RowLayout(SWT.HORIZONTAL));
//Button button1 = new Button(composite, SWT.PUSH);
Button button1 = new Button(composite2, SWT.PUSH); // fix 1
button1.setText("Button 1");
// Button button2 = new Button(composite, SWT.PUSH);
Button button2 = new Button(composite2, SWT.PUSH); // fix 2
button2.setText("Button 2");
//Button button3 = new Button(composite, SWT.PUSH);
Button button3 = new Button(composite2, SWT.PUSH); // fix 3
button3.setText("Button 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
This is the source of potential errors.
Is there any way to avoid this problem?
Upvotes: 1
Views: 330
Reputation: 5023
The SWT widgets rely on native widgets of the system the application is run in and are tightly bound to them. Look here for an excerpt from the Definitive guide to SWT and JFace where it's nicely explained.
The only solution for this "problem" that comes to my mind would be to create your own proxies for the SWT widgets. A proxy would have the desired setters for the widget's properties and some create
method which would construct the widget the SWT way out of the properties you set to the proxy.
public class ButtonProxy
{
private String text;
private String tooltipText;
// all the other parameters you would like to use
public void setText(String text)
{
this.text = text;
}
public void setTooltipText(String tooltipText)
{
this.tooltipText = tooltipText;
}
// all the other setters
public Button create(Composite parent, int flags)
{
Button btn = new Button(parent, flags);
btn.setText(this.text);
btn.setTooltipText(this.tooltipText);
return btn;
}
}
You could also make some Factory
for the proxies, use a builder design pattern, etc.
Hope this helps!
Upvotes: 1