Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: setPrefferedsize not working?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setPrefferedsize not working?

    Hi,

    scenario:

    i was working on a swing application,which involved placing one panel inside another hence i placed the panel2(layout:borderlayout) inside another panel1(layout: box layout),but when i tryed to change the size of panel2 with the help of setPrefferedsize() method it failed to resize the panel! please provide me any hint on this problem,do you have to use the method setMaximumsize() or sumthing?.

    apologies if my english was bad!

    class CallMe extends JDialog
    {
        public CallMe()
      {
            initUI();
     
      }
     
        public void initUI()
        {
            setTitle("Tip of the Day");
            setLocation(350,150);
            setSize(1000,1000);
     
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     
            JPanel parentpanel=new JPanel();
            BoxLayout box=new BoxLayout(parentpanel,BoxLayout.Y_AXIS);
            parentpanel.setLayout(box);       
            JPanel toppanel=new JPanel();
            BorderLayout borderlayout=new BorderLayout();
            toppanel.setLayout(borderlayout);    
     
          [B]  toppanel.setPreferredSize(new Dimension(400,50));     [/B]
     
            toppanel.setBackground(Color.red);
            JLabel label=new JLabel("JDeveloper Productivity Hints");       
            label.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
            toppanel.add(label,BorderLayout.WEST);
            ImageIcon icon=new ImageIcon("c:\\pics\\ship.png");
            JLabel label2=new JLabel(icon);
            label2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 30));
            toppanel.add(label2,BorderLayout.EAST);
            JSeparator seperator=new JSeparator();
            seperator.setForeground(Color.gray);
            toppanel.add(seperator,BorderLayout.SOUTH);       
     
            [B]parentpanel.add(toppanel);[/B]
     
            add(parentpanel);
     
        }
     
    }

    Kind regards,
    Manish87
    Last edited by copeg; February 9th, 2011 at 10:04 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: setPrefferedsize not working?

    See Solving common layout problems. By setting the preferred size you have given a hint as to how it will render, but the rendering size will be left to the layout manager (and how it deals with the complete layout eg container sizes, layout of other components and their sizes, etc..). Recommend reading a bit more on layouts, playing with different layouts, and using padding components such as those in the Box class to get the layout you truly want.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setPrefferedsize not working?

    Hello,


    Thanks a ton for the reply copeg.


    Yes,i did go through the link posted by you,Learned some new things about the layouts.

    Neways from the box layout section of the link you posted i was not clear with the few things that is,they have mention that the hieght of the container(box layout) depends on the maximum preffered hieght of the children(components),if incase sum of the hieght of the components donot meet the the size of the containers then the box layout manager shrinks or grows the componets to fill in the area,now coming back to the my code, the container(parentpanel) automatically resizes the component(top panel) fills in the extra area though i have mentioned the maximum preffered size of the component(top panel) ,using the method setPrefferedSize(); why is it so? please feel free to correct if i have mentioned anything wrong!

    when i used the method that is setMaximuSize(new Dimension(x,y)),the componets where arranged in my desired way,but when i use setPrefferedSize() it has no effect on the code!,basically the setMaximumSize() and setPrefferedSize() perform the same function rite?

    Sorry if my questions are too lame to be asked over here,i recently started working on java swing!



    Kind Regards,
    Manish87

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: setPrefferedsize not working?

    You may be mistaken on what actually is the parent container here - the JDialog, for which you call setSize()

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setPrefferedsize not working?

    Hi,

    Thanks for the reply sir,as you had pointed out some of the error in the code,i have modified it according to it!.But the problem of resizing of panel in the box layout container still persist.

    Here's My modified code:

     
    class CallMe extends JDialog
    {
        public CallMe()
        {
     
            initUI();
     
        }
     
        public void initUI()
        {
     
                setSize(500,500);
                setLocation(450,200);
                setTitle("Simple program");
     
     
                BoxLayout b=new BoxLayout(getContentPane(),BoxLayout.Y_AXIS);       //create boxlayout
                getContentPane().setLayout(b);                                             //change the default-layout(borderLayout) of   ontentpane to BoxLayout
                JPanel panel=new JPanel();
                JButton button1=new JButton("Click me");
                panel.add(button1);                                                            //add the button to the panel which has flow layout(default)
                panel.setBackground(Color.red);
                [B]panel.setPreferredSize(new Dimension(100,50));               //set The size of the panel ***dsnt seem to have any effect on the code***[/B]
     
           //panel.setMaximumSize(new Dimension(1500,50));              // when this code is added the panel is set to the desired size!
     
             getContentPane().add(panel);                                            //add the panel to the contentpane
             getContentPane().add(Box.createVerticalGlue());              //create an vertical invisible component once the panel has been added
     
     
        }
     
     
     
     
    }
    Last edited by Manish87; February 11th, 2011 at 10:39 AM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: setPrefferedsize not working?

    For future reference, please use the code tags (you can find out how to do so by reading the announcements post at the top of every forum).

    Switch your BoxLayout to a FlowLayout and see what happens.

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setPrefferedsize not working?

    Hello,


    Well sir thank you for the reply,As you directed,if i change the layout of the Contentpane from BoxLayout to flowLayout,the setPrefferedSize() comes into play and the panel appears at the top of the dialog window(as i want).Sir but why isnt the same method working when i use the boxlayout? Ergh! goin bonkers here!

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: setPrefferedsize not working?

    Quote Originally Posted by Manish87 View Post
    but why isnt the same method working when i use the boxlayout? Ergh! goin bonkers here!
    Because this is their programmed behavior. The API and tutorials contain information on this behavior (albeit sometimes buried within)

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setPrefferedsize not working?

    Hello,

    Well i finally figured out what the problem was with the code.Read the oracle turotial on boxlayout for like nearly 14 times and finally got it!*Sigh*. Neways key point of the BoxLayout is that it "HONOURES THE MAXIMUM SIZE OF THE COMPONENT UNLIKE OTHER LAYOUTS"!.












    Kind Regards,
    Manish87

Similar Threads

  1. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  2. [SOLVED] CoreJavaExample Not Working
    By prasana in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 12th, 2010, 11:23 AM
  3. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  4. Boggle not working?
    By gandalf5166 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:53 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM