-2

I have a frame (main). There are two buttons: Items and Sale.

When I click button Items it opens a frame (Items) and I want to, when I click on button Sale, it should close the Items and open Sale.

This is Items frame:

public class Items extends JFrame {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Items frame = new Items();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}

UPDATE :- here is my sale class

public class Sale extends JFrame {
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Sale frame = new Sale();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}
Manjit saha
  • 1
  • 1
  • 7
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) It's likely the 2nd 'frame' should be a dialog. See the accepted answer for alternatives. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 18 '18 at 05:52
  • Why do you even need to close and open a new frame.why dont you try a card layout with panels for Sale and for the other stuff. When you click the Sale, call the card layouts next() method. You can even go back to other panel when you need. That makes your life easy. Closing and opening a frame is time taking and not good. –  Sep 18 '18 at 05:57
  • Looks like a JTabbedPane is what you need. Your two buttons are replaced with the tab 'headers' and the effect of closing (/disposing) and opening frames is achieved just as the user clicks those headers. – Erwin Smout Sep 18 '18 at 06:39

2 Answers2

0

Just close the previous Item JFrame with dispose() method.

salesframe.setVisible(true);
itemframe.dispose();

In your case, I think you should also add an ActionListener to the button.

jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
       frameToClose.dispose();
    }
});
RogerSK
  • 393
  • 1
  • 18
  • this is not working – Manjit saha Sep 18 '18 at 05:53
  • literally i have three frames... one frame which have two buttons. and the two frames which can be opened using these buttons... and i want is to the frame which have buttons should also be visible while i open a new frame by clicking a button. and then when i click on sale button it should close item frame and open sale frame – Manjit saha Sep 18 '18 at 05:57
  • Can you provide some details about the error? The dispose() supposed work with JFrame closing. – RogerSK Sep 18 '18 at 05:57
  • there are no error in console .. it just don't close the frame – Manjit saha Sep 18 '18 at 05:58
  • @Manjitsaha Did you add an ActionListener for the button? – RogerSK Sep 18 '18 at 06:00
  • 1
    Make those three frames as panels and add them to a frame as card layout. Now all you need to do is call next() or previous() method of card layout based on what button is clicked. It would be simpler than closing and opening frames. –  Sep 18 '18 at 06:06
  • I have 8 button on the main frame... All opens different frame.. like.. Add items , add customer etc. Doesn't all of their methods in main frame will look mess – Manjit saha Sep 18 '18 at 07:22
  • @RogerT yes.. i tried everything.. Like... I tried 2-3 ways to dispose but... Not working – Manjit saha Sep 18 '18 at 07:24
  • @Manjitsaha Please do attach the code that you did for the button such as ActionListener or any onclick event. – RogerSK Sep 18 '18 at 10:02
0

You should .dispose(); the frame in the ActionListener of the button and since you are extending JFrame in you class it means you .dispose(); the instance itself. Something like this:

public class Main extends JFrame {
    public Main() {

        Sale sale = new Sale();
        Items item = new Items();

        JButton btnSale = new JButton("Sale");
        getContentPane().add(btnSale, BorderLayout.WEST);

        JButton btnItems = new JButton("Items");
        getContentPane().add(btnItems, BorderLayout.CENTER);
        btnSale.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                sale.setVisible(true);
                if(item.isVisible()) {
                    item.dispose();
                }

            }
        });

        btnItems.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                item.setVisible(true);
                if(sale.isVisible()) {
                    sale.dispose();
                }

            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
Zozinski
  • 73
  • 8
  • when i type sale in main frame , it does not give suggestion of dispose(); there is options of getFrames(), getWindows(). etc. – Manjit saha Sep 18 '18 at 13:00
  • and even typing manually produce error. but when i click to see what error is producing . it says no suggestion is available – Manjit saha Sep 18 '18 at 13:02
  • @Manjit saha. Well does your Sale class extend JFrame? If not you can call the frame of the class, I don't know what you have named it but something like sale.frame.dispose(); . Also make sure that you make the frame in the Sale class "public", so it can be used in your Main class – Zozinski Sep 18 '18 at 13:53
  • @Manjitsaha Also in the if() condition should then be if(sale.frame.isVisible){}. If the classes you are trying to call are not extending JFrame – Zozinski Sep 18 '18 at 15:10
  • i have extending all class to JFrame. – Manjit saha Sep 18 '18 at 15:38
  • @Manjitsaha Ok, because the code i sent in the answer works fine on my machine, maybe you should send your whole code, so we could trully help you and see where the problem is. What is Invoice();? – Zozinski Sep 19 '18 at 02:21
  • Invoice. that is i think mistakely pasted i corrected it – Manjit saha Sep 19 '18 at 02:47
  • and the main class contain 561 lines how can i paste it here – Manjit saha Sep 19 '18 at 02:47
  • when i type Items then dot(.) it does not give any suggestion – Manjit saha Sep 19 '18 at 02:49
  • even when i type Items.dispose(); it cause an error – Manjit saha Sep 19 '18 at 02:49
  • @Manjitsaha I assume this is because you are trying to call the dospose() in the main method. Look at the code I sent. The listeners and the disposing of frames are in the class constructor e.g. outside the public static void main method – Zozinski Sep 19 '18 at 13:18