Choosing a layout manager - java

I'm trying to set up a couple of labels and textfields in a table-like style.
Currently I'm using the GridLayout, which kind of works as I want it to. However, the TextFields and Button is expanding to the full size of the cell.
How can I make the TextFields and Button "normal" sized, and what Layout Manager would accomplish this the easiest way?
Here's the current code and a screenshot:
JPanel forms = new JPanel();
forms.setLayout(new GridLayout(3, 2));
JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
JTextField txt_navn = new JTextField();
JLabel lbl_adresse = new JLabel("Adresse:");
JTextField txt_adresse = new JTextField();
forms.add(lbl_navn);
forms.add(txt_navn);
forms.add(lbl_adresse);
forms.add(txt_adresse);
forms.add(Box.createRigidArea(new Dimension(10, 10)));
forms.add(new JButton("Opret virksomhed"));

I suggest you use gridBagLayout:
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
As an example:
JPanel forms = new JPanel();
forms.setLayout(new GridBagLayout());
JLabel lbl_navn = new JLabel("Virksomhedsnavn:");
JTextField txt_navn = new JTextField();
JLabel lbl_adresse = new JLabel("Adresse:");
JTextField txt_adresse = new JTextField();
//Setting grid bag constraints
GridBagConstraints c = new GridBagConstraints();
// Grid position coordinates
c.gridx = 0; c.gridy = 0;
//Align panel in top-left corner
c.anchor=GridBagConstraints.LINE_START;
forms.add(lbl_navn, c);
c.gridx = 1; c.ipadx = 195;
forms.add(txt_navn, c);
c.gridy = 1; c.gridx = 0; c.ipadx = 0;
forms.add(lbl_adresse, c);
c.gridx = 1; c.ipadx = 195;
forms.add(txt_adresse,c);
c.gridy = 2; c.gridx = 0; c.ipadx = 0;
forms.add(Box.createRigidArea(new Dimension(10, 10)),c);
c.anchor=GridBagConstraints.CENTER;
c.gridy = 2; c.gridx = 1;
forms.add(new JButton("Opret virksomhed"),c);
Hope this helps.

I think you can refer to this question where useful answers have been given:
JTextField Fixed Height

I read good things about Mig layout a while ago, I never used, but seems very powerful.
Check this Mig layout, may be a good solution for your problems.
UPDATE:
Mig layout is not a "standard" layout from sun/oracle.
GridBagLayout as mentioned, can accomplished your objectives(from experience). I personally when I want fixed sizes components I use to work with "null layouts".

Related

Java GridBadLayout positioning

I would be grateful for any help/suggestion regarding my problem. I attached the image of my simple program which shows that the positioning of the components seems a bit off. My question is - why the ComboBox From... as well as TextField Enter value here... start so far off of the left corner? I've given gridx=0 so it positions the component on the very edge of the window, but components start some pixels off from the edge. How can I fix it?
Also, what do I need to do/consider to remove dependency of the rows on each other? I mean how to position components anywhere I want in one row without effecting the position of other components in another row. Thank you!
Piece of Code:
JPanel container = new JPanel();
container.setLayout(new GridBagLayout());
getContentPane().add(container, BorderLayout.NORTH);
TitledBorder outputCenter;
GridBagConstraints c = new GridBagConstraints();
label = new JLabel("Choose measure system to convert");
label.setFont(new Font("Times New Roman", Font.PLAIN, 20));
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
c.insets = new Insets(10, 0, 20, 0);
container.add(label, c);
fromList = new JComboBox<String>(convertFrom);
c.gridx = 0;
c.gridy = 1;
c.gridwidth =1;
c.ipadx = 20;
c.anchor = GridBagConstraints.LINE_START;
container.add(fromList, c);
toList = new JComboBox<String>(convertTo);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.ipadx = 20;
container.add(toList, c);
//Field where user enters the value to be converted
input = new JTextField("Enter value here...");
input.setPreferredSize(new Dimension(150,30));;
input.setEditable(true);
input.setBackground(Color.WHITE);
input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
input.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
input.setText("");}});
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.ipady = 20;
container.add(input, c);
//The area where the output/result is shown
output = new JTextArea(10,30);
output.setEditable(false);
output.setFont(new Font("Serif", Font.BOLD, 12));
output.setBackground(Color.WHITE);
outputCenter = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Output");
outputCenter.setTitleJustification(TitledBorder.CENTER);
output.setBorder(outputCenter);
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(50,5,10,10);
c.gridwidth = 3;
container.add(output, c);
//Convert button
convert = new JButton("Convert");
c.gridx = 0;
c.gridy = 3;
c.ipadx = 50;
container.add(convert, c);
}
Output:
Remember, GridBagLayout is a "flexible grid" layout manager. It still relies on the concept of rows and columns, but each row and column has it's own size, based on the requirements of the components and the constraints applied to them.
This means, that you combo box is been aligned to the left position because of a combination of c.anchor = GridBagConstraints.LINE_START and the space requirements of the JTextField sharing the same column.
You "could" change c.anchor = GridBagConstraints.LINE_START to c.anchor = GridBagConstraints.LINE_END, while will (in your case) align the combo box to the right edge of the column, for example...
Another solution would be to use a combination of containers to reduce the overall complexity of the layout. For example, you could add both the combo boxes to their own container, managing the layout requirements for them in an isolated manner and then layout that container within the large scheme of things

