jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Box.createGlue() creates nothing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Box.createGlue() creates nothing" Watch "Box.createGlue() creates nothing" New topic
Author

Box.createGlue() creates nothing

Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
Hi everybody,
nomatter how long I fiddle with java, I always end struggling with the gui!
consider following stuff: I expext glue and gets a monstruous large JTextField. What have i overlooked now?
Thanks

import java.awt.*;
import javax.swing.*;
class Test extends JFrame{
Test(){
super();
String[] names = {"I","LIKE"};
String[] names2 = {"J","A","V","A","R","A","N","C","H"};
JList jList1 = new JList(names);
JList jList2 = new JList(names2);
JLabel jLabel1 = new JLabel("K.Sierra for pope!");
JButton jButton = new JButton("OK");
JTextField jTextField = new JTextField(12);
JLabel display = new JLabel("but swing? ");
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box,BoxLayout.X_AXIS));
box.add(new JScrollPane(jList1));
box.add(new JScrollPane(jList2));
Box mainPanel = new Box(BoxLayout.Y_AXIS);
mainPanel.add(jLabel1);
mainPanel.add(box);
mainPanel.add(jTextField);
//I expet some really large space here
mainPanel.add(Box.createGlue());
//and now continue
mainPanel.add(jButton);
mainPanel.add(display);
getContentPane().add(mainPanel,BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
}
public static void main(String[] args){
new Test();
}
}
Vikalp Setya
Greenhorn

Joined: May 10, 2003
Posts: 18
Hi,
While using BoxLayout, sometimes you need to put preferredSize and alignmentX and alignmentY to get the desired results. I have pasted the code below. see if it works:
import java.awt.*;
import javax.swing.*;
class Test extends JFrame{
Test(){
super();
String[] names = {"I","LIKE"};
String[] names2 = {"J","A","V","A","R","A","N","C","H"};
JList jList1 = new JList(names);
JList jList2 = new JList(names2);
JLabel jLabel1 = new JLabel("K.Sierra for pope!");
JButton jButton = new JButton("OK");
JTextField jTextField = new JTextField(12);
jTextField.setMaximumSize(new Dimension(200,30));
jTextField.setPreferredSize(new Dimension(200,25));
jTextField.setAlignmentX(1.0f);
JLabel display = new JLabel("Swing is superb");
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box,BoxLayout.X_AXIS));
box.add(new JScrollPane(jList1));
box.add(new JScrollPane(jList2));
Box mainPanel = Box.createVerticalBox();
mainPanel.add(jLabel1);
mainPanel.add(box);
mainPanel.add(jTextField);
mainPanel.add(Box.createHorizontalGlue());
//I expet some really large space here
mainPanel.add(Box.createVerticalGlue());
//and now continue
mainPanel.add(jButton);
mainPanel.add(display);
getContentPane().add(mainPanel);
setSize(800,800);
setVisible(true);
}
public static void main(String[] args){
new Test();
}
}


Regards,<br />vikalps
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Ko,
BoxLayout pays attention to maximum size of their components and this has not been limited.
I am not very sure if this is the optimal way but you can set the maximum size after it is determined by pack:
getContentPane().add(mainPanel,BorderLayout.CENTER);
pack();
jLabel1.setMaximumSize(jLabel1.getPreferredSize());
jTextField.setMaximumSize(jTextField.getPreferredSize());
setSize(800,600);
setVisible(true);


SCJP2. Please Indent your code using UBB Code
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
Yep: that's it!
I must say that I prefer jLabel1.setMaximumSize(jLabel1.getPreferredSize()) to jLabel1.setMaximumSize(new Dimension(200,200)) as I don't like "hard-coding" numbers. Doesn't make it more transparent, though.

Swing is certainly superb, Mr.Vikalps; but it's ways are somewhat mysterious too, I guess.
Thanks for the replies!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Box.createGlue() creates nothing