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.
Related
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.
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.
I have a gridlayout that consist from jPanels. Every JPanel containst a JLabel, they are 9x9. For this is a chess game, I need to be able to highlight every jPanel or its jLabel upon some events (such as being possible move option or an endangered field). However, I can only highlight fields that have their icons (figures) set, which means an empty jLabel has probably zero dimensions.
This is how I put a figure in jLabel (it sucks, but it somewhat works):
public void drawFigureAt(Figure fg, int x, int y) {
//retrieve the Jlabel from the field array
JLabel pole = policka_l[y*8+x];
//Set icon from file
pole.setIcon(new ImageIcon(fg.imageName()+(fg.color==1?"_white":"_black")+".png"));
//force redraw or whatever it is
pole.revalidate();
}
Some of the fields are now highlighted in very transparent black, so I can see that the JLabels ar as large as their icon is.
This is how I highlight:
public void highlightField(int x, int y, Color color) {
//Get the JLabel obejct
JLabel pole = policka_l[y*8+x];
//Set the color as the background
pole.setBackground(color);
//What is this?
pole.setOpaque(true);
//Another magic
pole.revalidate();
repaint();
}
But, as I've said, if JLabel has no icon, it can't be highlighted and that's very sad form me. I's much worse that this is a school project that must work this morning, so using different rendering system is not in question.
I've found some clues how to deal with it, however, they don't work. I hoped a lot in this:
JLabelinstance.setMinimumSize(new Dimension(70, 70));
No effect unfortunatelly.
So what I want now is to either make the function above work, or even better - configure JLabels to fill their parent JPanels so that highlighting will look nice, not stupid.
Also my figures do not resize with the window at all, so they tend to overflow.
I have a gridlayout that consist from jPanels.
You could:
1) Just add the labels directly to the GridLayout. Then the label will be the size of the grid
2) Set the layout manager of the panel to be a BorderLayout, then the label will be resized to fill the entire space of the panel.
pole.setBackground(color);
pole.setOpaque(true);
//pole.revalidate();
//repaint();
The revalidate() and repaint() methods are not needed. Swing components will automatically repaint themselves when a property changes.
Also my figures do not resize with the window at all, so they tend to overflow.
Don't use setSize(). You should add the icons to the label and then labels to the grid. Then pack() the frame and the grid will be sized to hold the largest icon.
I'll try to explain my problem as simply as possible but it's a tricky topic and people who haven't encountered the issue probably won't know what I'm talking about.
I want to use a BorderLayout using west, east, north, south, etc. components that are my "normal" components (JLabels, JButtons, etc.) then I want the center component to be an "image" (that is: pixels). To this end I'm using a BufferedImage and using setIcon on a JLabel that is inside a panel that is part of the "center".
However I want my image/pixels to be "fluid": whenever the user resizes the app, I want to compute the exact size of the JLabel (icon/text gap is set to 0) and then create a new image (pixels that I manipulate directly in a BufferedImage but whatever) that has exactly that size.
Now it does work fine. But only when I resize the main window ("window" as in "one of the window of the operating system) by making it bigger.
It doesn't work when I downsize my main window.
The reason, after a lot of testing, is obviously because the size of my JLabel (in which I did a setIcon( img ) is influencing the computation of the layout manager.
So here comes the billion dollar question: how should I use a BorderLayout (or any other layout) so that I can create a "fluid" rectangle of pixels in the center of my app?
Answering my own question with an answer that I will not accept even tough it does work...
The problem can be "worked around" by creating a picture a few pixels smaller than the getVisibleRect of the center area.
So in my case I create an ImageIcon from a BufferedImage that is 20 pixels smaller (both in width and height) than the area that will hold it.
What happens then is that because the picture is smaller it doesn't "block"/prevent the layout manager from putting everything at their correct place when downsizing the main window.
So by using such an hack I get the "fluid" behavior I want.
This is however an hack whose level of hackyness cannot be understated and I'm sure there's a very clean way to solve this.
The reason, after a lot of testing, is
obviously because the size of my
JLabel (in which I did a setIcon( img
) is influencing the computation of
the layout manager.
The preferred size of the JLabel is used in the preferred size of the panel, but this size is ignored when you resize the frame, since the CENTER only gets whatever space is left over after the preferred size of the other 4 components is considered.
To this end I'm using a BufferedImage
and using setIcon on a JLabel that is
inside a panel that is part of the
"center".
Sounds to me like it should work.
Create the panel with a BorderLayout. Add the JLabel to the Center of your main panel. Then add a ComponentListener to the panel. Now when the frame is resized the center panel size will be adjusted to take the space available to it. Now that you know the size of the center panel you can recreate the Icon and add it to your JLabel,
This is how you write a SSCCE:
import java.awt.*;
import javax.swing.*;
public class LabelTest2 extends JFrame
{
public LabelTest2()
{
JLabel picture = new JLabel(new ImageIcon("???.jpg"));
add(picture);
}
public static void main(String[] args)
{
LabelTest2 frame = new LabelTest2();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
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?