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!
Code java:
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
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.
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
Re: setPrefferedsize not working?
You may be mistaken on what actually is the parent container here - the JDialog, for which you call setSize()
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:
Code Java:
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
}
}
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.
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!
Re: setPrefferedsize not working?
Quote:
Originally Posted by
Manish87
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)
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"!.o:-)
Kind Regards,
Manish87