selena
selena

Reputation: 151

How do open a internal frame only once

The following code opens a JInternalFrame when a button is clicked. But I want this window to be opened once, so if the user clicks that button again it will not open another frame instead it would bring to the front the window whether it is iconified, behind another window, etc. I have tried a couple of ways mainly using a counter, but the problems is once the frame is closed it doesn't open it again either. Is there another easy way to do this, cause I am not able to make it work properly. Thanks in advance.

Below is the code I am working on:

  public class About implements ActionListener{

private int openFrameCount;
private JDesktopPane desk;
private JTextArea Tarea;
private JScrollPane scroll;
private BufferedReader in ;
int count =0;
MyInternalFrame frame;

public About(JDesktopPane desktop) {
    // TODO Auto-generated constructor stub
    desk = desktop;
    System.out.println(count);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    count += 1;

    if(count == 1){
    frame = new MyInternalFrame("SAD Imaging");
    count +=1;
    try {
        in = new BufferedReader(new FileReader("SADInfo.txt"));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    String line;
    String file = ""; 
    try {
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
            file += line;
            file +="\n";

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Tarea = new JTextArea();
    //System.out.println(file);

    Tarea.setText(file);
    Font f = new Font("TimesNewRoman", Font.ROMAN_BASELINE, 16);
    Tarea.setFont(f);
    Tarea.setBackground(Color.white);
    Tarea.setAlignmentX(SwingConstants.CENTER);
    Tarea.setEditable(false);

    JPanel panel = new JPanel();
    panel.add(Tarea);
    panel.setBackground(Color.white);

     //scroll = new JScrollPane(Tarea);
     scroll = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    frame.add(scroll);
    frame.setVisible(true);
    desk.add(frame);

     try {
            frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {

    }

    }
    else if(count > 1){
        try {
            //frame.setIcon(true);
            frame.setMaximum(true);
            frame.toFront();
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 1677

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

Basically, you just need to check to see if the frame is null or not. If it is, you create an instance, if it's not, you bring it to the front, for example

@Override
public void actionPerformed(ActionEvent arg0) {
    if (frame == null || (frame.getParent() == null && !frame.isIconifiable())) {
        // Your exitsing code
    } else {
        frame.setIcon(false);
        frame.setSelected(true);
        frame.moveToFront();
    }

You can also use an InteralFrameListener to the frame so you can detect when the frame is closed, so you null the internal reference, for example...

frame.addInternalFrameListener(new InternalFrameAdapter() {
    @Override
    public void internalFrameClosing(InternalFrameEvent e) {
        frame = null;
    }
});

Updated with runnable example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestInternalFrame {

    public static void main(String[] args) {
        new TestInternalFrame();
    }

    private JInternalFrame imageFrame;
    private JDesktopPane desktop;

    public TestInternalFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JMenu fileMenu = new JMenu("File");
                JMenuItem newMenu = fileMenu.add("Show...");
                newMenu.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (imageFrame == null || imageFrame.isClosed()) {
                            imageFrame = new JInternalFrame("Image");
                            imageFrame.setIconifiable(true);
                            imageFrame.setMaximizable(true);
                            imageFrame.setClosable(true);
                            imageFrame.setResizable(true);
                            JLabel label = new JLabel(new ImageIcon("..."));
                            imageFrame.add(label);
                            imageFrame.pack();

                            desktop.add(imageFrame);


                            imageFrame.setLocation(0, 0);
                            imageFrame.setVisible(true);
                        }

                        try {
                            imageFrame.setIcon(false);
                            imageFrame.setSelected(true);
                        } catch (PropertyVetoException ex) {
                            ex.printStackTrace();
                        }
                        imageFrame.moveToFront();
                    }
                });

                desktop = new JDesktopPane();

                JMenuBar mb = new JMenuBar();
                mb.add(fileMenu);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setJMenuBar(mb);
                frame.add(desktop);
                frame.setSize(1200, 900);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 2

Related Questions