Swing: Alignment Problem - java

I have a question about laying out some swing components.
Say, I have a JPanel which contains a JLabel and a JTextField. I want JLabel to be drawn on the left-most side of JPanel, and JTextField to be drawn on the right-most side of JPanel. I tried using BoxLayout and Horizontal Glues, but I couldn't make it work. Can somebody explain how this should be done? And by the way, I also should be able to set the JTextField's size, which will grow from right to left.
EDIT: Here is 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);
}
}

One of the best ways to debug swing UIs is to add visible borders to your components to get a better idea of what is going on.
Try adding this after you create the horizontalBox:
horizontalBox.setBorder(BorderFactory.createLineBorder(Color.black));
Most likely what you will find is that your TextField is shrunk to the absolute minimum size required to display whatever text you pass 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 a strut:
horizontalBox.add(Box.createHorizontalStrut(30));
That said, I think the biggest issue is that you are using a JPanel and then adding a box component to it, which makes resizing of 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);
}
[p.s.]
You really want to reconsider the name of that JPanel extension. Perhaps TextFieldDisplay or TextFieldPanel would be more appropriate.

You could also use border layout and add the label using the BorderLayout.WEST option and the TextField using the BorderLayout.EAST option.

I tried using BoxLayout and
Horizontal Glues, but I couldn't make
it work. Can somebody explain how this
should be done?
There is no trick to this. Read the Swing tutorial on How to Use Box Layout for a working example.
If it still doesn't work then you need to post your SSCCE because we can't guess what you might be doing wrong.

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 should "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 use GridBagLayout here.

The problem with the code that you posted isn't so much the BoxLayout, it's the layout that contains it. By default, JPanel uses FlowLayout. When you add a component to a FlowLayout it does not expand to fill all of the available space.
So, instead of adding a Box to the panel, either make the class extend Box or set the panel layout to BoxLayout and add the components directly.

Set your JPanel to use BorderLayout. This, in combination with Box will give you almost any layout you need.

Related

Align to the limit Components in a JPanel Swing

I need to totally align components to the JTextArea components, I am currently using a BoxLayout and I already used the setAlignmentX and setHorizontalAlignment to LEFT but it's not working. Here I upload an image to make clearer what I mean. For example look at "+ Pla PLAMARC" it's clearly not aligned with the text area component.
By the moment this is the code:
//Declarations
private JLabel nomPla;
private JTextArea infoPla;
private JScrollPane textAreaScroll;
//Inside the constructor
nomPla = new JLabel();
infoPla = new JTextArea(2, 50);
textAreaScroll = new JScrollPane(infoPla);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
nomPla.setAlignmentX(Component.LEFT_ALIGNMENT);
nomPla.setHorizontalAlignment(nomPla.LEFT);
textAreaScroll.setAlignmentX(Component.CENTER_ALIGNMENT);
this.setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));
this.add(nomPla, BorderLayout.NORTH);
this.add(textAreaScroll, BorderLayout.NORTH); //Orientacions
I am clearly telling nomPla to be on the leftside, but this is not the same as the JTextArea.
How is this done then?
I already used the setAlignmentX and
The setAlignmentX(...) needs to be applied to all the components you add to the BoxLayout if you want all to be left aligned with respect to the BoxLayout.
Edit:
I just want the labels to be on the left side, not the JTextArea components..
Then you need to use a wrapper panel for the BoxLayout Panel.
For example:
JPanel wrapper = new JPanel( new FlowLayout(FlowLayout.CENTER) );
wrapper.add(yourBoxLayoutPanel);
frame.add(wrapper);
Now all the components in the BoxLayout will be left aligned and the BoxLayout panel will be center aligned in the wrapper panel.
Layout management is about nesting panels with different layout manager to achieve you desired effect.

Java Swing Borders to textfields and buttons

I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.
I have read some materials regarding Titled borders but I think its only for single elements. Please suggest.
You could make a JPanel with a titled border, then put however many components you wanted in the JPanel using the content manager of your choice.
An example:
JPanel myPanel = new JPanel();
myPanel.setBorder(new TitledBorder(null, "My Title", TitledBorder.LEADING, TitledBorder.TOP, null, null));
myPanel.setLayout(new GridLayout(1, 0, 0, 0));
JButton button = new JButton("New button");
myPanel.add(button);
JLabel label = new JLabel("New label");
myPanel.add(label);
You can add that last 2 components to a JPanel and then add that panel to main frame. Now you can give border to JPanel and it will around 2 components inside it.
To give border to jPanel you can use following:
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createLineBorder(Color.black));
If you want titled border then you can use following:
pane.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.blue), "Title",
TitledBorder.LEFT, TitledBorder.TOP));
Reference: http://download.oracle.com/javase/tutorial/uiswing/components/border.html

