SilverRay SilverRay - 2 months ago 11
Java Question

Java open one instance of JInternalFrame only

I am creating an inventory system using java but I am having problem in displaying only one JInternalFrame in my application. I put a condition that will validate if the JInternalFrame is already visible or not and it's working but the problem is that the first clicked doesn't display anything only after the succeeding clicks. Here is my code for calling the JInternalFrame class:

private Planning pFrame;

private void firstWindow()
{

if(pFrame == null)
{
pFrame = new Planning();
Dimension desktopSize = desktop.getSize();
pFrame.setSize(desktopSize);
pFrame.moveToFront();
pFrame.setVisible(true);
desktop.add(pFrame);
try{
pFrame.setMaximum(true);
}catch(Exception e){}
System.out.println("Clicked");
}

if(pFrame.isVisible())
{
pFrame.setVisible(false);
}
else
{
pFrame.setVisible(true);
}
}

Answer

After code and try for several hours I found my answer and this is my code that works:

private void firstWindow()
{
    if(pFrame == null)
    {
        pFrame = new Planning();
        Dimension desktopSize = desktop.getSize();
        pFrame.setSize(desktopSize);
        desktop.add(pFrame);
        pFrame.setVisible(true);
        pFrame.moveToFront();
        try{
            pFrame.setMaximum(true);
            pFrame.setSelected(true);
        }catch(Exception e){}
    }
    else if(!pFrame.isVisible())
    {
        pFrame.setVisible(true);
        pFrame.moveToFront();
    }


    if(iFrame.isVisible())
    {
        desktop.remove(iFrame);
        iFrame = null;
    }
}

private void secondWindow()
{
    if(iFrame == null)
    {
        iFrame = new Inventory();
        Dimension desktopSize = desktop.getSize();
        iFrame.setSize(desktopSize);
        desktop.add(iFrame);
        iFrame.setVisible(true);
        iFrame.moveToFront();

        try{
            iFrame.setMaximum(true);
            iFrame.setSelected(true);
        }catch(Exception e){}
    }
    else if(!iFrame.isVisible())
    {
        iFrame.setVisible(true);
        iFrame.moveToFront();
    }

    if(pFrame.isVisible())
    {
        desktop.remove(pFrame);
        pFrame = null;
    }
}

Note: I also found a way to close the previous frame when you open other frame class..

And in my internal frame I put this code to make its visibility to false if user clicks the close button.

setDefaultCloseOperation(this.HIDE_ON_CLOSE);

Thanks to the people who helped...

Comments