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.
Related
Okay, I am kind of desperate right now. I hope you guys can help.
I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.
I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content
|<---- 30% ------->|<----- 70% -------------------->|
Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout).
If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.
Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.
You can achieve this by using nested BorderLayouts. Start by setting your Panel's layout as BorderLayout.
After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.
Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.
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 have a component that is inserted into the main content pane, and for some reason, it is not filling the area.
getInstance().add(internalPanel, LayoutFactory.newFactory().setX(1).setY(1).
setWeightX(1).setWeightY(1).setFill(GridBagConstraints.BOTH));
I made the LayoutFactory specifically for GridBagLayout, it just sets the property and returns the object.
Basically, the problem is that when I add the component to the content pane, it does not fill the space dedicated for it. It takes the absolute minimum, as if weightX and weightY were set to 0. I have tried to setAnchor(GridBagConstraints.FIRST_LINE_START) and setAnchor(GridBagConstraints.NORTHWEST). Neither have any visible effect on the program.
Also, when I set the border of the component, it only fills part of the space, so it is not a problem with the component I am adding to it.
It is worth noting that the component shows up in the centre of the space, and does not follow the anchor. From here, I figured my problem was the weightXandweightY properties. Looking at the other components of the content pane, the weight is set on every one of them.
getInstance().add(MainFrame.buttons, LayoutFactory.newFactory().setX(0).setY(1).
setWeightY(1).setWeightX(0).setFill(GridBagConstraints.BOTH));
and
getInstance().add(panel, LayoutFactory.newFactory().setX(0).setY(0).setWidth(2)
.setWeightX(1).setWeightY(0).setAnchor(GridBagConstraints.FIRST_LINE_START)
.setFill(GridBagConstraints.NONE));
It's certainly possible that something is going wrong somewhere else, but I didn't want to swamp this post with text. If you need more information to figure this out, I'd be more than glad to provide it. Hopefully this made sense. I've been using GridBagLayout for a while now and I've never run into something like this.
Here is an image of what it looks like in the space it is supposed to be
EDIT: Found out the problem lays somewhere else, as making a small example program works fine. Since I am a little stumped, I uploaded a full version of the program I am working on. Don't worry about what it actually does, I just care about the workload panel not filling the space.
Solution to problem :
Was adding component to InternalPanel type object, which had no layout manager. (Defaulted to flowLayout)
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.
Is there a simply layout manager I can use in a JPanel to create something akin to a bar chart? FlowLayout almost meets this need. The added component orientation needs to be left to right (default for FlowLayout), but they need to "rest" on the bottom of the panel with excess space at the top (not available in FlowLayout). Also, the components will all the be the same height and width.
Thanks.
A BoxLayout will do the trick as demonstrated in this posting
If you are going to do something like a bar chart, you might want to consider not using Components at all. Just have a single JComponent that overrides (IIRC) paintComponent. It'll be easier to do the calculations in a manner appropriate to a bar chart rather than trying to use an inappropriate layout manager abstraction.
FWIW, I default to GridBagLayout, even if a simpler layout manager will do, on this basis that the code can be more consistent.
You can do exactly what you want in GridBagLayout. Yes, I know everyone hates GBL; yes, I know I'll get down-voted. But it really is not difficult to understand and you can use it for almost any layout goal.
The trick to get a component to "stick" to the bottom is to use the anchor and fill properties of the GridBagConstraints object properly (i.e. SOUTH and NONE)
A BoxLayout might work for you. It lets you layout components left-to-right or top-to-bottom, with the tightly coupled Box class to force spacing constraints.
I actually prefer the FormLayout, since it is very flexible but you have to write a lot of code though. And in the beginning its a little bit confusing with its percentage and pixel parameters.
But you can for example tell a control that it is 5 pixels left of another control (thats the main part...it layouts controls in relation to neighbors), then it takes 100% of the lasting space availabel including a border space of 5 pixels (you need to use -5 then).
I think it looks somewhat similar to this
FormData data = new FormData();
data.left = new FormAttachement(neighborControl, 5);
data.right = new FormAttachement(100, -5);
...
button.setLayoutData(data);
This example is for JFace, but there are Swing implementations as well.
I will look up my old code later this day to check if the code I wrote is right :)
HereĀ“s a additional link