1

I'm trying to create a Java window (with a JFrame) that is able to resize itself when the user clicks a button in order to display some extra components (JLabel and JButton components) in a JPanel. These extra stuff panel is hidden when the window shows up. The window is located on the right border of the screen (Windows desktop), so it must grow to the left when the hidden panel becomes visible.

+---------------------------------------------------------------------------------+
|                                               +-------------------------------+ |
|                                               |                               | |
|            BEFORE:                            |             600px             | |
|                                               |                               | |
|                                               +-------------------------------+ |
|                                                                                 |
|                                                                                 |
|                                         <---                                    |
|                                       +-------+-------------------------------+ |
|                                       |       |                               | |
|            AFTER:                     | 150px |             600px             | |
|                                       |       |                               | |
|                                       +-------+-------------------------------+ |
|                                                                                 |
|                                                                                 |
|                                                                                 |
|                                                                                 |
+---------------------------------------------------------------------------------+

I've implemented a first version by using a vertical JSplitPane: the main window is on the right side and the extra panel is on the left side. When the user clicks the button, the system:

  1. sets the visibility of the extra panel to true.
  2. resizes the JFrame to width = 750px (its original width + the width of the extra panel).
  3. moves the window 150 px to the left because resizing is done to the right.

This algorithm works, but I have a very unpleasant "flash" effect during resizing/moving.

Is there a way to make this resizing/moving step smooth? Or, preferably, is there some swing or third part component that implements this kind of thing (a hidden panel that shows up and resizes the window smoothly to the left and without moving it)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Munniz
  • 11
  • 2
  • 1
    The programmer should not be guessing how big a GUI needs to be. Use `pack()` on the top level container for the necessary size as calculated by the JRE for that GUI, in that PLAF, on that OS at this moment. – Andrew Thompson Oct 09 '17 at 15:32
  • Thank you for your answer. I tried that, the width of my extra panel increases to the right and reduces the width of the main panel. Maybe I should try without a JSplitPane ? And even if it works, I think that the JFrame will always be resized to the right. – Munniz Oct 09 '17 at 16:53

0 Answers0