简体   繁体   中英

Expandable component to hide or unhide another component

I am wondering if there is a Swing-component which is able to be expanded, so that I am able to hide or unhide something like a menu.

As an example something similar can be found in MS Outlook:

在此输入图像描述

This is the default look, where all mail folders are unhidden. But clicking on the little arrow (circled red) hides that view:

在此输入图像描述

I would like to have something similar in my Java-GUI to do the same, while the included component should be hidden by default. I am not sure what component should be under that expandable "tab", but right now I am thinking about a JTree.


This is what I am generally trying. But if you want a bonus cooky, you could consider the requirement that this expandable menu has to expand in a flowing, smooth animation, instead of being hidden or unhidden instantly. The latter can be found in TeamViewer for example. There you have a menu bar on top, which can be hidden or unhidden, while it's going up and down in a smooth animation.

Example, TeamViewer:

在此输入图像描述


EDIT

First I tried the JSplitPane , but moving all existing components to fit the split pane schema was not a solution I would prefer. Instead I was looking for something more independent.

The next thing I tried was using Swing Timer to expand the width of the JFrame using its setBounds -method. It works exactly the way I want when it comes to toggling additional space for a menu. The JFrame gets bigger or smaller while the resizing process is animated. But I can see two disadvantages of this approach:

  • The animation is kind of slow and not perfectly smooth. I removed the delay. It is quite OK so far, but a more smoother solution is preferred here. But I can totally live with it how it is currently.
  • A big disadvantage is that the increasing of the size leaves black spaces between the old and the new width for half of a second. If anyone knows how to avoid that, I would have my perfect solution to this problem.

To make it clearer what I mean with "black spaces", see:

在此输入图像描述

Now you can see that black area. Like I said, it only remains for half of a second or even less. With Swing Timer I added 100 pixels to the width of the JFrame. The higher the value I add to the width, the higher the black area. If the JFrame 's width is completely resized, everything is in the correct color again.

So does anyone know why this happens? Is this hardware related or is it just simply a standard behavior of Java or Swing? Does anyone know solutions or workarounds for this?

See splitpane .

For example

JSplitPane mainSplitPanel = new JSplitPane();
mainSplitPanel.setDividerLocation(650);
mainSplitPanel.setOneTouchExpandable(true);

For samples click here

The solution which fit the best for me can be found in the edited part of my question. I found a good combination of delay time and frame resizing which appeared smooth enough (1 millesecond delay and increasing the width with 45 pixels). The issue with the black frame is not problematical anymore. Now the black screen is even shorter in its duration, and if the user waits around 2 seconds, the black area won't be displayed (visibly) at all. In that case it's OK for me, because the user should spend some seconds after expanding anyways.

For everyone who wants to know more about this black area while resizing JFrames, see here .

The code of the solution I described in my edited question:

    final Timer timer = new Timer(1, null);
    timer.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt)
        {

            double width = myFrame.getBounds().getWidth();

            if(isExpanded == false)
                width += FRAME_PIXEL_CHANGE;
            else
                width -= FRAME_PIXEL_CHANGE;



            if(myFrame.getBounds().getWidth() >= FRAME_SIZE_EXPANDED && isExpanded == false)
            {
                myFrame.setBounds(FRAME_X, FRAME_Y,  FRAME_SIZE_EXPANDED, FRAME_HEIGTH);
                btnExpand.setIcon(new ImageIcon(GUI.class.getResource("/img/close.png")));
                timer.stop();
                isExpanded = true;
            }
            else if(myFrame.getBounds().getWidth() <= FRAME_SIZE_REGULAR && isExpanded == true)
            {
                myFrame.setBounds(FRAME_X, FRAME_Y,  FRAME_SIZE_REGULAR, FRAME_HEIGTH);
                btnExpand.setIcon(new ImageIcon(GUIMain.class.getResource("/img/expand.png")));
                timer.stop();
                isExpanded = false;
            }
            else
            {
                myFrame.setBounds(FRAME_X, FRAME_Y, (int) width, (int) FRAME_HEIGTH);
                btnExpand.setBounds((int) (width-36), 246, 36, 36);
            }
        }
    });

    return timer;

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