Resizing JPanel

using Timer object..

Page 1 of 1

11 Replies - 10390 Views - Last Post: 03 April 2010 - 04:20 PM Rate Topic: -----

#1 rhaktkdl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-February 10

Resizing JPanel

Posted 01 April 2010 - 01:21 PM

Hi, I am trying to make a JFrame that contains JPanel which shows a ball bounce around the JPanel.

I succeeded to make it show up and bounce around,

but the problem is... I want to resize JPanel when I change the size of component's window.

Actually, I use Timer object to make the event handler occur. So, if I change the size of window, then timer checks it at every 10milliseconds and occur the event handler. I succeeded to set Timer object and apply it into JPanel, but I only could not resize the JPanel.

here is a part of my codes.

private class ResizingListener implements ActionListener
   {
	   public void actionPerformed (ActionEvent event)
	   {
		   wdwWidth = getWidth();
		   wdwHeight = getHeight();
		   
		 
		   j.setText("Width is " + wdwWidth);
		   j.setText("Height is " + wdwHeight);
		   setSize(new Dimension(wdwWidth, wdwHeight));
		   
	   }
   }


I cannot upload my whole codes because it is class assignment...

so, just please give me an idea (method, or any kind of codes) how to change/resize JPanel with Timer object.

Is This A Good Question/Topic? 0
  • +

Replies To: Resizing JPanel

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Resizing JPanel

Posted 01 April 2010 - 01:33 PM

You would need to call the repaint method for the JPanel, so you need to make a paint method.

In the paint method:
public void paint(Graphics g)
{
    JPanel panel = new JPanel();
    MyFrameClass frame = new MyFrameClass():
    height = frame.getHeight();
    width = frame.getWidth();
    panel.setSize(height, width);
}


Then you would call the repaint method (which calls paint) in the actionPerformed method
public void actionPerformed(ActionEvent ae)
{
    repaint();
}


I'm not 100% sure if this is will work right out of the box, but it is definitely something to consider implementing.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Resizing JPanel

Posted 01 April 2010 - 04:41 PM

@rhaktkdl: JPanel auto-resizes when JFrame is resized, so no need to force resizing. So your problem probably has to do with your painting codes.

This post has been edited by macosxnerd101: 01 April 2010 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Resizing JPanel

Posted 01 April 2010 - 06:53 PM

View Postzim1985, on 01 April 2010 - 03:33 PM, said:

You would need to call the repaint method for the JPanel, so you need to make a paint method.

In the paint method:
public void paint(Graphics g)
{
    JPanel panel = new JPanel();
    MyFrameClass frame = new MyFrameClass():
    height = frame.getHeight();
    width = frame.getWidth();
    panel.setSize(height, width);
}


Then you would call the repaint method (which calls paint) in the actionPerformed method
public void actionPerformed(ActionEvent ae)
{
    repaint();
}


I'm not 100% sure if this is will work right out of the box, but it is definitely something to consider implementing.


Zim, when overriding paint() or paintComponent(), the first call should always be a super call.

public void paint(Graphics g)
{
    super.paint(g);

    JPanel panel = new JPanel();
    MyFrameClass frame = new MyFrameClass():
    height = frame.getHeight();
    width = frame.getWidth();
    panel.setSize(height, width);
}


Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Resizing JPanel

Posted 01 April 2010 - 06:56 PM

Expanding on what Dogstopper said, the paint() and paintComponent() methods in the super classes have a lot of important functionality. When you override the respective methods and do not invoke the super methods, you eliminate this functionality enitrely, which will have unexpected side effects.
Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Resizing JPanel

Posted 01 April 2010 - 08:10 PM

Thanks Dogstopper, I'm kinda just getting into Swing and such, so that's helpful.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Resizing JPanel

Posted 01 April 2010 - 09:08 PM

And no need AT ALL to recreate a JPanel and a JFrame
they already exist... you just need to resize them (if Swing does not do it for you)
You are going into problems by creating new ones
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Resizing JPanel

Posted 01 April 2010 - 09:22 PM

View Postmacosxnerd101, on 01 April 2010 - 07:41 PM, said:

@rhaktkdl: JPanel auto-resizes when JFrame is resized, so no need to force resizing. So your problem probably has to do with your painting codes.


Swing already does this. If you track the size of the JPanel as you resize the JFrame it is on, you will notice a proportional change.
Was This Post Helpful? 0
  • +
  • -

#9 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Resizing JPanel

Posted 03 April 2010 - 02:16 PM

Whether or not the panel resizes will depend, of course, on the Layout it was added to. If you used GridLayout, or BorderLayout (and added it to the center, which is what happens by default if you added it to the the JFrame's contentPane without changing the Layout, and without designating an "anchor"), and probably some others. But, if it was added to a FlowLayout, or a GirBagLayout (without the proper fill and weights) then it will only be as large as it needs to be to display what it's displaying (or the preferredSize that was set, if there was one). Making the frame smaller will, obviously, cause it to resize (down to its "minimum" size), but expanding (under the circumstances listed above) will not always cause it to expand beyond its preferred size.

This post has been edited by masijade: 03 April 2010 - 02:17 PM

Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Resizing JPanel

Posted 03 April 2010 - 02:42 PM

Actually, regardless of the Layout you use, JPanels are resizable. The best way to prevent the resizing of your main JPanel is to prevent resizing of your JFrame. You can also set the JFrame's LayoutManager to something like GridBagLayout, and add the JPanel using appropriate constraints.

Edit: I completely misread your post. It sounds like you and I are saying the same thing.

This post has been edited by macosxnerd101: 03 April 2010 - 02:44 PM

Was This Post Helpful? 1
  • +
  • -

#11 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Resizing JPanel

Posted 03 April 2010 - 02:49 PM

View Postmacosxnerd101, on 03 April 2010 - 09:42 PM, said:

Edit: I completely misread your post. It sounds like you and I are saying the same thing.


;-)

Edit: And, oops, I meant GridBagLayout not GirBagLayout. Another case where my mind outpaced my fingers, so they left something out to catch up. ;-)

This post has been edited by masijade: 03 April 2010 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Resizing JPanel

Posted 03 April 2010 - 04:20 PM

View Postmasijade, on 03 April 2010 - 04:49 PM, said:

....Another case where my mind outpaced my fingers, so they left something out to catch up. ;-)


We all do that...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1