Button inside text box

My code shows a button inside a textbox, but when the input value changes, the size of the text box also changes. That I don't like. Is there any solution such that the textbox size remains fixed? Or any other idea on how to create a button inside textbox?
The following is my code:
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
Try a BorderLayout and add the textfield at BorderLayout.CENTER and the button at BorderLayout.EAST.
I tried your code, and it seemed to work fine for me, with a slight modification. It may be that panel is resizing itself, not textField. Try wrapping panel in another JPanel to force its shape.
JPanel panel = new JPanel();
JPanel outerPanel = new JPanel();
panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) );
panel.add(textField);
panel.add(button);
panel.setBackground( textField.getBackground() );
panel.setBorder( textField.getBorder() );
textField.setBorder(null);
outerPanel.add(panel);
Your example appears to be the second alternative discussed in the article Component Border. Instead, you may want to install() a ComponentBorder, a third alterative described later in the same article.
Is there any solution such that the textbox size remains fixed?
I'm guessing you are creating the JTextField like:
JTextField textField = new JTextField();
In this case the text field size changes as you add text to it.
Instead you should use:
JTextField textField = new JTextField(10);
and the text field will remain a constant size an hold a minimum of 10 characters before scrolling.
Use a different Layout, or try absolute positioning

How do I keep JTextFields in a Java Swing BoxLayout from expanding?

I have a JPanel that looks something like this:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
panel.add(jTextField1);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton1);
panel.add(Box.createVerticalStrut(30));
panel.add(jTextField2);
panel.add(Box.createVerticalStrut(10));
panel.add(jButton2);
... //etc.
My problem is that the JTextFields become huge vertically. I want them to only be high enough for a single line, since that is all that the user can type in them. The buttons are fine (they don't expand vertically).
Is there any way to keep the JTextFields from expanding? I'm pretty new to Swing, so let me know if I'm doing everything horribly wrong.
textField = new JTextField( ... );
textField.setMaximumSize( textField.getPreferredSize() );
If you want the width to keep changing, just keep it set to MAX INT. So...
textField.setMaximumSize(
new Dimension(Integer.MAX_VALUE, textField.getPreferredSize().height) );
In my case I need a combination of all the answers for it to work properly. If I don't use glue, it is not centered vertically; if I don't restrict maximum size, it extends vertically; if I restrict both width and height, it is too small, being only wide enough to contain the initialization text.
textField = new JTextField("Hello, world!");
textField.setMaximumSize(
new Dimension(Integer.MAX_VALUE,
textField.getPreferredSize().height));
Box box = Box.createVerticalBox();
box.add(Box.createVerticalGlue());
box.add(textField);
box.add(Box.createVerticalGlue());
set the max height.
or put them in a scroll region
JPanel panel = new JPanel();
Box box = Box.createVerticalBox();
JTextField tf = new JTextField(8);
box.add(tf);
panel.add(box);
frame.getContentPane().add(panel, BorderLayout.CENTER);

How do i get vertical scrolling to JPanel?

I have written a code in java using swing, so that I will have a JscrollPane added to JPanel and then I will add buttons of fixed size to JPanel in vertical fashion
JPanel panel=new JPanel();
panel.setBackground(Color.WHITE);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(panel,v,h);
jsp.setPreferredSize(new Dimension(600,600));
jsp.setBounds(150,670,850,200);
frame.add(jsp);
then I am adding buttons to it at run time.
for(i=0;i<n;i++)
{
button[i]=new JButton();
button[i].setBounds(20,y,120,120);
button[i].setSize(120,120);
button[i].setToolTipText(file[i].toString());
button[i].setIcon(Icon);
panel.add(button[i]);
y=y+140;
}
I want to add a buttons one below the other...(i.e I want a vertical scrollbar)
i.e. button1
button2
'
'
but above code is giving me buttons in a line (i.e. I am getting horizontal scrollbar)
i.e. button1 button2...
another problem is the size of the buttons. Using btn.setSize() is not affecting size at all...
can anybody help me?
You must use an appropriate Layoutmanager like GridLayout, Boxlayout or GridBagLayout for the panel.
It depends what else you want to put into the panel.
GridLayout is easier to use IMO:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1)); // any number of rows, 1 column
...
panel.add(button[i]);
BoxLayout is almost as easy:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
...
panel.add(button[i]);
GridBagLayout is more powerful, allowing more than one column, components spanning more than one cell, ... needs a GridBagConstraints to add the elements:
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints(
0, RELATIVE, // x = 0, y = below previous element
1, 1, // cell width = 1, cell height = 1
0.0, 0.0 // how to distribute space: weightx = 0.0, weighty = 0,0
GridBagConstraints.CENTER, // anchor
GridBagConstraints.BOTH, // fill
new Insets(0, 0, 0, 0), // cell insets
0, 0); // internal padding
...
panel.add(button[i], constraints);
Have a look at this tutorial: Laying Out Components Within a Container (The visual guide is a good start point)
EDIT:
you can also lay out the components by hand, that is, specify the location and size of each component in the container. For this you must set the LayoutManager to null so the default manager gets removed.
JPanel panel = new JPanel();
panel.setLayout(null);
...
button[i].setLocation(x, y);
button[i].setSize(width, heigth);
// OR button[i].setBounds(x, y, width, height);
panel.add(button[i]);
You need to define an appropriate LayoutManager for your JPanel, which is responsible for how the Components added to it are positioned. The default LayoutManager is FlowLayout, which lays out Components left-to-right. For laying out Components vertically you should consider using BoxLayout or GridBagLayout.
You have to set LayoutManager for JPanel or use Box(BoxLayout.Y_AXIS) instead.
For the size of buttons use preferredSize
For your layout problem you need to change the layout manager to one that does a vertical layout. For playing around purposes you can use BoxLayout like this:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
This is much easier if you let the layout manager do its work.
In Swing, the way the components are layout over other component ( a panel for instance ) is using a layout manager.
It is used to avoid having to compute the coordinates of all the components against each other each time the container component resizes, or a new component is added.
There are different layout mangers, the one that you need here is BoxLayout.
By using this layout you don't need to specify the button position, nor its size. The layout manager query each component and use that information to place them in the correct position and size.
For instance the following frame
Was created this ( modified version of your ) code:
import javax.swing.*;
import java.awt.*;
public class ScrollTest {
private JPanel panel;
private Icon[] icons = new Icon[3];
public void main() {
panel =new JPanel();
// Use top to bottom layout in a column
panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
panel.setBackground(Color.WHITE);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(panel,v,h);
jsp.setPreferredSize(new Dimension(600,600));
jsp.setBounds(150,670,850,200);
JFrame frame = new JFrame();
frame.add(jsp);
// my addition to load sample icons
loadImages();
// simulate dynamic buttons
addButtons();
frame.pack();
frame.setVisible( true );
}
void loadImages() {
icons[0] = new ImageIcon( "a.png" );
icons[1] = new ImageIcon( "b.png" );
icons[2] = new ImageIcon( "c.png" );
}
void addButtons() {
for( int i = 0 ; i < icons.length ; i++ ) {
JButton button = new JButton();
Icon icon = icons[i];
button.setIcon( icon );
// Set the button size to be the same as the icon size
// The preferred size is used by the layout manager
// to know what the component "better" size is.
button.setPreferredSize( new Dimension( icon.getIconWidth(),
icon.getIconHeight() ) );
// This is IMPORTANT. The maximum size is used bythe layout manager
// to know "how big" could this component be.
button.setMaximumSize( button.getPreferredSize() );
panel.add( button );
}
}
public static void main( String ... args ) {
new ScrollTest().main();
}
}
I hope this helps.
One can also get a vertical scrolling for JPanel with SpringLayout. It's possible if panel's vertical size will be defined by setting a constraint SpringLayout.SOUTH. This can be done like this:
JPanel panel = new JPanel();
SpringLayout panelLayout = new SpringLayout();
panel.setLayout(panelLayout);
// Adding components to the panel here
// .....
// That's what defines panel's exact size and makes its scrolling possible
panelLayout.putConstraint(SpringLayout.SOUTH, panel, 0,
SpringLayout.SOUTH, lastComponentOfThePanel);
JScrollPane panelScrollPane = new JScrollPane(panel);
where lastComponentOfThePanel is a component at the bottom of a panel.
Hope this will help somebody. In my opinion, SpringLayout is very powerful layout manager, and sometimes it's very difficult or almost impossible to replace this one with GridBagLayout.
What about?
JScrollPane scrollPane = new JScrollPane(yourpanel);
container.add(scrollPane);

Categories

Resources