I am making a game, and i'm in the menu, however, I have to put one button. And, to make this, I created a JLabel with one image, and I used the MouseListener, but I have to prescribe the proportion of this JLabel, how can I do this? Thanks for the answer, and sorry my bad english.
Use a layout manager to control the position and size of the components. Take a look at Laying Out Components Within a Container for more details
Use a Border (like EmptyBorder) to affect the "padding" to the label, which will change it's overall size. Take a look at How to Use Borders for more details
Consider using an undecorated button instead of a JLabel. See How to Use Buttons, Check Boxes, and Radio Buttons for more details
JLabel j = new JLabel();
j.setSize(x,y);
where X is the width in pixels and Y is the height in pixels.
JLabel#setPrefferedSize is probably what you're looking for, but I recommend looking into Layout Managers that will set the size for you.
Related
I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".
I have a JLabel with unknown content and I have two things I want to do:
I want to set a maximum or perhaps even static width of the label. And if the text is larger than the label it would somply shorten it, like this:
Verylonglabel
becomes
Veryl
Is it a bad idea to use static width on components in a gui? If that is the case, what is the alternative? Please give me advice!
When you hover over the label I want a tooltip with the full length string to appear. So in our case, if i hover over the label that says "Veryl", a tooltip displaying "Verylonglabel" would appear. However, it should display a tooltip with the full length string even if it was not shortened.
Help with either of these is greatly appreciated.
So far I've just messed around a bit and tried things like this without sucess. It doesn't seem to care about the size at all.
JLabel label = new JLabel("Verylonglabel");
label.setSize(15, 5);
Best regards, Goatcat
The size of your JLabel is determined by the LayoutManager of the parent container. Consult the tutorial for more information.
Note that the JLabel has already the behavior you are looking for
When the text is too long, the text which is cut-off will be replaced by "..." . So in your example, the "Verylonglabel" would be replaced by e.g. "Verylo..."
You can use the setToolTipText method to specify the tooltip, which will be shown when hovering over the JLabel
This is what you need:
JLabel label = new JLabel("Verylonglabel");
// Create tool tip.
label.setToolTipText(label2.getText());
// Set the size of the label
label.setPreferredSize(new Dimension(80,40));// Width, Height
setMaximumSize will only be effective if the LayoutManager you use honors components' desired/max sizes. You may want to check out BoxLayout as a LayoutManager. Unlike several other Swing LayoutManagers, BoxLayout honors the size settings of its components.
This still leaves you the question of what size to set as the maximum size. The width will be whatever you are targeting as your fixed value, but the height should be big enough for whatever font is being used. Here is the code I would use to make the height flexible but the width fixed to 50 pixels:
label.setMaximumSize(new Dimension(10000, 50));
Finally, the tooltip will show the full label text with a line like:
label.setToolTipText(labelText);
I am not good with GUIs or User Interfaces in Java.
Would a Border or JPanel be good for something like the image below?
So, what would be the best option for me? Thank you.
Read the section from the Swing tutorial on Using Layout Managers. You can easily nest panels to get the desired effect.
Maybe start with a BorderLayout. Then you can add a panel that uses a GridLayout, which contains all your image tiles, to the CENTER of the BorderLayout. Then you can add the scrollpane containing the text area to the SOUTH. Then you can create another panel to add to the EAST.
Be creative and experiment.
You can make 4 seperate panels for a border, using BorderLayout.NORTH,BorderLayout.EAST,BorderLayout.SOUTH,and BorderLayout.WEST, This is the easiest way in my opinion.
By the way, in the top right of your picture, where you wanted the information panel, you should put an information LABEL (JLabel) instead, because they hold text. JLabel topRight = new JLabel(); then set the text, position, etc.
p.s. to erase the borders around every tile (if you want to do so), use setBorderPainted(false).
Good morning,
I am making a GUI thanks to javax.swing.Box class
Inside the panel:
JLabel
JTable with fixed height
JLabel
JTable with automatic height
I tried everything to fix the first JTable height but without any success.
I dedicate a Box.createHorizontalBox() for each component of the above rows and then I add them to the Box.createVerticalBox().
Instead of getting the first result I get a layout where both JTable has a automatic height, and I'd prefered the first JTable to have a fixed height...
Thanks for any answer,
Cheers
I found a solution and I shouldn't have annoyed you with such a silly problem:
For each horizontal box I created, I added an horizontal strut of 10 pixels to show a kind of padding. Thoses struts were the firsts in the rows and it was automaticly taken as the "height reference" for the box layout building, but I'm new to awt/swing layout so I may be mistaking saying that.
I removed those struts and inserted a vertical box which contained a horizontal struts of 10 pixels. It did the job.
Anyway, thanks for your time Markus & Michael, I'll dive deeper in sun's tutorial when my boss will let me the time to do so
Cheers
You can change to row height for example by calling
TableColumn column = table.getColumnModel().getColumn(0);
column.setPreferredWidth(150);
//set all rows height
table.setRowHeight(20);
//set specific row height
table.setRowHeight(2,50);
The table size you can update by calling
setPreferredSize(Dimension preferredSize)
You also have to decide, which layout the panel shoul have. Did you set a layout?
How about showing us the actual code?
It sounds like you're not using layout managers correctly. You should probably use a BorderLayout with the "automatic" table in its CENTER position and the rest inside a second panel in the NORTH position, with that second panel using either a Boxlayout or a FlowLayout.
Sun has a very good tutorial on using Layout managers that can probably help you a lot.
I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.