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)
Related
I am working on my own little text editor. Files can be edited in a JScrollPane, now if a line of text is longer than the window, you can scroll to the right as it is ment to be. But when the Blinker(or whatever it's called) is at the very end of the line, it's not visible because it seems to be covered by the border.
//the JTextArea is inside the JScrollPane of course
Border scrollPaneBorder = new LineBorder(interfaceColor, 8, true);
Border textAreaBorder = new LineBorder(backgroundColor, 4, true);
Setting both borders to 0 won't change anything. Has anyone got a way to deal with this problem?
The most elegant solution in my opinion is the one in Notepad++. There it somehow puts some space between the text and the border as soon as you get close to the border. But I don't know how/if this is possible in java.
Whatever solution, thanks for the help guys.
From the documentation of setMargin:
Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).
It’s a bit strange that Swing’s text component will move the cursor up to a location that is covered by the painted Border if there is no margin, but it explains why the problem occurs when you set a custom Border but not with the default border as the default value for the margin property is non-zero.
You can create a custom border that reads the margin property and implements an unpainted inner region of that size or, if you don’t need support for different values for the margin property, you can just combine your border with an empty border to get a similar effect (of a hardcoded margin space):
Border textAreaBorder=BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(backgroundColor, 4, true),
BorderFactory.createEmptyBorder(2, 2, 2, 2)
);
I have a column of JPanel instances that has content in it, that when it is clicked, the selected Panel is set to have a border (in order to distinguish it), and only 1 at a time has the border.
The problem is that when it sets the border, it sets the outer section of the panel to the border, and shrinks the content inside. Although it seems minor it is not very professional, and I would much rather have it add more like an overlay, where the content will not shrink.
I am thinking maybe there is some method of graphics that will let me do this? I haven't been able to find any way of doing this.
Start by setting all the components to have a EmptyBorder set to a single pixel inset.
When you select a panel, simply set the newly selected panel's border as you are (presumably using a LineBorder) and the set the previously selected panel's border to the single pixel EmptyBorder.
If you're clever, you could get away with a single instance of EmptyBorder ;)
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 want to make a layout with two componenents in a vertical row.
The first component is an instance of my own class, ImagePanel, that extends Panel and shows an image, and I want it to take up the exact space that it needs to show the entire image. The remaining space should then be filled by the other component (in this case another Panel with a GridLayout). See the picture.
In Android you can do this by using the weight property, but I have not been able to find anything like that in Java, and I can´t see that any of the standard layout managers in Java would be suitable for this.
I tried putting the ImagePanel in BorderLayout.NORTH and the other panel in BorderLayout.CERNTER, but the second panel was then overlapping the image, so that didn't work.
I also thougth about using a GridLayout but the grid would not care about the size of the image, so I don't think that would work either.
Any help is appreciated.
A BorderLayout should work, provided the image panel's getPreferredSize() returns the correct dimension (i.e. the dimension of the image it displays).
Using a simple JLabel containing an ImageIcon and no text instead of your custom ImagePanel would do that for you.
The most powerfull layout in java is GridBagLayout. It has weightx and weighty properties, anchor, fill, etc.
By default, with GridBagLayout each component has all the necessary space to show fully. If you want the second panel to expand, it should be enough with weighty=0.0 in your image and weighty=1.0 in your other panel.
The javax.swing.Box should be exactly what you want. There are 2 types of boxes, vertical and horizontal, you need a vertical:
Box box = Box.createVericalBox();
box.add(comp1);
box.add(Box.createVerticalStrut(5)); // add some empty space
.
.
.
add(box);
Box will only force one side to be equal, for vertical, it's the width, the length could be any 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.