Reputation: 45
I am very new to UI coding, I have to create multiple wizard pages and then try navigating from one page to another. I have written the below code which I have read on a blog. But I am unable to launch it.
public class MyWizardPage extends WizardPage {
public MyWizardPage() {
super("My Wizard");
}
public void createControl(Composite parent) {
// Create the parent control
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
// Create some controls
new Label(composite, SWT.LEFT).setText("Field #1:");
Text field1 = new Text(composite, SWT.BORDER | SWT.SINGLE);
field1.setLayoutData(new GridData(GridData.FILL_BOTH));
new Label(composite, SWT.LEFT).setText("Field #2:");
Text field2 = new Text(composite, SWT.BORDER | SWT.SINGLE);
field2.setLayoutData(new GridData(GridData.FILL_BOTH));
// Important!
setControl(composite);
}
}
Please help me in launching/running the program.
Modifications:
public class MyWizardPage extends WizardPage {
/******Newly added code****************/
static Composite composite=null;
public static void main(String[] args) {
IWizard mypage= new MyWizardPage();
WizardDialog dialog = new WizardDialog(composite, mypage);
dialog.open();
}
/******Newly added code****************/
public MyWizardPage() {
super("My Wizard");
}
public void createControl(Composite parent) {
// Create the parent control
composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
// Create some controls
new Label(composite, SWT.LEFT).setText("Field #1:");
Text field1 = new Text(composite, SWT.BORDER | SWT.SINGLE);
field1.setLayoutData(new GridData(GridData.FILL_BOTH));
new Label(composite, SWT.LEFT).setText("Field #2:");
Text field2 = new Text(composite, SWT.BORDER | SWT.SINGLE);
field2.setLayoutData(new GridData(GridData.FILL_BOTH));
// Important!
setControl(composite);
}
}
I have modified the code as above:
But I get the following error: MyWizard Page is not of IWizard Type. I did this because to instantiate a WizardDialogue object it requires IWizard Type.I tried to implement IWizard interafce for MyWizardPageClass but it requires implementation for 18 methods. I am not sure how to proceed.
Upvotes: 0
Views: 51
Reputation: 2352
Please go though below code for wizard and it pages: See TestWizard main method which opens the wizard
Page One :
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class PageOne extends WizardPage {
private Text text1;
private Composite container;
public PageOne() {
super("First Page");
setTitle("First Page");
setDescription("Fake Wizard: First page");
}
@Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 2;
Label label1 = new Label(container, SWT.NONE);
label1.setText("Put a value here.");
text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
text1.setText("");
text1.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (!text1.getText().isEmpty()) {
setPageComplete(true);
}
}
});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
text1.setLayoutData(gd);
setControl(container);
setPageComplete(false);
}
public String getText1() {
return text1.getText();
}
}
Page Two :
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class PageTwo extends WizardPage {
private Text text1;
private Composite container;
public PageTwo() {
super("Second Page");
setTitle("Second Page");
setDescription("Fake Wizard: Second page");
}
@Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 2;
Label label1 = new Label(container, SWT.NONE);
label1.setText("Put a value here.");
text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
text1.setText("");
text1.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (!text1.getText().isEmpty()) {
setPageComplete(true);
}
}
});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
text1.setLayoutData(gd);
setControl(container);
setPageComplete(false);
}
public String getText1() {
return text1.getText();
}
}
Demo Wizard :
import org.eclipse.jface.wizard.Wizard;
public class DemoWizard extends Wizard {
protected PageOne one;
protected PageTwo two;
public DemoWizard() {
super();
setNeedsProgressMonitor(true);
}
@Override
public String getWindowTitle() {
return "Export My Data";
}
@Override
public void addPages() {
one = new PageOne();
two = new PageTwo();
addPage(one);
addPage(two);
}
@Override
public boolean performFinish() {
// Print the result to the console
System.out.println(one.getText1());
System.out.println(two.getText1());
return true;
}
}
Main test class :
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestWizard {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Open Wizard");
button.addListener(SWT.Selection, event -> {
WizardDialog wizardDialog = new WizardDialog(shell, new DemoWizard());
if (wizardDialog.open() == Window.OK) {
System.out.println("Ok pressed");
} else {
System.out.println("Cancel pressed");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Upvotes: 1