of course sorry for my bad English. I'm working on a window in which I have a panel and the panel within a form. The panel uses the distribution manager "GridBagLayout" I could properly distribute the components. My problem is that when filling a text field, type in the (JTextField) enlarges the field and does not retain its size. As you can see from the picture the more letters introduce the "Brand" field is becoming bigger. I'm working with NetBeans if someone happened or know what can be the problem Thanks in advance. Cheers
I am not sure about how to do this with a window editor like they have in NetBeans, but for GridBagLayout the power comes with how you configure your GridBagConstraints when you add another Component. If you want to keep every Component on a given row to keep their ratios fixed, then you need to define each of their GridBagConstraints.weightX fields to sum up to 1. So if you want something like:
|Label|TextField|Blank|
I would add Label with GridBagConstraints.weightX=0.3, TextField with GridBagConstraints.weightX=0.3, and a final "empty" Label("") as a buffer with GridBagConstraints.weightX=0.4. Total = 1.
There are of course tutorials available GridBag Tutorial
Related
i'm trying to make a UI panel that serves like a form (Im not using GUI designer). Left side contains labels, and the right side contains text field, etc. I want to make it, so that when resized, only the right size (the text fields, change shape).
Currently, i am using GridLayout(x, 2) with one JLabel and JTextField per row.
So on resize, i wanna make it so that this goes
from [(JLabel)|(JTextField)]
to [(JLabel)|( JTextField )]
not [( JLabel )|( JTextField )].
Plus, of course, i need to make sure that all of the rows always match.
If you want to use a single layout manager you could use a GridBagLayout.
For the components in the second column you would set the weightx constraint to 1.0f. This means any extra space will only be allocated to this component.
Read the section from the Swing tutorial on How to Use GridBagLayout. The topic on Specifying Constraints will explain in more detail how this constraint works.
GridLayout can not be used for this. Use GridBagLayout or a easier to use layout manager, like MigLayout.
I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".
I have a JPrame and it it set to borderLayout. Then i created another JPnael which uses BoxLayout. Now i have added labels(which contain text) and textFields to the JPanel. I have also setLayout of JPanel to WEST. Now i dont know why, but I have two problems with what is being displayed.
1) The labels are all indented 4-5 tab spaces, this is so random I dont even know why it is tabbed. All the labels in this panel are like this. Please note that this only happens if I add more stuff to the JPanel. If I only have 1 label in the JPanel it is correctly alligned to the west corner of the screen. Does anyone know why they get indented when i add more elements to the JPanel?
2)The Textfields size are huge, as in they take up many lines. I have set the size of the text fields only to 20. Yet they take up like 5 lines. Why is it not just a single line?
Im sorry guys, I have been trying to fix this for the past 2 hours and dont know what is causing this issue. I would post code, but this is an assignment and I dont feel comfortable posting it on to the internet. I hope you understand.
Just to make things clear, I have a JPanel for example called "aPanel" which is set to BorderLayout then I have another JPanel called 'subPanel' which uses boxlayout and set the layout of that to be west. After this I add stuff to this 'subPanel' expecting the elements to stack over eacher other towards the left border of the JFrame.
1) Read the section from the Swing tutorial on Fixing Alignment Problems. When you add different components to the panel they may have different "X alignment" so you get a different layout than you expect.
2) The box layout will expand to fill the available space. So you need to override the getMaximumSize() method of you text fields to return the preferred height of the text field and the maxiumum width of the text field.
Edit:
Just read the last part where you mention a FlowLayout. If this is what you are using then read the FlowLayout API. It has a parameter that controls left/center/right alignment of components added to the panel.
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.
I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.