0

I have question for you: how to open new windows in JFrame using JMenu items and close the previous at the same time. Please do not send me to JFrame documentation because it will not help me, better show me e.g code if you may. What I have now? I make a new functions in my application to call them and that way open new window. Code look like this :

Here I add Listener for a JMenu item. What should be clear for you.

  JMenuItem wyswietlbaza = new JMenuItem("Wyswietl baze");
        akcja.add(wyswietlbaza);
        wyswietlbaza.addActionListener(this);

// Here I call a method to open the window and close another.

public void actionPerformed(ActionEvent e){  

    new BazaDanych().setVisible(true);  
    this.dispose();
    }

This way work only if i have one window(some main and another e.g window2). When I have 3 windows my code stop work properly even when I make another function like below:

 public void actionPerformed1(ActionEvent e){  

        new ZmienBaza().setVisible(true);  
        this.dispose();
      } 

My question : how to add another windows and Listeners for them? I just want to make few windows which should be open by JMenu item and close at the same time(first close second open)but now my Listener open only second window even when I push JMenu item "Open window1". Do you have any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Lukas
  • 69
  • 1
  • 10
  • 1) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Dec 14 '13 at 20:11

1 Answers1

0

You should add a different ActionListener to each JMenuItem.

Like:

wyswietlbaza.addActionListener(new ActionListener() {

    // If the JMenuItem "Wyswietl baze" is clicked, this action will happen
    @Override
    public void actionPerformed(ActionEvent e) {
        new BazaDanych().setVisible(true);  
        YourClassName.this.dispose();
    }

};
user3100783
  • 259
  • 1
  • 8
  • Ok I will try this thank you. Maybe you misunderstood me. I want to close window when I am opening another like I wrote. My code work but only for two windows(main and another). When I add third window, JMenu item open the third one even when I click "Open second". I think something is messed with this (word). Ok I pasted this on my code and it works the same way as my did. When I push open window2 it open window3. – Lukas Dec 14 '13 at 20:43
  • Ok I think I understand your problem now. How many `JMenuItem`s do you have? Is each one supposed to open a different window? Is there only one, and it opens a different window each time? How do you want it to work? – user3100783 Dec 14 '13 at 21:11
  • I have five menu items. In my another code every single item open another window(but do not close previous) in this I want do the same + close the previous window. I want to work it that way:JMenu items open windows(item always open the same window e.g button1 open window1, button 2 open window2 etc.).When I am opening another I want to close this one when I am actually. – Lukas Dec 14 '13 at 21:18
  • Ok you need to add a different `ActionListener` to each `JMenuItem` like in my updated answer. – user3100783 Dec 14 '13 at 21:28
  • Ok thank you I will try this. Thank you very much that works. I am really glad. I lost 3 days searching the answer.Really thank you. – Lukas Dec 14 '13 at 21:36