Eclipse WindowBuilder - text doesn't fit the JLabel - java

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.

Related

suggestions on designing a JFrame

I am designing a Jframe using netbeans. I do have few questions.
Can we create a label for a field in a desired location(For eg.,we have a field named height, I need to display a label below it indicating height is in cm) conditionally?
Can we disable a field based on a condition?(by disable I mean it shouldn't be displayed in my frame)
Can someone suggest me whether we can achieve them through some examples.
Tried this, after some helpful suggestions
private void englishRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JLabel userlabel;
if (englishRadioButton.isSelected())
{
userlabel = new JLabel("Inches");
userlabel.setBounds(311, 59, 64, 36);
//userlabel.setLocation(307,55);
//userlabel.setSize(70,40);
userlabel.setForeground(Color.green);
userlabel.setText("Inches");
userlabel.setVisible(true);
System.out.println(englishRadioButton.getBounds());
inchesTextField.setVisible(true);
}
}
The textfield is visible only when I click the English radio button,at the same time I need to get a label but it's not displayed with the above code. Can I know where I am going wrong?
Please see the attached screenshots
When English button is clicked, I need a label beneath the second textfield as inches, I am disabling the text field when Metric is displayed. I am able to achieve the later one but not the former one
Thanks!!
Yes, relative placement of components is easily achieved with use of layout managers.
Yes, all components have a setEnabled(...) and a setVisible(...) method either of which can be called at any time during a program's run. The former helps you activate/inactivate components and the latter helps make them visible/invisible. If you want to swap complete "views", use a CardLayout.
Regarding:
Can someone suggest me whether we can achieve them through some examples.
Please, you first as I strongly believe that the onus of effort here should be yours, the questioner's, since you're the one asking the questions, and the one with the most to learn by coding as much as possible. Let's see your attempts and we can help you with them. Otherwise the best examples are to be found at the Swing Tutorials.
For links, please look here: Swing Tag Info.
Edit
You ask:
I tried the above posted code,conditionally disabling the text field works well but getting a label doesn't work. Can you please suggest on that?
I don't see you adding your JLabel to any component. If you are going to create a component on an event, you must add it to a component whose ancestor hierarchy eventually reaches a visible top-level component such as a JFrame. Then after adding a component to a container (say a JFrame), you must call revalidate() on the container to have its layout managers re-layout its components, and then repaint() to repaint any "dirty" pixels.
I again will re-iterate that you're far better off not using null layout and absolute positioning, but rather using layout managers and relative positioning. If you want a label with and without visible text, it's often best to add an empty JLabel to the GUI on GUI creation, and just set its text when needed, as long as the label is located somewhere that allows its text to shrink and expand.
Also, as to your current problem, you might wish to show a picture of what you're trying to achieve, and what you're getting. Or if you can't post a picture here yet, post a link to an image or images you've created, and then we'll post it for you.

Using CardLayout in NetBeans GUI Builder

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

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

jTextfield display problem in java

I have made a frame in which i have put two jTextfield boxes where the user can see the path of the loaded file. Problem is that if the path is too long , the textfield expands to accomodate the full path which again leads to display problems. I would like to keep the textfield's length constant and instead , display the full path of file as a tooltip instead.
How can this be done?
Code for layout manager of jinternal Frame:
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
I never use an IDE so I don't know how the GroupLayout works.
But when using the other layout managers I always use:
JTextField textField = new JTextField(10); // or whatever size your want
This will give the text field a preferred size and the layout manager can use that information when laying out the component.
Code the GUI by hand instead. You will avoid problems like this and it will be much easier to make changes to your code.
you need to choose a layout manager to manage the proportions of your JComponents.
Try to put your textfiels on a JPanel so you can select a layout useful for you
Later you can use JTextField. setToolTip("full path") to set a tool tip
I solved my problem:
Anybody having the same problem can set the Property Columns using Netbeans. The default is 0, so the textfield cannot accomodate the full text. Use some value like 3 to achieve it.

Swing GUI components too tighly packed

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.

Categories

Resources