GridBag Layout, positioning the pieces

What I want is the label and check box at the top left corner and the three buttons on the bottom right corner.
However, it doesn't appear the the anchors are working properly.
Result:
Code:
JPanel bottomPanel = new JPanel( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0, 0, 0, 20);
c.anchor = GridBagConstraints.NORTHEAST;
bottomPanel.add(spinachLabel, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
bottomPanel.add(checkbox, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 5;
c.weightx = 0.5;
c.insets = new Insets(0, 0, 0, 5);
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(applyButton, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 5;
c.weightx = 0.5;
c.insets = new Insets(0, 0, 0, 5);
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(refreshButton, c);
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 5;
c.weightx = 0.5;
c.anchor = GridBagConstraints.SOUTHWEST;
bottomPanel.add(cancelButton, c);
return bottomPanel;
First, we need to clarify what GridBagConstraints.anchor does: It specifies the placement of a component within the GridBagLayout cell.
spinachLabel is being placed at gridx = 0. applyButton is also being placed at gridx = 0. Therefore, they are guaranteed to be placed in cells which are in the same column. Their respective anchor constraints can move their position within their cells, but cannot move the cells themselves.
Second, you have not set any weighty constraints. Whenever a container which uses a GridBagLayout is larger than the preferred sizes of its child components, it uses the GridBagLayout’s weight constraints to decide which cells will grow to take up that extra space. When there are no weight constraints at all, as is the case for the vertical dimension in your layout, GridBagLayout doesn’t give any of the cells that extra space, and instead centers them. That’s what you’re seeing: Since no cell has a weighty set, all the cells are vertically centered.
In summary, your components will never appear at the top and bottom, unless you set some positive weighty constraints.
This doesn’t seem like a good use of GridBagLayout. When you want to place components at the edges, BorderLayout is usually a better choice:
JCheckBox checkbox = new JCheckBox("Enable Spinach Study");
JButton applyButton = new JBUtton("Apply");
JButton refreshButton = new JBUtton("Refresh");
JButton cancelButton = new JBUtton("Cancel");
JComponent buttonPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonPane.add(applyButton);
buttonPane.add(refreshButton);
buttonPane.add(cancelButton);
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(checkbox, BorderLayout.PAGE_START);
bottomPanel.add(buttonPane, BorderLayout.PAGE_END);
(A JCheckBox should always have text, rather than being placed to the right of a label. That way, the user has a much larger mouse target, and your user interface is accessibility compatible.)

Does GridBagLayout require placeholder panels for empty cells?

I made a simple GridBagLayout which adds buttons in the cells (0,0), (1,0), and (0,1).
JPanel panelMain = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panelMain.add(new JButton("0,0"),c);
c.gridx = 1;
c.gridy = 0;
panelMain.add(new JButton("1,0"),c);
c.gridx = 0;
c.gridy = 1;
panelMain.add(new JButton("0,1"),c);
I was happy to see the resultant UI:
I want to add a JButton in a cell that is not connected to the existing cells. I want it to be separated by an empty space. When I try this, the new JButton is lumped in next to the others. Here is the addition:
JPanel panelMain = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
panelMain.add(new JButton("0,0"),c);
c.gridx = 1;
c.gridy = 0;
panelMain.add(new JButton("1,0"),c);
c.gridx = 0;
c.gridy = 1;
panelMain.add(new JButton("0,1"),c);
c.gridx = 3;
c.gridy = 0;
panelMain.add(new JButton("3,0"),c);
The output:
The JButton("3,0") is displaying at the cell (2,0). Do I need to use an empty JPanel as a place holder in the cell (2,0)? More importantly, why is this happening?
The layout does not know what should be the width to be left at 2,0 unless there is a component placed in the gridx = 2. By the time you complete the UI, if any component gets placed, it should look fine. For e.g.:
In other case you may add empty JPanel with background color matching to the background color of the container.
Do I need to use an empty JPanel as a place holder in the cell (2,0)?
You can use an "insets" grid bag constraint to give space between components. Read the Swing tutorial on How to Use GridBagLayout for more information on the inset constraint.
Or, if you want to have a place holder then you can use a Box.createHorizontalStrut(...) to easily specify the width.
More importantly, why is this happening?
Cells don't have a size unless there is a component in the cell. Each cell is independent of one another so what size would you expect cell (2,0) to be?

gridheight moves component on top of each other

I have a JFrame which consists mainly of a left and right side. The left has 2 components 1 above the other, and the right has 9, again 1 on top of each other. As the left has 2, I want 1 component to equal the same vertical space as 6 on the right. I am using the gridBagConstraints layout and have each JPanel positioned in the main JFrame. Everything looks OK (apart from what I was just saying). To sort this problem I use c.gridHeight = 6 on the JPanel on the left. However this then puts the 6 JPanels which are on the right ontop of each other, ignoring their formatting. How can I rectify this problem? Snipets of my code which still cause the same problem are:
void create()
{
JFrame screen = new JFrame("Dungeon of Doom");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
screen.setSize(new Dimension(screenWidth, screenHeight));
screen.setPreferredSize(new Dimension(screenWidth, screenHeight));
//Add all JPanes to screen
c.gridy = 0;
c.gridx = 0;
c.gridheight = 6;
screen.add(lookReply(), c);
c.gridy = 0;
c.gridx = 1;
screen.add(title(), c);
c.gridy = 1;
c.gridx = 1;
screen.add(space(), c);
c.gridy = 2;
c.gridx = 1;
screen.add(commands(), c);
//...So on, same for others
}
A sample method - the JPanel - all methods have the same content just different titles and less/more buttons/labels
private JPanel title()
{
JPanel titlePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
titlePanel.setBackground(Color.red);
titlePanel.setOpaque(true);
JLabel title = new JLabel("DOD");
titlePanel.add(title, c);
return titlePanel;
}
I put a background on the JPanels just to help see where they are in the window
All help appreciated! Thanks :)

GridBagLayout - Height of one row causes the width of next row to change

The UI I am working on displays a panel which lets a user select a movie and play. There are controls to play, pause, etc.
The layout seems to look the way I want. The panel uses a GridBagLayout. Row 2 displays a text area for status messages and row 3 displays a panel with buttons and a progress bar.
The problem I am running into is that when I have too many lines of text in the text area, the buttons in row 3 wrap around. This is irrespective of the height of the outer frame.
The height in row 2 is affecting the width in row 3. I don't understand this behavior. I am wondering if someone can tell me what is it that I am doing wrong and how I can fix it? I have attached the code.
On a slightly different topic, if you are looking at the code, can you also suggest a way to leave a margin between the bottom-most component and the outermost panel?
Thank you in advance for your help.
Regards,
Peter
private static JButton CreateImageButton(String fileName) {
JButton retVal = new JButton("xxx");
return retVal;
}
public MoviePanel() {
this.setLayout(new GridBagLayout());
this.setBackground(Color.WHITE);
JButton btnRefresh = CreateImageButton("refresh.png");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill = GridBagConstraints.NORTH;
c.insets.left = 10; c.insets.right = 10; c.insets.top = 10;
this.add(btnRefresh, c);
JComboBox cbMovieList = new JComboBox();
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.right = 10; c.insets.top = 10;
c.weightx = 1.0;
this.add(cbMovieList, c);
JButton btnAuthorize = new JButton("Get Info");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.insets.top = 10;
this.add(btnAuthorize, c);
JTextArea txtInfo = new JTextArea();
txtInfo.setFont( new Font("SansSerif", Font.BOLD, 12));
txtInfo.setBackground(Color.cyan);
// txtInfo.setText("abc\ndef");
txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.NORTHWEST;
c.weighty = 1.0;
c.insets.top = 10;
this.add(txtInfo, c);
JPanel controllerOuter = new JPanel();
controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS));
controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder());
FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER);
controllerLayout.setHgap(0);
JPanel controller = new JPanel(controllerLayout);
controller.setBorder(new EmptyBorder(10, 10, 10, 10));
Dimension dim = new Dimension(60, 40);
JButton btnPlay = CreateImageButton("play.png");
btnPlay.setPreferredSize(dim);
controller.add(btnPlay);
JButton btnPause = CreateImageButton("pause.png");
btnPause.setPreferredSize(dim);
controller.add(btnPause);
JButton btnStop = CreateImageButton("stop.png");
btnStop.setPreferredSize(dim);
controller.add(btnStop);
JButton btnForward = CreateImageButton("forward.png");
btnForward.setPreferredSize(dim);
controller.add(btnForward);
JComboBox cbAspectRatio = new JComboBox();
cbAspectRatio.setPreferredSize(new Dimension(100, 40));
cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0));
controller.add(cbAspectRatio);
controllerOuter.add(controller);
JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setPreferredSize(new Dimension(350, 40));
pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10));
pbProgress.setValue(50);
pbProgress.setString("50/100");
pbProgress.setStringPainted(true);
pbProgress.setForeground(Color.BLUE);
pbProgress.setBorderPainted(true);
controllerOuter.add(pbProgress);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
c.weightx = 1.0;
this.add(controllerOuter, c);
}
I see several things in your code:
You force the preferredSize of the JButton's. If possible, I would remove that because this will often get you more problems than solutions. If you want to force the preferredSize, you should also pay attention to set the minimum and maximum sizes as well, otherwise you get weird behaviour like the one you are observing
You use a BoxLayout to display the controls. While this is perfectly acceptable, BoxLayout also relies on min/max size to perform the layout, which you did not set.
You use imbricated layouts. This is fine too, but why not use only the GridBagLayout of your MoviePanel?
Usually TextAreas are wrapped in JScrollPane, in case the text is too big. You can also setLineWrap(true) on the TextArea, so that it does not go too far on the right. By setting rows/columns on the TextArea, you will define its preferreSize (to prevent it from depending of the text it contains).
On your GridBagConstraints, the fill property can only be: NONE, VERTICAL, HORIZONTAL or BOTH (You used VERTICAL for one of them). Also, it is not needed to recreate a new instance, you can reuse the same GridBagConstraint over and over, it is automatically cloned by the LayoutManager when you set the constraint for the component.
Now for the solutions, I found several:
When you add the contollerOuter, also specify c.fill = GridBagConstraints.HORIZONTAL; (This is the easiest way to solve your issues)
When you set the preferredSize of the JButtons, also force their minimumSize to the same value.
Use only the GridBagLayout to layout all components. (This would be my favorite)
Replace the FlowLayout by a BoxLayout with a X_AXIS.
Rember that GridBagConstraints properties :
gridx, gridy: specifies the location
gridwidth, gridheight: specifies the colspan/rowspan
weightx, weighty: specifies who gets the extra horizontal/vertical space and in what proportion
anchor: specifies the alignement of the component withing its "cell", if the "cell" is bigger than the component
fill: specifies if the component should stretch to the cell width/height
Just adding one JPanel each for Center and Bottom will do the trick for you, so till your JTextArea your GridBagLayout will server the purpose and after that the BorderLayout of the MAIN JPanel will do. Moreover, adding JScrollPane also to the whole thing reduces the effort needed at other areas. Have a look at the code and output :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class JTextPaneExample extends JPanel
{
private Icon info = UIManager.getIcon("OptionPane.informationIcon");
private Icon error = UIManager.getIcon("OptionPane.errorIcon");
private static JButton CreateImageButton(String fileName) {
JButton retVal = new JButton("xxx");
return retVal;
}
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JTextPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setBackground(Color.WHITE);
JPanel centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
centerPanel.setLayout(new GridBagLayout());
centerPanel.setBackground(Color.WHITE);
JButton btnRefresh = CreateImageButton("refresh.png");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
c.fill = GridBagConstraints.NORTH;
c.insets.left = 10; c.insets.right = 10; c.insets.top = 10;
centerPanel.add(btnRefresh, c);
JComboBox cbMovieList = new JComboBox();
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets.right = 10; c.insets.top = 10;
c.weightx = 1.0;
centerPanel.add(cbMovieList, c);
JButton btnAuthorize = new JButton("Get Info");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
c.insets.top = 10;
centerPanel.add(btnAuthorize, c);
JTextArea txtInfo = new JTextArea();
txtInfo.setFont( new Font("SansSerif", Font.BOLD, 12));
txtInfo.setBackground(Color.cyan);
// txtInfo.setText("abc\ndef");
txtInfo.setText("abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\nyz");
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(txtInfo);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0;
c.insets.top = 10;
centerPanel.add(scroller, c);
JPanel controllerOuter = new JPanel();
controllerOuter.setLayout(new BoxLayout(controllerOuter, BoxLayout.Y_AXIS));
controllerOuter.setBorder(BorderFactory.createRaisedBevelBorder());
FlowLayout controllerLayout = new FlowLayout(FlowLayout.CENTER);
controllerLayout.setHgap(0);
JPanel controller = new JPanel(controllerLayout);
controller.setBorder(new EmptyBorder(10, 10, 10, 10));
Dimension dim = new Dimension(60, 40);
JButton btnPlay = CreateImageButton("play.png");
btnPlay.setPreferredSize(dim);
controller.add(btnPlay);
JButton btnPause = CreateImageButton("pause.png");
btnPause.setPreferredSize(dim);
controller.add(btnPause);
JButton btnStop = CreateImageButton("stop.png");
btnStop.setPreferredSize(dim);
controller.add(btnStop);
JButton btnForward = CreateImageButton("forward.png");
btnForward.setPreferredSize(dim);
controller.add(btnForward);
JComboBox cbAspectRatio = new JComboBox();
cbAspectRatio.setPreferredSize(new Dimension(100, 40));
cbAspectRatio.setBorder(new EmptyBorder(0, 10, 0, 0));
controller.add(cbAspectRatio);
controllerOuter.add(controller);
JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setPreferredSize(new Dimension(350, 40));
pbProgress.setBorder(new EmptyBorder(0, 10, 10, 10));
pbProgress.setValue(50);
pbProgress.setString("50/100");
pbProgress.setStringPainted(true);
pbProgress.setForeground(Color.BLUE);
pbProgress.setBorderPainted(true);
controllerOuter.add(pbProgress);
add(centerPanel, BorderLayout.CENTER);
add(controllerOuter, BorderLayout.PAGE_END);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new JTextPaneExample().createAndDisplayGUI();
}
});
}
}
Here is the output as you add more lines :

Categories