Farrukh Zeb
Farrukh Zeb

Reputation: 21

Multiple JFrames are opened when executed

I have a task of implementing MPJ-Express in Java Swing, unfortunately It isn't going well. I keep getting multiple JFrames when I execute the code. I have used OOP concepts. For now I'm just using a simple JFrame to show a Button and when I click the button it should give me the Rank and the Size of the Processes.

I have tried everything but all in vane going from sequential programming to OOP. I did try one thing is when the rank is 0 It shows 1 JFrame but when I click the button I get only the rank 0 every time, I want all the ranks shown.

public class Main {

    public static void main(String[] args) {
        Gui gui = new Gui(args);
    }
}

public class Process extends mpi.MPI {

    public final int rank;
    public final int size;

    public Process(String[] args){
        super.Init(args);
        this.rank = COMM_WORLD.Rank();
        this.size = COMM_WORLD.Size();
    }

    public void exec() {
        System.out.println("Rank <" + rank + ">");
        System.out.println("Size <" + size + ">");
    }

    public void processFinalize() throws Throwable {
        super.finalize();
    }
}

class Gui {

    Process p;

    public Gui(String[] args) {
        p = new Process(args);
        welcomeScreen();
    }

    private void welcomeScreen() {
        // Create and set up a frame window
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("MPJ Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Set the panel to add buttons
        JPanel panel = new JPanel();

        BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.X_AXIS);
        panel.setLayout(boxlayout);

        // Set border for the panel
        panel.setBorder(new EmptyBorder(new Insets(150, 200, 150, 200)));

        JButton jb1 = new JButton("Button 1");

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

                System.out.println("Rank <" + p.rank + ">");
                System.out.println("Size <" + p.size + ">");

            }

        });
        panel.add(jb1);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

Let the number of processes be 3
Rank:0
Size:3
Rank:2
Size:3
Rank:1
Size:3
Not:
Rank:0
Size:3

I want all the ranks to be shown.

Upvotes: 0

Views: 36

Answers (0)

Related Questions