1

I am using Eclipse and created an SWT application. I have two windows:

  1. My first form (MainForm) is SWT application window
  2. Second form (Form2) is a JFrame.

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.

skomisa
  • 16,436
  • 7
  • 61
  • 102
  • 2
    1. To close a `JFrame` 1 from another you'll have to path a reference of `JFrame` 1 to it for example by `Form2 f = new Form2(5, this);`. However the whole concept is probably wrong. See the answer of Andrew Thompson to [The Use of Multiple JFrames: Good or Bad Practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice). 2. Use `javax.swing.JButton` rather than `java.awt.Button` 3. For more help post [mcve] – c0der Apr 04 '19 at 05:14
  • 2
    Mixing SWT and Swing is hard and best avoided. – greg-449 Apr 04 '19 at 06:24

0 Answers0