I am using Eclipse and created an SWT application. I have two windows:
- My first form (
MainForm
) is SWT application window - Second form (
Form2
) is aJFrame
.
MainForm
has a button (b1) and Form2
has a button (b2).
When b1 is clicked I create an object of Form2
and setVisible()
to true so that I can see the Form2
. I also hide the MainForm
during this process by shell.setVisible(false);
Now, I want to open the MainForm
after clicking b2 at Form2
, but I am not finding anyway to do it. Can you help me how can I do that?
Piece of code from MainForm
:
public class MainForm {
protected Shell shell;
private Text text;
private Text text_1;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
MainForm window = new MainForm();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button button1 = = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Form2 f = new Form2(5);
f.setVisible(true);
shell.setVisible(false);
}
});
}}
Form2
code snippet:
public class Form2 extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public Form2( int a) {
JOptionPane.showConfirmDialog(null, a);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 834, 560);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//I need to know what should I write here to display MainForm. I tried with the following but does not work.
//MainForm f = new MainForm();
//MainForm.this.shell.setVisible(true);
//f.shell.setVisible(true);
}
});
btnBack.setBounds(264, 46, 188, 80);
contentPane.add(btnBack);
}
}
Please see the commented portion of Form2
. I would be really grateful if you can give me an idea what should be written in the commented part of Form2
so that I can display MainForm
from there.