简体   繁体   中英

Swing: move JPanel

I have 2 jpanels, one is used as a header for another. I want to hide panel with content and move header down when clicked on header (like minimizing). I am able to hide panel, but can't change the location of another one. Here is the code:

 private void dbStatsLabelMouseClicked(java.awt.event.MouseEvent evt)     {                                          
    if (report1Panel.isVisible()) {
        report1Panel.setVisible(false);
        Point p = reportHeaderPanel.getLocation();
        p.y = p.y + 100;
        reportHeaderPanel.setLocation(p);
    }
}

You can remove (then add if necessary) report1Panel instead of hiding it (then revalidate parent component) - the behaviour will depend on the layout now. If you provide "auto resizing", your "header" component should expand to the parent size.

Other solution would be setting size of report1Panel - horizontal or vertical - to zero. Here again - you should provide "auto resizing" functionality

And as @StanislavL mentioned you can use JSplitPane with event on header click:

splitPane.setDividerLocation(0); //or
splitPane.setDividerLocation(1.0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM