Dynamic GUI frame

Can a Swing JFrame or JPanel be resized dynamically?

I mean like the new "Apple Software Update" GUI (think it's for iTunes), it grows and shrinks on the fly.

But not only does it change vertical size, while doing this it also hides the components and then later make them reappear after another auto change.

I've been trying setsize and setvisible, but they haven't been working too good.

[417 byte] By [abijaha] at [2007-10-3 9:23:25]
# 1
Try something like:frame.setSize(...);frame.getContentPane().validate(); or frame.pack();
camickra at 2007-7-15 4:37:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

my messy code look like this, i hope you get the idea.

However, the bottom component keeps intruding on the one above it

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

//--

import javax.swing.JToolBar;

public class MoveWindow extends JWindow

implements MouseListener, MouseMotionListener, ActionListener {

Point location;

MouseEvent pressed;

public void mousePressed(MouseEvent me){

pressed = me;

}

public void mouseClicked(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseDragged(MouseEvent me){

location = getLocation(location);

int x = location.x - pressed.getX() + me.getX();

int y = location.y - pressed.getY() + me.getY();

setLocation(x, y);

}

public void mouseMoved(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

boolean down = false;

public void actionPerformed(ActionEvent e){

String strSource = e.getActionCommand();

if(strSource.equals("CMD")){

if(down){

window.setSize(300, 300);

getContentPane().add(toolBar, BorderLayout.SOUTH);

toolBar.setVisible(true);

this.getContentPane().validate();

down = !down;

}

else{

toolBar.setVisible(false);

//window.setSize(300, 50);

//toolBar.setVisible(true);

getContentPane().validate();

down = !down;

}

}

}

public MoveWindow()

{

JPanel panel = new JPanel();

panel.setBackground(Color.RED);

//panel.setPreferredSize( new Dimension(200, 50) );

getContentPane().add(panel, BorderLayout.NORTH);

JButton button = new JButton("one");

JPanel buttonPanel = new JPanel();

buttonPanel.setPreferredSize( new Dimension(300, 50) );

buttonPanel.add(button);

getContentPane().add(buttonPanel, BorderLayout.CENTER);

button.addActionListener(this);

button.setActionCommand("CMD");

toolBar = new JToolBar("App Control");

JButton opp = new JButton();

JButton prf = new JButton();

toolBar.add(prf);

toolBar.add(opp);

//toolBar.setVisible(false);

addMouseListener( this );

addMouseMotionListener( this );

}

public static void main(String args[]){

window = new MoveWindow();

window.setSize(300, 50);

window.setLocationRelativeTo( null );

window.setVisible(true);

}

private static MoveWindow window;

JToolBar toolBar;

}

abijaha at 2007-7-15 4:37:13 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hmm, not sure why, but it looks the the validate() method needs to be invoked on the window, not the contentPane.Although a better solution is to use the pack() method, then the window will be resized to fit the components exactly.
camickra at 2007-7-15 4:37:13 > top of Java-index,Desktop,Core GUI APIs...