0

I have Meni JFrame which i set invisible, when i call other frame, with button click eg. Reports JFrame, and now how to call, from Reports frame, again same frame Meni which i had before, and not new one.

My code for now is something like this:

Meni

JButton btnReports = new JButton("   Reports");
    btnReports.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            Reports r = new Reports();
            reports.setVisible(true);
            setVisible(false); //setting current JFrame invisible 

        }
    });

Now i want to return to same Meni frame which i earlier set invisible, and not to create a new one, how to do that, ofc if its possible ?

Reports

 JButton btnMeni = new JButton("   Meni");
    btnMeni.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //if i do the same like in Meni it will call new window, and 
            //i don't want that i simply want to setVisible Meni window again

            dispose();

        }
    });

Thanks !

Storm
  • 23
  • 5
  • 2
    As i understand you have different JFrames? Maybe have a look here first https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice – pL4Gu33 Aug 25 '18 at 13:12
  • Possible duplicate of [How to close a JFrame and open it consequently, without exit the JVM/Application?](https://stackoverflow.com/questions/11354473/how-to-close-a-jframe-and-open-it-consequently-without-exit-the-jvm-application) – DontKnowMuchBut Getting Better Aug 25 '18 at 13:13
  • You're asking essentially, "how can I call a method (here `.setVisible(true)`) on another object, and the key is to have a reference to that object where it is needed. How you do this depends on how your code is structured. But having said this, yes, as @pL4Gu33 points out, your user will likely be very annoyed to have JFrames flung at them in this way. Don't do this but instead use a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – DontKnowMuchBut Getting Better Aug 25 '18 at 13:15

2 Answers2

1

You should not use 2 Frames here. Use CardLayout instead. Here is a small example how you can place two sceens into single JFrame

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;

public class ScreenUI {

    private static final String FIRST_SCREEN = "first";

    private static final String SECOND_SCREEN = "second";

    public static void main(String[] args) {
        JFrame frame = new JFrame("Multiscreen");
        CardLayout cl = new CardLayout();
        // set card layout
        frame.getContentPane().setLayout(cl);
        // build first screen
        JPanel firstScreen = new JPanel(new BorderLayout());
        firstScreen.add(new JScrollPane(new JList<>(new String[] {"One", "Two", "Three", "Four"})));
        JButton nextScreen = new JButton("Next");
        // action to show second screen, which is referenced per string constant
        nextScreen.addActionListener(e -> cl.show(frame.getContentPane(), SECOND_SCREEN)); 
        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonsPanel.add(nextScreen);
        firstScreen.add(buttonsPanel, BorderLayout.SOUTH);
        frame.getContentPane().add(firstScreen, FIRST_SCREEN);
        // build second screen
        JPanel secondScreen = new JPanel(new BorderLayout());
        secondScreen.add(new JScrollPane(new JTree()));
        JButton previousScreen = new JButton("Previous");
        // action to show first screen, which is referenced per string constant
        previousScreen.addActionListener(e -> cl.show(frame.getContentPane(), FIRST_SCREEN));
        buttonsPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonsPanel.add(previousScreen);
        secondScreen.add(buttonsPanel, BorderLayout.SOUTH);
        frame.getContentPane().add(secondScreen, SECOND_SCREEN);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
0

You could add a listener to Your "main" frame, that makes your frame visible again, when the report frame is closed:

   r.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent e)
        {
            setVisible(true);

        }
    });

Java doc for WindowListener: https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html#windowClosing(java.awt.event.WindowEvent)

That, on your report frame, just close the window in the OnButtonClick methode with

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
Donatic
  • 323
  • 1
  • 13