• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Box.createGlue() creates nothing

 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic