1

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:

enter image description here

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

enter image description here

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:

enter image description here


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:

enter image description here

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?

KJaeg
  • 698
  • 3
  • 7
  • 23

2 Answers2

3

See splitpane.

For example

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

For samples click here

Tamil
  • 1,193
  • 9
  • 24
  • Using a JSplitPane is not really the solution I am looking for. It requires too much work to set it up the way I want it to be. What I was looking for was a component, which is getting the required space by itself by expanding it. The JSplitPane more or less takes the set space and fills either the left or the right panel side within the defined space. Additionally I have a full GUI, which requires now a moving of its content to the left panel side. But I will still work on it, sooner or later I will get it the way I want it. – KJaeg Jul 29 '16 at 12:46
0

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;
Community
  • 1
  • 1
KJaeg
  • 698
  • 3
  • 7
  • 23