Swing: an alignment problem

I had a question about how to lay out some of the swing components.

Let's say I have a JPanel that contains a JLabel and a JTextField. I want the JLabel to draw on the leftmost side of the JPanel, and the JTextField on the right side of the JPanel. I tried using BoxLayout and Horizontal Glues, but I couldn't get it to work. Can someone explain how this should be done? And by the way, I also have to set the size of the JTextField to grow from right to left.

EDIT: Here's my class, it's pretty simple.

public class TextField extends JPanel {
    private JLabel label;
    private JTextField textField;
    public TextField(String labelText){
        this.label = new JLabel(labelText);
        this.textField = new JTextField("");
        Box horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(label);
        horizontalBox.add(Box.createHorizontalGlue());
        horizontalBox.add(textField);
        add(horizontalBox);
    }
}

      

+2


source to share


6 answers


You can also use the border layout and add a label using the BorderLayout.WEST parameter and the TextField using the BorderLayout.EAST parameter.



+1


source


One of the best ways to debug swing user interfaces is to add visible borders to your components to better understand what's going on.

Try adding this after creation horizontalBox

:

horizontalBox.setBorder(BorderFactory.createLineBorder(Color.black));

      

You will most likely find that yours TextField

shrinks to the absolute minimum size needed to display any text passed to the constructor and the minimum size of the JTextField (which is basically just one visible character space).

Now try adding this to the constructor:

horizontalBox.setPreferredSize(new Dimension(400, 40));

      

Then try replacing the glue with the rack:



horizontalBox.add(Box.createHorizontalStrut(30));

      

However, I think the biggest problem is that you are using a JPanel and then add a box component to it, which makes resizing the component problematic.

Try this and see if it works for you:

public TextField(String labelText){
    this.label = new JLabel(labelText);
    this.textField = new JTextField("");
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    this.setBorder(BorderFactory.createLineBorder(Color.black));  // debug
    this.add(label);
    this.add(Box.createHorizontalStrut(30));
    this.add(textField);
}

      

[ps]

You really want to revise the name of this JPanel extension. Perhaps TextFieldDisplay or TextFieldPanel would be more appropriate.

+2


source


I tried using BoxLayout and Horizontal Adhesives, but I couldn't make it work. Can someone explain how this should be done?

There is no trick. Read the Swing tutorial on How to Use Box Layout for a working example.

If it still doesn't work, you need to send an SSCCE message because we can't guess what you might be doing wrong.

+1


source


To use BoxLayout:

public TextField(String labelText){
    this.label = new JLabel(labelText);
    this.textField = new JTextField("");
    this.setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) );
    this.add( label );
    this.add( Box.createHorizontalGlue() );
    this.add( textField );
}

      

I like to use GridBagLayout for panels that have either complex layouts or components that need to "fill" part of the panel.

JPanel panel = new JPanel();
JLabel label = new JLabel( "Enter your text:" );
JTextField textField = new JTextField();
panel.setLayout( new GridBagLayout() );
panel.add( label, 
    new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, 
    GridBagConstraints.EAST, GridBagConstraints.NONE,
    new Insets( 0, 0, 0, 0 ), 0, 0 ) );
panel.add( textField, 
    new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0, 
    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
    new Insets( 0, 0, 0, 0 ), 0, 0 ) );

      

You can find a good explanation of how you are using the GridBagLayout here .

0


source


The problem with the code you posted is not so much the BoxLayout as the layout that contains it. JPanel uses FlowLayout by default. When you add a component to the FlowLayout, it doesn't expand to fill all the free space.

So, instead of adding a box to the panel, make a class that extends the box, or set the panel layout to BoxLayout and add components directly.

0


source


Install JPanel to use BorderLayout. This, combined with Box, will give you just about any layout you need.

0


source







All Articles