BoxLayout and JTextField
Hi, I have the following problem:
Inside a JFrame I use borderlayout and I added a BoxLayout to the EAST. The boxlayout contains a JButton and a JTextfield. When i run the code, on the left side of the BoxLayout a gap appears, with the size depending from the size of the textfield. If the textfield is not added to the boxlayout, the gap disappears.
Here is an example of the code i use:
package example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass Mainextends JFrame{
class Closeextends WindowAdapter{
public Close(){}
publicvoid windowClosing(WindowEvent e){System.exit(0);}
}
public Main(){
super("frame");
this.addWindowListener(new Close());
this.setBounds(0,0,500,500);
this.setVisible(true);
//this.pack();
JPanel mainpanel =new JPanel(new BorderLayout());
JPanel subpanel =new JPanel();
subpanel.setBorder(BorderFactory.createRaisedBevelBorder());
subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
subpanel.add(new JButton("jbutton"));
JTextField tf =new JTextField(3);
tf.setMaximumSize(tf.getPreferredSize());
subpanel.add(tf);
mainpanel.add(subpanel, BorderLayout.EAST);
this.add(mainpanel);
this.setVisible(true);
}
publicstaticvoid main(String[] args){
Main m =new Main();
}
}
adding the textfield to a panel and adding that panel to the boxlayout does not help. My question is: how can i make that gap no to appear/ why is that gap displayed?
# 1
1)Nest your components in JPanels to give you more control.
2)Utilyze preferredSize and maximumSize when dealing with LayoutManagers that honor such things (view LayoutManager Docs to understand how they work)
3)Box class is invaluable to Gui Designer. Use Glue, Struts etc. when needed...
see this code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
class Close extends WindowAdapter{
public Close(){}
public void windowClosing(WindowEvent e){System.exit(0);}
}
public Main() {
super("frame");
this.addWindowListener(new Close());
this.setBounds(0,0,500,500);
this.setVisible(true);
//this.pack();
JPanel mainpanel = new JPanel(new BorderLayout());
JPanel subpanel = new JPanel();
subpanel.setBorder(BorderFactory.createRaisedBevelBorder());
subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
JButton button =new JButton("jbutton");
button.setPreferredSize(new Dimension(73,20));
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
buttonPanel.setMaximumSize(new Dimension(150,20));
subpanel.add(buttonPanel);
JTextField tf = new JTextField("blaghblah");
tf.setPreferredSize(new Dimension(73,20));
JPanel textPanel = new JPanel();
textPanel.add(tf);
textPanel.setMaximumSize(new Dimension(150,20));
subpanel.add(textPanel);
subpanel.add(Box.createGlue());
mainpanel.add(subpanel, BorderLayout.EAST);
this.add(mainpanel);
this.setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}
# 2
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Box Layout[/url] for an explanation on how components with different alignments are handled. Here is the updated code with the alignment difference fixed:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
class Close extends WindowAdapter{
public Close(){}
public void windowClosing(WindowEvent e){System.exit(0);}
}
public Main() {
super("frame");
this.addWindowListener(new Close());
this.setBounds(0,0,500,500);
this.setVisible(true);
//this.pack();
JPanel mainpanel = new JPanel(new BorderLayout());
JPanel subpanel = new JPanel();
subpanel.setBorder(BorderFactory.createRaisedBevelBorder());
subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
JButton button = new JButton("jbutton");
System.out.println(button.getAlignmentX());
subpanel.add(button);
JTextField tf = new JTextField(3);
System.out.println(tf.getAlignmentX());
tf.setAlignmentX(0.0f);
tf.setMaximumSize(tf.getPreferredSize());
subpanel.add(tf);
mainpanel.add(subpanel, BorderLayout.EAST);
this.getContentPane().add(mainpanel);
this.setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
}