I'm really new to this. I'm still trying different examples.
Please correct me if I'm wrong. If I have the image below.
with
Button 1:
gridx = 0; grid y = 0;
Button 2:
gridx = 1; grid y = 0;
Button 3:
gridx = 2; gridy = 1;
and I want to hava a blank area or cell between button2 and button3. I thought maybe I do it with insets or is there any correct way of doing this?
I'm still learning so I'm following this guide. http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
I see components as objects in cells, how about the blank cells, what are they in gridbag layout perspective? how do I tell java that this is a blank cell? Should I always use the fill = GridbagConstraint.HOR.. ? I understand that widths are like spans or merged cells.
I hope you can better explain this. The things I haven't understood fully yet are weights and when I can extend a component further using gridx and gridy. Though I understand weights as spaces. I'm just looking for a practical explanation or comparison to html / excel
My questions are:
If i have 3 components on x's 0,1,2 will it not allow me to put the third component on like say, gridx=3 or gridx=5?
Given the 2 subpanels in the screenshot I included, how do i position the left panel to the upper left corner of top panel enclosing the 2 subpanels? Do i have to go straight to anchors??
Does the weighty always push other components up and weightx push left?
What is the most effective way of using fill()?
Related
I am working on java swing and I am stuck with a UI layout
My current output is as below.
I want to modify it a lil and add 2 text inputs in between as shown in sample below. Please help on how to achieve the text inputs side by side.
First of all: You should really give your components more meaningful names than jComboBox2.
Your example picture is not that easy to produce with GridBagLayout. You have to understand that the layout will create a n*m grid and you can put your components (like textfields, labels, comboboxes, etc.) freely anywhere inside that grid.
For example your jLabel4 is at the position 0/3 in the grid and though i'm not actually sure what a gridwidth of -1 does i'm pretty sure it's still at 0/3. If gridwidth was for example 3, your jLabel4 would span from 0/3 to 2/3.
So if you want to put something between those two rows, you'll need to put it at the right grid coordinates and give it the right width and height.
BUT: Sadly, getting it exactly as in your picture requires you to use some tricks (for example increase the grid width of the upper and lower components or add another panel containing the new row components instead of the components themselves).
Try to somehow make it work (even if it doesn't look exactly like your picture) without those tricks first as that might help you understand how GridBagLayout actually works. As soon as you really understand that, it should not be that difficult to recreate your picture.
Take a look at this image:
As you can see, I have a JSeparator between my "Auto Refreshing" JCheckBox and my "Show Column" menu, and my "Show Column" menu is wanting to be as far right as possible. Why is it not aligning itself to the left, like everything else before the JSeparator? And I can't seem to make it do so, here is my current code:
JCheckBox pulling = new JCheckBox("Auto Refreshing");
...
menuBar.add(pulling);
menuBar.add(new javax.swing.JSeparator(javax.swing.SwingConstants.VERTICAL));
JMenu showMenu = new JMenu("Show Column");
showMenu.setAlignmentX(Component.LEFT_ALIGNMENT);
menuBar.add(showMenu);
This tutorial might be helpful. A quote:
By default, most components have center X and Y alignment. However, buttons, combo boxes, labels, and menu items have a different default X alignment value: LEFT_ALIGNMENT.
So you can see that placement logic differs, in other words, don't count on it. However, I do not know why your manual alignment to left did not work. Most likely the problem is the size of your last menu. What you can do, is use glue as filler since JMenuBar has a BoxLayout.
menuBar.add(showMenu);
menuBar.add(Box.createHorizontalGlue());
This invisible space will be added to the end of your menu and it will push components before it the left.
The issue was the size of the JSeparator, it wanted to take up as much horizontal space as possible. So, my solution was to restrict it's size so that it could only be one pixel wide max:
JSeparator menuSep = new JSeparator(javax.swing.SwingConstants.VERTICAL);
menuSep.setMaximumSize(new java.awt.Dimension(1, 1000));
menuBar.add(menuSep);
I'm trying to create a CustomComponent where I have two columns (possibly four) where the two columns are rows of Labels. The first column is the identifier such as "Firstname: ", "Lastname: ", etc. and the second column is the actual value for the identifier such as "John", "Smith", etc.
I'm using a GridLayout so that the data is all line up nicely (for example John is lined up with Smith on the next line).
The problem I'm having is that I want the second column to take the remainder of the space width wise and have the text wrap to the next line when it's too long. So for example if I have a Comments Label that is a paragraph long I want the text to wrap but it just keeps going off the screen.
Now I understand that the Label needs to have a defined width to be able to wrap the text however I can't figure out how to use this to make the text in the labels in a grid wrap. I've tried everything I could think of.
My code is:
GridLayout gridLayout = new GridLayout();
gridLayout.setColumns(2);
gridLayout.setColumnExpandRatio(0, 1f);
gridLayout.addComponent(new Label("Firstname"));
gridLayout.addComponent(firstnameValueLabel);
gridLayout.addComponent(new Label("Lastname"));
gridLayout.addComponent(lastnameValueLabel);
// and so on.
VerticalLayout verticalLayout = new VerticalLayout();
verticalLayout.addComponent(myHeaderComponent);
verticalLayout.addComponent(gridLayout);
I've tried everything I could think of to have the valueLabel (firstnameValueLabel, etc.) text wrap but nothing seems to work. I've tried to assign specific sizes to the GridLayout, the VerticalLayout, rather than 100%, and so on but without any success. I am using a VerticalLayout because I have additional stuff above the data.
Perhaps using a GridLayout isn't the best option, maybe there's a better way to line up forms that aren't fields. Although FormLayout would be perfect it only seems to work with input fields.
In any case how should I implement this so that the Label text wraps?
The answer was a combination of things. First the biggest tip I can offer is to look at Consume available space in Vaadin Gridlayout, but consider line breaks
After that the code above was using setCaption() when it should've been using setValue().
Combining these two things resolved the issue and the text wrapped.
Do
int colums = 2;
int rows = 0;
GridLayout gridLayout = new GridLayout(colums,rows);
hope that helped
I have a JPanel with GridBagLayout set. It has 2 columns and 1 row (2 cells). Every cell contains one JPanel which contains one JLabel (type of component is insignificant).
The JLabel in the left cell has width attribute set to 100px. The JLabel in the right cell has width attribute set to 50px. In such case the left cell extends a little and it's wider than the right cell.
I thought that GridBagLayout expands cell only when contained components take too much space.
Is it a GBL bug?
Any idea how I can solve this problem?
Any idea how I can solve this problem?
Java Swing does not work the same way as CSS/HTML. You create the components in Swing and let the GUI worry about the sizing.
Without knowing what you're trying to accomplish, all I can do is say define the Swing components and let the GridBagLayout manage the layout.
If you want the grid areas to be the same size, you would use the GridLayout.
I am afraid this is a difficult question to answer because GridBagLayout doesn't arrange it's columns and rows this way. If you have two columns and two components the columns will size themselves as the sizes of the largest components put into those columns. So your left column is bigger, because your left component is bigger.
You can adjust how much of any spare space a cell takes by adjusting your component's corresponding GridBagConstraints attributes. The weight attributes (weighty & weightx) control how much of any spare space the columns (weightx) and rows (weighty) take up. If for example your JPanels were using GridBagConstraints objects called rightGbc and leftGbc you could use the following code to alter their relative sizes.
leftGbc.weightx = 0.5;
rightGbc.weightx = 1;
This means that the right column will take up twice as much spare space as the left column and therefore (hopefully) make up for the difference in the size of your components.
Using the weight attributes can feel like a very abstract process, and it can take a while to get used to them, but once you start using them for a while you will get it. Depending on which components you are using, there are other quirks which can effect how much affect the weight attributes have on the columns/rows.
I'd like to achieve the following:
The first column 20% of the total width and the second the 80%. And it should be dynamic(ex. when i expand/shrink it should change accordingly like liquid layout in css)
MigLayout is your best friend: http://www.miglayout.com
The code would look like this:
JPanel panel = new JPanel(new MigLayout("wrap 2, fill", "[fill,20%][fill,80%]"));
panel.add(panel1);
panel.add(panel2);
For Swing you'd use a gridbag layout.
You have two columns, one row.
The gridwidth properties will be set so that the first column's is .2 and the second column is .8 or any set of numbers such that the first column's value is 1/4 the second column's value.
The gridheight for both columns should be the same.
You can experiment a bit with fill. If you don't mind space in your UI which is not filled with a component, then use none. If you want them to resize nicely but keep the .2 to .8 ratio then try horizontal and see if that keeps the proper ratio automatically.
If it doesn't then try setting weight to .2 for the first column and .8 for the second. Yo're trying to keep that.2 to .8 ratio no matter how big the JPanel is made by the user.
Let me know if you need more help.
If you are asking about desktop java application (Swing based), you can use GridBagLayout - a standard layout comes with JDK. Although it is somewhat hard to understand.
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
The best solution would be making your own layout - it's not that hard at all.
GridBagLayout is more feared than understood.
I had worked on an application with most screens having a header footer and a left navigation panel. I used Gridbag layout then and only that gave the required behaviour during resize. Had used visualcafe.
Your two column requirement seems to be a good one to start using Gridbag since only a couple of gridbag constraints will be affected.
I suggest to use a tool to build the UI if you have more rows/colums.