sajad
sajad

Reputation: 2174

how to find out a java Component being displayed on screen

I want to find out if a JPanel is on the screen or not. It doesn't mean that isVisible() method could be used for this situation. I mean I want to find out whether a component that has been initiated before, presently is one of components on my main panel or not.

Edit and more explanation: I have several panels initiated before in my program and use them on my form as needed. I want to know for example jpanel1 in now on any of panels that now are present on my form.

Example:

public class GUI extends JFrame() {

    private JPanel1, jPanel2;

    public static void main(String[] args) {
        GUI gui = new GUI();
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        gui.setContentpane(jPanel1);
        gui.setVisible(true);
    }

}

now jPanel1 is visible on screen bu jPanel2 is not visible. How can I find out this?

Upvotes: 12

Views: 17900

Answers (6)

Tech of the Absence
Tech of the Absence

Reputation: 47

Note, that if the component is out of bounds of the JFrame, you won't see it, but each of these methods returns true. To find out whether the component is really visible, use (jframe.getBounds().contains(jPanel.getBounds())&&jPanel.isShowing())

Upvotes: 0

Juan C Nuno
Juan C Nuno

Reputation: 553

Call Component::isShowing. Even that comes with a couple of caveats.

Upvotes: 0

Akshay Prabhu
Akshay Prabhu

Reputation: 305

jPanel1.isVisible()==true jPanel1.isVisible()==false

for panel jPanel1.isShowing() also works

Upvotes: 5

sajad
sajad

Reputation: 2174

After investigation I find out this method represents that the component is displayed on screen or not:

isDisplayable()

in my Example:

jPanel1.isDisplayable() // returns true

jPanel2.isDisplayable() // returns false

as Simple as this!

Upvotes: 16

Juvanis
Juvanis

Reputation: 25950

Write your own panel class that extends JPanel. Add a new method to this class named isOnTheScreen() which return a boolean indicating whether the panel is added to the window or not.

public class MyPanel extends JPanel
{
    boolean isAdded = false;

    public boolean isOnTheScreen()
    {
       return isAdded;
    }

    public void setOnTheScreen(boolean isAdded)
    {
       this.isAdded = isAdded;
    }
}

After creating your own panel objects, use the methods above to learn whether a panel is added to main panel/frame or not. Suppose you have added a panel to a frame:

JFrame frame = new JFrame()
MyPanel panel = new MyPanel();
frame.getContentPane().add(panel);
panel.setOnTheScreen(true);

As soon as you add it to the main screen, in this case a frame, call setOnTheScreen(true) And similarly call setOnTheScreen(false) when you remove the panel.

After this design you can determine whether a panel is added to the main window or not by just invoking isOnTheScreen() anywhere else in your code. I hope this design helps you.

Upvotes: 0

raveturned
raveturned

Reputation: 2677

If you're looking for children of a main panel, you could call getComponents() on the main panel to return an array of its Components, then iterate through them to check if any of them are the Panel you are looking for. You may need to call this recursively if the panel is not a direct child of the main panel.

Upvotes: 0

Related Questions