Issues with resizing Java Swing GridBagLayout panel

I am working on a simple little swinging app for a relative, but I'm having trouble implementing animations ...

Here is a description of my layout:

I have a main window (created by instantiating, packaging, and displaying a JFrame). I told the contents pane of the main window to use GridBagLayout as my layout. My grid is 2 grids wide and 3 grids high. In the first column, I have three JButtons (A, B and C), each of which occupies one row of vertical grid space and one column of horizontal grid space. In the second column, I have another JPanel that has a width of one column and three rows tall.

This second JPanel is also configured to use GridBagLayout as a layout. In this case, there are two columns and one row. The left column has one column wide, one row JPanel high with button 1 inside it. The right column consists of only one JButton (Button 2), which also has a width of one column and one row height.

Here is a screenshot of what I just described: Image of layout

Now that you understand what a layout is, let me explain what I was trying to do:

I am trying to use the Universal Tween Engine to resize the Jpanel that contains button 1. However, in my attempts, I end up with:

enter image description here

, JPanel, Button 1, ! , 2 , , , 2 JPanel, Button 1!

, JPanel, Button 1, GridBagLayout. -, .

, ... , "" GridBagLayout, , Jpanel, 1? GridBagLayout.invalidatelayout() GridBaglayout, JFrame.getContentPane().invalidate() . .

:

ImageManager.java:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import aurelienribon.tweenaccessors.swing.*;
import aurelienribon.utils.swing.*;
import aurelienribon.tweenengine.*;
import aurelienribon.tweenengine.equations.*;

public class ImageManager
{
    /**
     * @param args
     */

    public static JFrame mainwindow;
    public static TweenManager tweenManager;
    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() //For thread safety!
        {
            public void run()
            {
                InitGUI();
                InitTween();
            }
        });
    }


    private static void InitTween()
    {
        ((Window)mainwindow).addWindowListener(new WindowAdapter() {
            @Override public void windowOpened(WindowEvent e)
            {
                    new DrawingCanvas()
                    {
                            @Override protected void update(int elapsedMillis)
                            {
                                tweenManager.update(elapsedMillis);
                            }
                    }.start();
            }
        });
    }


    private static void InitGUI()
    {
        //Init the window and layout systems
        mainwindow = new JFrame("Imaffect");
        Container pane = mainwindow.getContentPane();
        pane.setLayout(new GridBagLayout());

            //Begin creating the UI!
            pane.add(new JButton("Button A"), new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 100, 0));
            pane.add(new JButton("Button B"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 100, 100));
            pane.add(new JButton("Button C"), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.PAGE_START, GridBagConstraints.VERTICAL, new Insets(0,0,0,0), 100, 20));

            pane.add(InitPreviewGUI(), new GridBagConstraints(1, 0, 1, 3, 1, 1, GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));


        //Finalize the window
        mainwindow.pack();
        mainwindow.setVisible(true);
    }


    private static JPanel InitPreviewGUI()
    {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(InitSettingsGUI(), new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
        panel.add(new JButton("Button 2"), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));


        return panel;
    }

    private static JPanel InitSettingsGUI()
    {
        JPanel panel = new JPanel();
            SetupSettingsTweens(panel);
            SetupSettingsContent(panel);
        return panel;
    }


    private static void SetupSettingsTweens(JPanel panel)
    {
        Tween.registerAccessor(Component.class, new ComponentAccessor());
        tweenManager = new TweenManager();
        Tween.to(panel, ComponentAccessor.WIDTH, 1000)
            .target(200)
            .ease(Cubic.INOUT)
            .repeatYoyo(-1, 200)
            .delay(500)
            .start(tweenManager);

    }

    private static void SetupSettingsContent(JPanel panel)
    {
        panel.add(new JButton("Button 1"));
    }
}

, :

, , Eclipse Juno runnable jar:

, , :

  • .
  • ( )
  • , SwingUtilities.invokeAndWait update()
  • update(), Container.setSize() JPanel, 1.

! Thankyou:)

P.S. , . , , .

+5
1

, update() Button1

container.revalidate();
container.repaint();

revalidate() repaint() .

setSize() Button1, Button2 repaint() ( revalidate(), ).

+3

All Articles