boxlayout question
hey all
i have a jfame that have a panel that have a y-axis boxlayout, in this panel i have another panel that have an x-axis boxlayout, in this panel i added a jlabel and a jtextfield but the problem is that those two components appear in the center of the panel, i tried setalignmentx() but it doesn't work...
how to make the two component with left alignment?
please help
thanks in advance
[424 byte] By [
bif_fa] at [2007-11-27 1:55:01]

# 1
use setAlignmentX() for all the Y_AXIS components
# 2
do u mean that i should use setalignmentX() for the jlabel and the jtextfield in the panel that have the Y_AXIS ?
# 3
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Medawwar DVD rental center");
setSize(1000, 700);
setLocation(20, 20);
initializeMenuBar();
initializeGUI();
}
public void initializeGUI() {
JTabbedPane tp = new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tp);
tp.addTab("Rent Fees", new RentFeesGUI());
}
public void initializeMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
}
public static void main(String[] args) {
GUI g = new GUI();
g.setVisible(true);
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("exit"))
System.exit(0);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
public class RentFeesGUI extends JPanel{
private JLabel rentFee = new JLabel("Rent Fee ");
private JLabel lateFee = new JLabel("Late Fee ");
private JTextField rentFeeField = new JTextField(10);
private JTextField lateFeeField = new JTextField(10);
private JPanel p = new JPanel(true);
private JPanel p1 = new JPanel(true);
private JPanel p2 = new JPanel(true);
private Font f = new Font("Times New Roman", Font.BOLD, 20);
public RentFeesGUI(){
initializeGUI();
}
public void initializeGUI(){
rentFee.setFont(f);
lateFee.setFont(f);
rentFeeField.setFont(f);
lateFeeField.setFont(f);
rentFeeField.setEditable(false);
rentFee.setAlignmentX(LEFT_ALIGNMENT);
lateFeeField.setEditable(false);
add(p);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.setAlignmentX(LEFT_ALIGNMENT);
p.add(p1);
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p.setAlignmentX(LEFT_ALIGNMENT);
p1.add(rentFee);
p1.add(rentFeeField);
p.add(p2);
p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
p.setAlignmentX(LEFT_ALIGNMENT);
p2.add(lateFee);
p2.add(lateFeeField);
}
}
this is my code, i did as u told me but it didnt work so i put setalignmentX() in every place you could think about....
can you please tell me what is wrong and where is the right place to put it in order to get the desired effect ?
thanks in advance
# 4
> do u mean that i should use setalignmentX() for the jlabel and the jtextfield in the panel that have the Y_AXIS ? No. The components that you add directly to the vertical BoxLayout should have the alignment set. So that would be panels p1 and p2.
# 5
thanks a lot, i will try what u suggested asap and i will get back to u with an answer...hope it works...
# 6
i modified my RentFeesGUI class as follow
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
public class RentFeesGUI extends JPanel{
private JLabel rentFee = new JLabel("Rent Fee ");
private JLabel lateFee = new JLabel("Late Fee ");
private JTextField rentFeeField = new JTextField(10);
private JTextField lateFeeField = new JTextField(10);
private JPanel p = new JPanel(true);
private JPanel p1 = new JPanel(true);
private JPanel p2 = new JPanel(true);
private Font f = new Font("Times New Roman", Font.BOLD, 20);
public RentFeesGUI(){
initializeGUI();
}
public void initializeGUI(){
rentFee.setFont(f);
lateFee.setFont(f);
rentFeeField.setFont(f);
lateFeeField.setFont(f);
rentFeeField.setEditable(false);
lateFeeField.setEditable(false);
add(p);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p1.setAlignmentX(LEFT_ALIGNMENT);
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
p1.add(rentFee);
p1.add(rentFeeField);
p.add(p1);
p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
p2.setAlignmentX(LEFT_ALIGNMENT);
p2.add(lateFee);
p2.add(lateFeeField);
p.add(p2);
}
}
and my GUI class remained the same
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("DVD rental center");
setSize(1000, 700);
setLocation(20, 20);
initializeMenuBar();
initializeGUI();
}
public void initializeGUI() {
JTabbedPane tp = new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tp);
tp.addTab("Rent Fees", new RentFeesGUI());
}
public void initializeMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
}
public static void main(String[] args) {
GUI g = new GUI();
g.setVisible(true);
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("exit"))
System.exit(0);
}
}
but i still have the same problem which is that the components (Jlabel and jtextfields) are still in the center even though i used setAlignmentX(LEFT_ALIGNMENT)....
please help...
thanks in advance
# 7
Your RentFeesGUI class already is a panel, which uses a FlowLayout by default. So when you add your "p" panel to it, it get centered, which is the default for a FlowLayout.
So get rid of the "p" panel and just make the layout of the class a vertical BoxLayout. Then you add p1 and p2 directly to the panel.
# 8
thanks a lot... i will try it asap and will get back to you if anything goes wrong...thanks in advance
# 9
man, i did try what u suggested, it kinda worked but now the components occupy the whole panel, they look huge...
here is my code with the updates
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("DVD rental center");
setSize(1000, 700);
setLocation(20, 20);
initializeMenuBar();
initializeGUI();
}
public void initializeGUI() {
JTabbedPane tp = new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tp);
tp.addTab("Rent Fees", new RentFeesGUI());
}
public void initializeMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
}
public static void main(String[] args) {
GUI g = new GUI();
g.setVisible(true);
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("exit"))
System.exit(0);
}
}
class RentFeesGUI extends JPanel{
private JLabel rentFee = new JLabel("Rent Fee ");
private JLabel lateFee = new JLabel("Late Fee ");
private JTextField rentFeeField = new JTextField(10);
private JTextField lateFeeField = new JTextField(10);
private JPanel p1 = new JPanel(true);
private JPanel p2 = new JPanel(true);
private Font f = new Font("Times New Roman", Font.BOLD, 20);
public RentFeesGUI(){
initializeGUI();
}
public void initializeGUI(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
rentFee.setFont(f);
lateFee.setFont(f);
rentFeeField.setFont(f);
lateFeeField.setFont(f);
rentFeeField.setEditable(false);
lateFeeField.setEditable(false);
p1.setAlignmentX(LEFT_ALIGNMENT);
p1.setLayout(new BoxLayout(p1,BoxLayout.LINE_AXIS));
p1.add(rentFee);
p1.add(rentFeeField);
add(p1);
p2.setLayout(new BoxLayout(p2,BoxLayout.LINE_AXIS));
p2.setAlignmentX(LEFT_ALIGNMENT);
p2.add(lateFee);
p2.add(lateFeeField);
add(p2);
}
}
# 10
Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html]How to Use Box Layout[/url]. The box layout respects the maximum size of each component so it resizes the components automatically to fill the available space.
So, you can set the maximum size of each component equal to the preferred size.
Or, you can just use a FlowLayout instead.
# 11
for all who are interested in a solution, here is the solution that i came up with
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class GUI extends JFrame implements ActionListener {
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("DVD rental center");
setSize(1000, 700);
setLocation(20, 20);
initializeMenuBar();
initializeGUI();
}
public void initializeGUI() {
JTabbedPane tp = new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tp);
tp.addTab("Rent Fees", new RentFeesGUI());
}
public void initializeMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
this.setJMenuBar(menuBar);
}
public static void main(String[] args) {
GUI g = new GUI();
g.setVisible(true);
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("exit"))
System.exit(0);
}
}
class RentFeesGUI extends JPanel implements WindowListener {
private JLabel rentFee = new JLabel("Rent Fees ");
private JLabel lateFee = new JLabel("Late Fees ");
private JTextField rentFeeField = new JTextField(10);
private JTextField lateFeeField = new JTextField(10);
private final int HORIZONTAL_SPACING = 600;
private final int VERTICAL_SPACING = 700;
private final int SMALL_VERICAL_SPACING = 10;
private final int FONT_SIZE = 20;
private Font f = new Font("Times New Roman", Font.BOLD, FONT_SIZE);
public RentFeesGUI() {
setupComponents();
setupFonts();
initializeGUI();
}
public void setupFonts() {
rentFee.setFont(f);
lateFee.setFont(f);
rentFeeField.setFont(f);
lateFeeField.setFont(f);
}
public void setupComponents() {
rentFeeField.setBorder(BorderFactory.createLoweredBevelBorder());
lateFeeField.setBorder(BorderFactory.createLoweredBevelBorder());
}
public void initializeGUI() {
rentFeeField.setEditable(false);
lateFeeField.setEditable(false);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Box b1 = new Box(BoxLayout.LINE_AXIS);
b1.add(rentFee);
b1.add(rentFeeField);
b1.add(Box.createHorizontalStrut(HORIZONTAL_SPACING));
add(b1);
add(Box.createVerticalStrut(SMALL_VERICAL_SPACING));
Box b2 = new Box(BoxLayout.LINE_AXIS);
b2.add(lateFee);
b2.add(lateFeeField);
b2.add(Box.createHorizontalStrut(HORIZONTAL_SPACING));
add(b2);
add(Box.createVerticalStrut(VERTICAL_SPACING));
}
public void setupButtons() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void resetFields() {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked the first time a window is made visible.
*/
public void windowOpened(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when the user attempts to close the window
* from the window's system menu.
*/
public void windowClosing(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window has been closed as the result
* of calling dispose on the window.
*/
public void windowClosed(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window is changed from a normal to a
* minimized state. For many platforms, a minimized window
* is displayed as the icon specified in the window's
* iconImage property.
*
* @see java.awt.Frame#setIconImage
*/
public void windowIconified(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a window is changed from a minimized
* to a normal state.
*/
public void windowDeiconified(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when the Window is set to be the active Window. Only a Frame or
* a Dialog can be the active Window. The native windowing system may
* denote the active Window or its children with special decorations, such
* as a highlighted title bar. The active Window is always either the
* focused Window, or the first Frame or Dialog that is an owner of the
* focused Window.
*/
public void windowActivated(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Invoked when a Window is no longer the active Window. Only a Frame or a
* Dialog can be the active Window. The native windowing system may denote
* the active Window or its children with special decorations, such as a
* highlighted title bar. The active Window is always either the focused
* Window, or the first Frame or Dialog that is an owner of the focused
* Window.
*/
public void windowDeactivated(WindowEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
