Using Swing, what is the best way to indent some components underneath a checkbox or radio button? I need to make something in the style of Firefox 3.6's Options->Privacy dialog where some checkboxes are indented under a "main" checkbox. I can use any of the standard AWT/Swing layout's including GroupLayout. I also have JGoodies FormLayout available to me. I tried using setLeadingColumn offset in FormLayout at first, but it seemed like it was not going to work well unless I was indenting under a Separator. Maybe I was just doing it wrong?
Is there anything like SWT GridLayout's horizontalIndent setting? That would be perfect.
I am working with JDK1.6.0_23.
Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.
There are several ways to do this:
Set each components border to: new EmptyBorder (0, 10, 0, 0).
Use a GridBagLayout and use an Inset (0, 10, 0, 0) to pad the left side.
Use a GridBagLayout and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.
Supply custom checkbox icons that have some empty space added to their left hand edges.
etc.
My advice would be to learn the GridBagLayout - it is somewhat unwieldy to use but it does give you pretty much all the layout power you could want. The JGoodies stuff is useful for when you want particular automatic column sizing behaviour that GBL won't give you without some additional code on your part.
You should be able to accomplish this just fine with FormLayout, just add another column for the sub items, and have the main item span 2 columns.
Another option is to use SpringLayout and add padding.
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 am trying to make something like the above, a frame that consists of many JButton components (gray rectangles). These buttons are of the exact shape but their positioning looks like the picture. I am new to AWT, Swing and such and don't know what layout should I use, I know a bit of GridLayout and BorderLayout which are obviously not suitable in this case.
Any ideas?
I think its better to use GridBagLayout. GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths. For further details, see How to Use GridBagLayout. For more details - https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#gridbag
I strongly recommend you to use Grid Bag Layout, with this layout you can make good looking GUI or strange GUI in most cases. In your problem Check insets in Grid Bag layout, this layout is kind of tricky but if you will learn it, you will see that it is simple.
For a project I have an almost working code, but I do not have the GUI. I want to make a screen that consists of clickable labels and has the following design:
I was thinking about first making the middle GridBagLayout with with a dimension of 6 by 2. Then 'wrapping' that up and adding the two buttons to the side, and then 'wrapping' that and adding the two buttons below.
I am inexperienced with Swing, and I have no idea how to start. I hope someone can give me some hints in the right direction.
Several approaches to a very similar layout using GridBagLayout and/or nesting are shown here. Consider using JButton for each clickable area, rather than JLabel. If you go with a nested layout,
Use BorderLayout for the enclosing panel.
Add buttons to EAST and WEST for the leftmost and rightmost areas.
Add a GridLayout(1, 2) of buttons to SOUTH for the bottom row.
Add a GridLayout(2, 6) of buttons to CENTER for the central twelve areas.
Addendum: A critical issue will be what you want the resize behavior to be.
as said in the comments above you could (should ?) use the WYSIWYG Window Builder plugin available for Eclipse; it's simple to use.
However, that doesn't answer your question, so to do so, here is how I would structure the UI if I were to make one like that :
http://www.hostingpics.net/viewer.php?id=902716gZHkK26.jpg
I basically use BoxLayout because that's the one I'm most familiar with. Every Rectangle is a JPanel. I think the image is pretty self eplanatory.
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 building a PropertyPanel. Currently I'm using a GridLayout to manage the JLabels and their corresponding fields where I can specify the value. But the problem is that the GridLayout automatically manages the size of the columns: it makes them the same width.
This means when I'm having a big value field, the colum, is getting bigger (which is good), but the other column (with all my JLabels) is getting bigger as well. Here is a screenshot:
< BAD
As you can see, the image property has a huge value, which makes both columns bigger, and I'm having a lot of space after the JLabels.
So, I'm searching for a LayoutManager which makes each column as big as necessary.
I want a layout like this (it's edited with Gimp):
< GOOD
Thanks
You can use SpringLayout for this. See How to Use SpringLayout.
Example layout:
Remember that you also can nest layouts.
SpringLayout is what I typically use for forms like this. Although I think GridBagLayout would also work nicely.
I tend to try to hack everything by mixing GridLayout and BorderLayout, so maybe it's not the best solution but...
Create two GridLayouts, both have a single column. One for the labels the other for the controls.
Now create a BorderLayout to be the parent.
Add the left grid to the BorderLayout.WEST and the right grid to the BorderLayout.CENTER.
While this was answered 11 hours ago, I just thought I'd pop in & make a suggestion. I suggest GroupLayout.
I was looking to break from nested layouts for a name/value dialog recently and looked at both GroupLayout & SpringLayout. It seemed the only advantage offered by SpringLayout was that it could achieve the right aligned text of the labels (there may be a way to do it using GL, but I couldn't figure out how). On the downside, the Java Tutorial examples for SpringLayout used a whopping 'helper class' to define layout constraints.
In the end (it was only a very short 'study') I chose to use GroupLayout.
Consider using MigLayout. If constrained within the current JDK, GridBagLayout.
Here's an overview of the standard LayoutManagers:
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
You could e.g. use GridBagLayout or the non-standard MigLayout, if you want to code the GUI by hand.
If you want to use a GUI builder (e.g. the one in NetBeans) you could use the GroupLayout.