Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-23-2005, 05:17 PM   #1
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Resizing a JFrame when JPanel resizes?

Ok, I am trying to do something in Java. I have a JPanel. This JPanel dynamically adds components to it, so it's size can change at any time. Now, I have a JFrame. This JFrame holds this JPanel. What I want is for when the JPanel gets resized (by adding a component to it), the JFrame will be resized as well. I am trying to use a ComponentListener in the JFrame, which listenes to the JPanel, and resizes the JFrame when the JPanel gets resized. However, for some reason, this Component listener doesn't work. When the JPanel is resized it doesn't run the componentResized function. It also doesn't call any other of the component functions when it should for the JPanel (like componentShown, etc...).

Example of code:

public class MyPanel extends JPanel {
  public MyPanel() {
    JButton b = new JButton("!!!");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        add(new JButton("..."));
        setSize(getPreferredSize().width, getPreferredSize().height);

  public static void main(String args[]) {
    JFrame j = new JFrame("");
    MyPanel m = new MyPanel();
    m.addComponentListener(new ComponentAdapter() {
		public void componentResized(ComponentEvent e) { 
    j.getContentPane().add(new Asn5());
This doesn't print out "AHH!!!" when it is resized (and the getPreferredSize DOES change, so it should be working). What am I doing wrong. Is there a way to force the JPanel to notify it's component listeners of a change?
Old 03-23-2005, 05:48 PM   #2
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Original Poster
Rep: Reputation: 32
Nevermind, I'm stupid.
In my program I had the lines like this:
m = new MyPanel();
m.addComponentListener( new ComponentAda.....
j.add(new MyPanel());
So it wasn't even using the Panel w/ the component listener! Ignore this post


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
java JFrame sizes. byteframe Programming 8 10-25-2005 03:09 AM
why GTK box resizes in both horizontal and vertical direction? monojit_18 Programming 0 12-06-2004 01:50 AM
[JAVA] JPanel over JPanel ... not being displayed properly linux_ub Programming 0 08-13-2004 12:21 PM
(jwstric2) JFrame error on JAVA nedian123 Programming 2 07-02-2004 06:10 AM
(java) mouselistener in JPanel dave bean Programming 6 01-27-2004 01:44 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration