Vaadin incorrect alignment - java

I have the following piece of code that I wrote. I am trying to center both the texts one after another in the panel like this:
Your name is:
1
Can anyone tell me how to go about doing this?
Thank you.
Code:
VerticalLayout layout1 = new VerticalLayout();
layout.setSizeFull();
layout1.setSizeFull();
Label label = new Label("Your name is: ");
label.setStyleName(Runo.LABEL_H1);
Label label1 = new Label(name);
label1.setStyleName(Runo.LABEL_H1);
layout1.addComponent(label);
layout1.addComponent(label1);
layout1.setComponentAlignment(label, Alignment.MIDDLE_CENTER);
layout1.setComponentAlignment(label1, Alignment.MIDDLE_CENTER);
layout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setContent(layout);
panel.setContent(layout1);
Output:

Labels in Vaadin have a default width of 100%, meaning the Label component fills the entire horizontal space, leaving no room for alignment. The reason for this default is that in most cases you'll want the text to wrap once it reaches the edge of the parent component.
To center the labels in your code, just set their widths to undefined by doing label.setWidth(null);
The reson for the extra vertical space is in the VerticalLayout. Since you've set an expicit height for it but haven't specified any expand ratios for the slots, it's making both of the slots equally high; half the height of the layout each. To get around that, you can do one of two things: get rid of the height in layout1 by replacing the call to layout1.setSizeFull(); with layout1.setWidth("100%");—or set an expand ratio for the second slot by doing layout1.setExpandRatio(label1, 1);. Setting the expand ratio will cause the layout to use all the extra space in the second slot, leaving the two labels closer together.
With setExpandRatio you'll of course also have to change the alignment of label1 from MIDDLE_CENTER to TOP_CENTER, so that the empty space is placed below the label and not evenly around it.

Related

JFrame & JPanel sizing issue

I am attempting to code a JFrame containing a JPanel. Within the JPanel is an array of JTextField's. So, my GUI looks like:-
I am not using a layout manager, and have set this to null for the JFrame and the JPanel. I am sizing these components by hand.
You can see that the right hand portion of the JPanel is chopped off, even though I have used the same sizing as the containing JFrame.
The code appears as below:-
I have calculated the required width of the JPanel by multiplying the number of columns in the JTextField array by the width of the JTextField. Aside from that would need to be added the width of each gap between the JTextFields (there would be (columnNumber - 1) of them), as well as the two border gaps.
I have done this, yet the right hand side border gap is chopped off, as you can see from the diagram.
If I add some random amount to the panelWidth, then you can see the right hand gap there, but my question is what am I missing here? This ought to work surely, if the JFrame side and the JPanel size are identical, which they are as I have also printed them both out, and the print outs give the same number.
Jeremy
I want for any combination of row/column values to allow a constant vertical & horizontal distance between each JTextField, and for each of those text fields to maintain default sizing.
The GridLayout allows you to specify a horizontal/vertical gap between each component and allows you to control the size of the grid.
Then you can wrap the panel using a GridLayout in a panel that respects the size of the grid.
For example you could do:
JPanel grid = new JPanel( new GridLayout(...) );
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(grid, new GridBagConstraints());
frame.add(wrapper, BorderLayout.CENTER);
If you pack the frame, the grid panel will be displayed at is preferred size.
If you resize the frame the grid panel will remain centered in the wrapper panel.
The top part of that GUI is well suited to a grid layout, the bottom part with 'Go / Cancel' buttons - a flow layout. Put the grid layout in the CENTER of a border layout, the flow layout in the PAGE_END, pack the top level container (for non-cropped, 'right size') & the job is done.
It might end up looking something like this:

Java - JSpitPane Anchor Right

Is there a way to anchor the divider of the JSplitPane to the right of the window?
This is my current my current window when I run the application. However, when I resize it, the divider remains on the location I set.
Like this:
Is there a way to anchor it such that the right component will retain its size?
Here's my current code:
JSplitPane p = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
p.setLeftComponent(new JPanel());
p.setRightComponent(new JPanel());
p.setDividerLocation(500);
Thank you!
You need to setResizeWeight to the required weight you need between 0 and 1. setResizedWeigth divides the amount of space taken on by each panel. e.g. if you set the weight to 0.5 then the panels will half the new free space, if you set it to 1 the right panel will stay the same size and the left panel will absorb all the new space. So an answer to your question if im reading correctly, just add this line of code
p.setResizeWeight(0.5);
Hope this helps.

Adding margins in a JTextArea?

So I have a JTextArea which shows the text right from the top left corner. I want some margin on all 4 sides, so there's some space between text and boundary of the area.
I have researched a lot and could not find any solution. How can I do it?
Also, I was thinking maybe put up a label on all 4 sides to create dummy margin. How can I create a JLabel with certain width and height?
May be I dont understand your question correctly. However you can use setMargin()
// set the margin for all four sides
tt.setMargin( new Insets(10,10,10,10) ); // tt is JTextArea instance
Some Important Links
1. setMargin API
2. Class Insets

How do I add a JLabel to a JUNG graph?

I'm trying to add a label to my graph, but can't get it out of the default center position. This is what I have right now in my update method (it refreshes the display):
Graph<String, String> graph = getGraph();
BasicVisualizationServer<String, String> vv = getViewer(graph);
frame.getContentPane().removeAll();
frame.getContentPane().add(vv);
JLabel label = new JLabel("<html>ndp: blue<br>mdp: dark green</html>", JLabel.LEFT);
vv.add(label);
frame.pack();
It shows the label, but won't put it on the left no matter what I attach the label to or how I specify that it should be left... What am I doing wrong?
Just a thought, but try programming how far from the top, bottom, left and right and adjust from there. If you are using Eclipse and the WindowBuilder, you will see the code on how it programs the position of things for starters. I believe it uses NORTH, SOUTH, EAST, WEST instead of the previous directions to place components. From there, you should be able to adjust the values (programmatically) and place the JLabel where you want it to be.
It's ugly, but I fixed it by just adding lots of non-breaking spaces to one row of the label content (Alt+0160), which expanded the width of the Label to be roughly the size of the window so when it is centered, the text is left-aligned. setSize() didn't work.

How do I create a component that auto-sizes in the Borderlayout North Position

I want to create a JLabel (containing an image) in the north position of a border layout that auto-sizes to a length matching the preferred width of a component in the center position of a border layout.
The only way I can do this at present is to create another panel in the north position and add the label in the center position of this panel.
Is there a way to do this without the extra panel?
There is no need to add extra panel, As I see that you only need Label in North (i.e. Top).
Components added to north in borderlayout will occupy complete width and height will be preffered height of component. This is decided on various factors.
You just need to take care of setting label text and image in center. Look at alignment api's of label for same.
Details:
http://www.ehow.com/way_5579409_java-borderlayout-tutorial.html
e.g.
http://www.java2s.com/Tutorial/Java/0240__Swing/1340__BorderLayout.htm
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/border.html
Well, I'm not sure I understand the question. A JLabel does not "autosize" itself. The size of the label is the size of the Icon added to the label. So the size of the image will not change even if the width changes.
Maybe you can use:
label.setAlignmentX(...);
label.setHorizontalAligment(...);
To horizontally center the label in the North of the panel, if thats what your question is.
Why don't you post your currently working SSCCE that shows what you are doing. Also, what is the problem with using a second panel?

Categories