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.
Related
I have the following piece of code that I wrote in Vaadin.
The problem is that the alignment does not work. I set it to bottom_center, but the components are all stuck to the top (top_center) of my web browser.
Can anyone help me out with this?
Thanks!
VerticalLayout layout = new VerticalLayout();
VerticalLayout layout1 = new VerticalLayout();
Panel panel = new Panel();
panel.setWidth("500px");
panel.setHeight("300px");
Button button = new Button("Enter");
Button login = new Button("Login");
layout1.addComponent(textfield);
layout1.addComponent(button);
layout1.addComponent(login);
layout.addComponent(panel);
layout.setComponentAlignment(panel, Alignment.BOTTOM_CENTER);
layout1.setComponentAlignment(textfield, Alignment.BOTTOM_CENTER);
layout1.setComponentAlignment(login, Alignment.BOTTOM_CENTER);
layout1.setComponentAlignment(button, Alignment.BOTTOM_CENTER);
The reason for the components appering to be aligned to the top is that you haven't specified a height for your VerticalLayouts. When you don't specify a size for a layout, its size is determined by the child components it contains. In such a case, setting the alignment won't make any difference, as there's no extra room inside the layout's slots.
Set the size, using e.g. layout.setHeight("100%"); or layout.setSizeFull(); and you should notice the difference immediately.
You did not specify a height for the layout so it'll expand from the top towards the bottom as it needs with each component you add. If you set layout.setHeight("100%"); the components will be aligned at the bottom.
you mus set setSizeUndefined() to your panel, textfield, login, button component
and setSizeFull layout1.
I would like some help on choosing or creating a layout for a component to which I will add labels at runtime. For example the component size will be 400*50 and empty when the application start up. Then some action will add a first label at position 0, the second at position first label's width, the third at first label's width + second label's width and so on. A bit like the FlowLayout except that I want my labels to start at the left of the container and not in the center.
Thanks for helping.
Just use a FlowLayout and call setAlignment(FlowLayout.LEFT); (or use one of the constructors that takes an alignment code).
You can use FlowLayout(FlowLayout.LEFT)
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
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.
I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.