Using CardLayout in NetBeans GUI Builder - java

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 ;)

Related

Eclipse WindowBuilder - text doesn't fit the JLabel

I've created a custom JPanel, added JLabel and type some text in it, and made the JLabel as small as possible, so that the text fits it. I'm using the absolute layout.
However, when I use it in my JFrame, the text no longer fits the JLabel, even though it did in the designer. Is there any particular reason for this? Or is it a bug?
Don't use the absolute flow layout. That's why you get this problem - let a real layout manager handle the size of your component instead of setting defaults.
There are some good tutorials around.

Java SWING - How to put my functionality under my tabs?

At the Moment I am coding my GUI, but my programm functionality flows under my tabs.
Example:
Does anybody has an idea how to fix that?
Your Layout is broken.
Try to split up component-parts (tabs and the panel content below tabs).
Add only these parts and see whether the layout of those particular components is OK.
Put everything you have into a scrollable would help you to analyse the problem and see the actual required size of your components.
Try to avoid using prefferedSize/minSize/maxSize in your code, it was not meant to layout your stuff.

java swing- Possible to add JLabel next to application icon/title?

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

Two JPanels overlapping?

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

Need help with Layouts in Swing

I'm using Java Swing and I have the following problem:
I have a class TnaiPanel that extends JPanel. In this class I am creating 3 components and then lay them out in a horizontal line using a BoxLayout.
Also, I have a class TnaimDinamimPanel that also extends JPanel. This class contains multiple occurances of TnaiPanel, layed out vertically using a BoxLayout.
Also, I have a class MainFrame that extends JFrame. This frame contains a menu-bar and one main panel. The main panel can change (when choosing a certain menu-item, I create a new panel and set it to show as the main panel of the frame).
Now, for some reason I get "BoxLayout can't be shared" when I add the newly created TnaimDinamimPanel to the components of the frame.
I don't mind using different layout objects.
The result I want to get is a sort of "table" of components, where each TnaiPanel will have fixed component sizes and spacing, essentially serving the role os a "row" in the "table".
Thanks,
Malki.
You probably create only a single BoxLayout instance. Create a new one each time you need one (i.e. one per TnaiPanel, one per TnaimDinamimPanel and probably one per MainFrame).
To answer the second part of your question, ie "having a table of components", I would say that you can't do it with different panels, except if you start setting the individual min, pref and max sizes of components and panels, which is highly unadvised.
If you need to have correct alignment as in a table of components, then you need to put all your components in one panel, which also means you need to use only one layout. However, the only default swing layout that can allow you to do what you want is the GridBagLayout. Actually the GroupLayout (java 6) would also fit the bill but it absolutely requires a graphical designer (eg the one within netbeans).
If, like me, you are allergic to builders, then you'd better use one 3rd-party LayoutManager that is intended to be use programmatically (I would not consider GridBagLayout to be in this category although I have already used it that way in the past).
MigLayout (as suggested by Skeptic) is one option. Another option is DesignGridLayout which might fit your purpose better and is easier to use than MigLayout.
Try MiGLayout instead.

Categories

Resources