I have a JLabel with a Icon and text aligned to bottom, the icon is center to the text,
how do i have the Text and the icon aligned to left and the text below the icon.
My current code is as below
label.setIcon(new ImageIcon(fileName));
label.setText("This text is going to be varying size and can be bit long");
label.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
label.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
If you mean someting like this, then I cheated.
Basically, I created two JLabels on a JPanel. Using a GridBagLayout I aligned the two labels onto of each other and anchored to the WEST
It would pretty easy to create a custom component for the purpose if you wanted to reuse it.
your just setting the Position of your text in your code..not the Icon it self
Try to separate the icon in the label then format it using set bounds or any formlayout
Related
I'm trying to set up a few JLabels to use as buttons inside a BoxLayout, stacked on top of each other. The layout is fine, but I'm finding that I can't resize the labels to the dimensions I want. I'm using the following code to size them:
JLabel fileAddBtn = new JLabel("Add File", SwingConstants.CENTER);
fileAddBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileAddBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileAddBtn.getMinimumSize().height));
and
JLabel fileRemBtn = new JLabel("Remove File", SwingConstants.CENTER);
fileRemBtn.setBorder(BorderFactory.createLineBorder(Color.black));
fileRemBtn.setMaximumSize(new Dimension(Integer.MAX_VALUE, fileRemBtn.getMinimumSize().height));
As of now I have two labels, with one being longer than the other. They are both taking the width of the longer label, which is good, but the labels are hugging the edges of the text right to the nearest pixel. Is there any way to make the labels a little bigger so that there is a bit of a border around the labels? I've tried using setSize() but it doesn't take. I've also added straight values into the above code, but it doesn't change them either. I tried adding an EmptyBorder() around them, which worked for sizing, but it hid my line border which surrounds them. Any thoughts?
Is there any way to make the labels a little bigger so that there is a bit of a border around the labels?
Sure. Add an EmptyBorder.
But since the code is already adding a border to the labels, to retain that line border, make a CompoundBorder consisting of the empty border and the line border, and set the compound border to the label.
See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
I know that when I create an instance of tabbed panes I set the position as such:
jtp = new JTabbedPane(SwingConstants.LEFT)
My question is that is it possible to set the position to the left as above, but to shift the starting position a little bit downwards? I do not want the first tab appearing at the very top of the left hand side. I want to leave some empty space. Is that possible? Thanks.
You can pack your component into javax.swing.Box add shift it:
jtp.add("Component shifted vertically", shiftVertical(your_component, 5)); // 5 px or more
public static Box shiftVertical(JComponent component, int size){
Box vBox = Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(size));
vBox.add(component);
return vBox;
}
I managed to find a work around for this problem. I did this by creating a "dummy" icon which was basically a square with the same colour as the background colour of the tabbed panes. I then set this icon as the icon for a "dummy" tabbed pane and disabled the tab:
jtp.addTab("", dummyIcon, null);
jtp.setEnabledAt(0, false);
By resizing the icon I could make the first tabbed pane take up as much space as i wanted since the panes don't have to be the same size. This way The first tab was not clickable and it had a colour similar to the background colour of the panel, so it appeared as if the space was empty.
I have a problem, I have a JLabel inside the content panel which layout is absolute X Y.
The problem is that the text of my JLabel is centered on JLabel, but when I add a few letters to the text, the position of the JLabel is pushed a bit back.
Can't I fix the JLabel position and use it always for the entire application? even if the text changes.. ?
Thanks in advance ;)
When the horizontal alignment is set to JLabel.CENTER, the text will want to expand let and right about the center position of the label.
This means that the label is doing exactly what the label was asked to do.
To,over come this, you should use JLabel.LEFT as the horizontal alignment and position the label itself as you need within its parent container
The label is in the same place. It's position is the upper left corner. With less text in it, it will be smaller.
If you want to use it with absolute layout, set its position and size.
I've started doing some experiences with Swing in Netbeans.
I've created two panels and inside each one I've inserted one JLabel.
How can I define the vertical position of my label inside the frame?
Since I'm unable to align the two labels (one on each panel) I would like to set each one to some vertical alignment.
Layouts with layout padding and component borders.
See:
Laying Out Components Within a Container
How to Use Borders
you can use
setBounds(x,y,width,height);
JLabel label = new JLabel();
label.setBounds(10,10,40,20);
I'm having problem in my JToolBar. I'm using images of different size.
Tool bar is not looking good. How can I use different size images in the JToolbar buttons?
How can I show the button label below each image?
How can all the buttons be aligned from the top, left corner?
For example,
java.net.URL imageURL2 = cldr.getResource("Images/report2.jpg");
ImageIcon aceOfDiamonds1 = new ImageIcon(imageURL2);
btnReport = new JButton(aceOfDiamonds1);
btnReport.setMaximumSize(new Dimension(49, 43));
btnReport.addActionListener(this);
jToolBar1.add(btnReport);
I'm using images of different size.
Resize them. Either once at time of build (recommended), or at run-time.
And I want to show the button label below each image
This is close.
newJButton(String,Icon);
And i want to show the button label below to the each image?
With the setVerticalTextPosition and setHorizontalTextPosition methods of JButton:
// Place text below icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
How to show all this buttons align from the left top corner
As Andrew Thompson said, the images all have to be the same size.