I am kind of newbie em Java and right now I am trying to program a simple GUI. Recently, I was looking for some kind of GroupBox (for WindowBuilder if possible), like the "Search" and "Overview" Groupboxes in the following image: http://imagizer.imageshack.us/a/img812/7239/jxsv.jpg
So, which graphical object would be nice for it?
Take a look at using a TitledBorder that includes a lowered etched (EtchedBorder.LOWERED) border on a separate JPanel container using Swings BorderFactory. Read How to Use Borders
You would simply place the controls onto a JPanel with a TitledBorder.
Further reading: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html
Related
I have to create a multitext editor, and when you open the a new frame it should appear on top of all the others, but must remain on the same level so it could fall to the very back when needed. Whats the easiest way to do this? I have tried the moveToFront function but it doesn't seems to work.
Read the section from the Swing tutorial o How to Use Internal Frames for a working example.
The key would appear to the setSelected(true) method.
I am using the NetBeans 7.2.1 GUI Builder. I could do all of this by hand. Instead, I am using my current project to learn how this tool works so that I can make an informed decision of when to use it in the futre (if at all).
Now with help of archived questions here, I have figured out how to change the LayoutManager to a CardLayout. I have also added three JPanels to the layout (although, there seems to be a bug...maybe a question about that later). The first JPanel displayed by the CardLayout will have two buttons. Each button will cause the CardLayout to display one of the other two JPanels. To do this, I found that I can use CardLayout#show(Container, String).
I need to know what the value of the String is for each JPanel. Doing some further research, I found that NetBeans generates a line of code such as
getContentPane().add(addCardsPanel, "card2");
So I can use "card2" to show addCardsPanel. It would be convenient to use a more applicable String. Will NetBeans allow me to set this identifying String to whatever value I wish? If so, how do I do it?
In the Navigator window select the panel you want.
In the Properties window scroll down to the Layout group. You'll see a Card Name property. Knock your self out ;)
Is the above question possible? The effect I'm trying to achieve is similar to how MS Word displays "Document- Microsoft Word (Technical Preview)" in this picture link: http://img.blogsolute.com/ms-word-2010.png, but with a colored background.
You can set the title of any frame you create by passing the title string to the constructor of the JFrame. You can't, however, add any controls to the 'decoration' portion of the frame - i.e., the title bar.
What you probably can do, however, is create an undecorated frame, and manually add the decoration using customised Border objects. This effectively allows you to put any controls you like around the outside, and the root pane will happily work inside it.
Why do you need JLabel for that? You can use setTitle("") for this purpose
This is the code I'm dealing with: http://pastie.org/1501054 When you run this, for some reason, the two panels overlap. Why is this so? Any way I can fix it?
The ActionListener that is provided as an argument is irrelevant to this part of the program.
Also, where can I find a good swing tutorial that uses Eclipse?
I am not seeing overlapping panes when I run your code. I wonder if you can post a screenshot of the effect that you see.
As for tutorials in Eclipse. I would suggest using Windows Builder pro in Eclipse for building Java GUI. This is an excellent product (free) and very good documentation on the site.
While I'm not sure what you want the final result to look like, here's a few of my suggestions.
In the snippet where you add the warning panel to the JFrame
warningPanel.add(warningLabel);
contentPane.add(warningPanel);
pack();
contentPane being the container returned by JFrame.getContentPane()
JFrames by default use the BorderLayout, and so,
contentPane.add(<someComponent>)
is identical to
contentPane.add(<someComponent>, BorderLayout.CENTER)
You also add the mainPanel the same way, and you can't have two components with the same constraints, so instead set the warningPanel to BorderLayout.NORTH
contentPane.add(warningPanel, BorderLayout.NORTH)
And also remove the call to pack() in that code snippet, since you call it later on in your code.
Hope this helps.
PS
As for GUI building in Eclipse, this previous question on Eclipse GUI Builder plugins maybe of use. I can't speak for tutorials on using Swing in Eclipse, but a quick google search digs up this tutorial using the Eclipse Visual Editor project
When you say you want a good swing tutorial for eclipse, do you want a GUI builder or a tutorial on Swing? Swing is all about layouts. Once you get that down, it's a piece of cake. Just start with flow layouts.
If you want a GUI builder, use Netbeans. It's incredible!
As for the overlap, it's not an overlap, but an overwrite. You can only have one panel in BorderLayout.CENTER, the default location.
If you want them side by side, do getContentPane().setLayout(new FlowLayout());
Or just add(panel, BorderLayout.SOUTH)
Yes I see the warning label is over lapping the first row
That is because You didnt specify the layout for your contentpane.
Heres how you fix it
contentPane.setLayout(new GridLayout(2,1));
add this line under
Container contentPane = getContentPane();
and I think you should change your warning panel to gridlayout 1 and 1 not 0 and 1 because you still have 1 row and 1 column
In the Java Swing app I made it seems to me that all the component are too tightly packed.
In QT one can specify padding and margins for the layout.
Is there something similar for swing?
alt text http://img12.yfrog.com/img12/9612/screenshotscreenerconfi.png
Here is a screen shot of my application that I thing is too tight (is it? what do you think?.
Thanks.
Take a look to the GridBagLayoutManager. Its the most compex layout manager but everything can be acomplished whith it.
It uses the GridBagConstraintObject which has the inset property, it specifies the separation to the top, bottom, left and right components.
example: GridBagConstraintObject.insets.left=20
You could use MiGLayout as your layout manager. It allows all kinds of customizations, including margins/paddings.
You could achieve a much better layout for the example above by using DesignGridLayout in just a couple of lines of code (one per row in your layout). DesignGridLayout will automatically use the correct spacing for the runtime platform.
besides I would highly suggest that you DON'T use TitledBorders in your form because it prevents ANY LayoutManager (as advanced as it may be) from automatically aligning correctly the various components across different groups. Instead you could use a JLabel with a JSeparator (there are examples in DesignGridLayout, but this works with any other LayoutManager).
Since Java 1.6 swing there is a new GroupLayout manager that make this kind of works easier.
For instance there is a method: setAutoCreateGaps() that:
...you add two components to a SequentialGroup a gap between the two components is automatically be created...
For instance:
What LayoutManager are you using? Adding margins is quite easy, it depends however on the specific LayoutManager used.
FormLayout is another good layout manager. With a good GUI editor like JFormDesigner it makes GUI building easy enough. JFormDesigner actually automatically adds sufficient padding in most cases. I have to recommend against using GridBagLayout. It does the job alright, but is very complex which makes it difficult to use and maintain.