Why JFrame doensn't work properly when opens from another JFrame's button?

Viewed 50

I created a menu in a JFrame. When a user click on each of them, it creates an Object from another class and in that class we have a JFrame too. But the new JFrame doesn't work properly. For example, although I set setDefaultCloseOperation on EXIT_ON_CLOSE it doesn't close when I click close button. Or it doesn't load images. I don't know where is the problem! Here is part of my code in first class (channel and plan are objects from another classes that I use to fill menu items):

//create a frame
    JFrame frame = new JFrame("Menu");
    //create a menu
    JMenuBar menu = new JMenuBar();
    //set menu layout
    menu.setLayout(new GridLayout(0,1));

    //create menu items
    for (int i = 0; i < channelList.size; i++) {
        if (channelList.channels[i] != null){
            Channel channel = channelList.channels[i];
            JMenu file = new JMenu(channel.getName());
            menu.add(file);

            for (int j = 0; j < channel.getSize(); j++) {
                if (channel.getPlans()[i] != null){
                    Plan plan = channel.getPlans()[j];
                    //create the submenu
                    JMenuItem plano = new JMenuItem(new AbstractAction("My Menu Item") {
                        public void actionPerformed(ActionEvent e) {
                            if (plan.isShowing()){
                                frame.dispose();
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException ex) {
                                    ex.printStackTrace();
                                }
                                ImageReceiver receiver = new ImageReceiver(plan);
                            }
                            else {
                                JOptionPane.showMessageDialog(null, "This plan is not showing now!",
                                        "Hey!", JOptionPane.ERROR_MESSAGE);
                            }

                        }
                    });
                    plano.setText(plan.getName() + ": " + plan.getStart() + " - " + plan.getEnd());
                    file.add(plano);
                }
            }
        }
    }

    //add menu to frame
    frame.setJMenuBar(menu);
    frame.setSize(300, 250);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

and here is part of my code in second class:

    JLabel labelImage = new JLabel();
    JLabel windowImage = new JLabel();

    JFrame frame = new JFrame("Multicast Image Receiver");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(labelImage);
    frame.setSize(300, 10);
    frame.setVisible(true);
    frame.addKeyListener(this);
    BufferedImage image = ImageIO.read(new File("./images/1.jpg"));
                
    labelImage.setIcon(new ImageIcon(image));
    windowImage.setIcon(new ImageIcon(image));

    frame.pack();
0 Answers
Related