LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-23-2005, 04:17 PM   #1
95se
Member
 
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:

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);
      }
    }
    add(b);
  }

  public static void main(String args[]) {
    JFrame j = new JFrame("");
    MyPanel m = new MyPanel();
    m.addComponentListener(new ComponentAdapter() {
		public void componentResized(ComponentEvent e) { 
		    System.out.println("AHH!!!\n");
		}
	    });
    j.getContentPane().add(new Asn5());
    j.pack();
    j.setVisible(true);
  }
}
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, 04:48 PM   #2
95se
Member
 
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
 
  


Reply


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 02:09 AM
why GTK box resizes in both horizontal and vertical direction? monojit_18 Programming 0 12-06-2004 12:50 AM
[JAVA] JPanel over JPanel ... not being displayed properly linux_ub Programming 0 08-13-2004 11:21 AM
(jwstric2) JFrame error on JAVA nedian123 Programming 2 07-02-2004 05:10 AM
(java) mouselistener in JPanel dave bean Programming 6 01-27-2004 12:44 PM


All times are GMT -5. The time now is 01:56 AM.

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