Storm
Storm

Reputation: 23

How to call prevous frame from new frame, how to set visible again?

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 !

Upvotes: 0

Views: 388

Answers (2)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

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);
    }
}

Upvotes: 1

Donatic
Donatic

Reputation: 323

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));

Upvotes: 0

Related Questions