at now i am trying to add a label beside a frame. And i have a text for the label and want to align it to center but it fails and always be left-align. Please help me!
Simulation sm = new Simulation(dm);
JFrame simulation = new JFrame();
simulation.setTitle("Traffic light and Car park Siumulation");
simulation.setSize(800,600);
simulation.setResizable(false);
simulation.setVisible(true);
simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simulation.add(sm, BorderLayout.CENTER);
//Carpark label
JLabel carparklb = new JLabel("abcd");
carparklb.setPreferredSize(new Dimension(200,600));
carparklb.setVerticalTextPosition(JLabel.CENTER);
carparklb.setHorizontalTextPosition(SwingConstants.CENTER);
simulation.add(carparklb, BorderLayout.EAST);
From the Java Docs
setVerticalTextPosition(int textPosition)
Sets the vertical position of the label's text, relative to its image.
Try JLabel.setVerticalAlignment and JLabel.setHorizontalAlignment instead
Try:
carparklb.setAlignmentY(JLabel.CENTER_ALIGNMENT);
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
This will align the text to the center of the label both horizontally and vertically. If you only wanted to center it horizontally, you can try either:
JLabel carparklb = new JLabel("abcd", JLabel.CENTER_ALIGNMENT);
or
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
Use html code as:
"<html><font size=\"5\"><P ALIGN =\"CENTER\">ARGHYA</P></font></html>"
and use it as the String in the label.
Related
I have a JPanel with several JLabels in it and a JTextPane. I want them to be below each other (so no two two labels on the same line), and aligned to the left. I have tried several things:
Using a BoxLayout with BoxLayout.Y_AXIS works for properly getting all elements below each other. However, while the JTextPane correctly aligns to the left, the JLabels stay centered, even when calling several methods to try and get the alignment to the left (see code below).
Using a GridLayout will correctly put the elements below each other and align them to the left, but then the elements will be vertically spread all over the JPanel with huge spaces between the text lines. I want all elements to be in the top of the panel as far as possible.
private final void init() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JLabel("a"));
final JLabel label = new JLabel();
final JTextPane pane = new JTextPane();
add(label);
add(pane);
// these three lines seem to be ignored
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setAlignmentX(LEFT_ALIGNMENT);
label.setHorizontalTextPosition(SwingConstants.LEFT);
pane.setForeground(Color.RED);
pane.setEditable(false);
}
How can I get this right?
I want them to be below each other (so no two two labels on the same line), and aligned to the left.
You need to set the alignment on all components, even the JTextPane.
add(new JLabel("a"));
How do you expect to change the alignment of that label when you don't have a reference to it?
final JLabel label = new JLabel();
final JTextPane pane = new JTextPane();
add(label);
add(pane);
You don't set the alignment of the above components.
Read the section from the Swing tutorial on Fixing Alignment Problems
This seems like a simple thing to do, but I can't get it to work.
I have a BorderLayout. I want to use the top part for a title bar. I want to add a JPanel with labels, buttons and other components. However, the PAGE_START part of the border layout won't left align the panel. Here's the situation, with comments in where I've tried to set the alignment.
I noticed that when I don't add a panel to the border layout, and just write the JLabel straight in, it has left alignment by default.
This is not what I want, though, because I am planning on putting a BoxLayout.X_AXIS horizontally through the BorderLayout.PAGE_START title area. Seems to be a reasonable thing to do?
The Container pane argument to the static method is just the single panel on the main JFrame.
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
Even when I tell the panel to left align the label, it doesn't appear left aligned.
Does anyone know what I am doing wrong?
By default a JPanel uses a FlowLayout with "center" alignment.
if you want components "left" aligned, then you need to set the layout on the panel to use a FlowLayout with "left" alignment.
Read the FlowLayout API for the proper constructor to use to set the alignment.
Or you can also read the Swing tutorial on How to Use FlowLayut which gives the constructors and valid values to specify the alignment.
I would like to ask if it is possible in a single JLabel to have text icon text, what i mean is the icon to be in the center of my String text.
I managed to move text left or right of the icon but i cant figure out how to put the icon in the middle.
icon to be in the center of my String text
A JLabel has text and icon - you can have the label on top of the text, but not two text's and one icon. You can achieve the same look with 3 JLabel's together in the proper Layout. For example, using a BoxLayout:
Box box = Box.createHorizontalBox();
box.add(new JLabel("Text 1"));
JLabel image = new JLabel();
image.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
box.add(image);
box.add(new JLabel("Text 2"));
Alternatively, if you wish the text to be on top of the image you can do so by setting the appropriate alignments:
JLabel label = new JLabel();
label.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
label.setText("My Text");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
I would like to ask if it is possible in a single JLabel to have text icon text
A JLabel can display simple HTML:
String html = "<html>before <img src=\"file:someFile.jpg\">after</html>";
JLabel label = new JLabel( html );
but HTML takes longer to render so you may want to consider the BoxLayout approach.
I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!
I have a JPanel with some JLabel added with the add() method of JPanel. I want to align the JLabel to the right like the image below but I don't know how to do that. Any Idea? Thanks!
This can be done in two ways.
JLabel Horizontal Alignment
You can use the JLabel constructor:
JLabel(String text, int horizontalAlignment)
To align to the right:
JLabel label = new JLabel("Telephone", SwingConstants.RIGHT);
JLabel also has setHorizontalAlignment:
label.setHorizontalAlignment(SwingConstants.RIGHT);
This assumes the component takes up the whole width in the container.
Using Layout
A different approach is to use the layout to actually align the component to the right, whilst ensuring they do not take the whole width. Here is an example with BoxLayout:
Box box = Box.createVerticalBox();
JLabel label1 = new JLabel("test1, the beginning");
label1.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label1);
JLabel label2 = new JLabel("test2, some more");
label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label2);
JLabel label3 = new JLabel("test3");
label3.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label3);
add(box);
JLabel label = new JLabel("fax", SwingConstants.RIGHT);
To me,
it seems as if your actual intention is to put different words on different lines.
But let me answer your first question:
JLabel lab=new JLabel("text");
lab.setHorizontalAlignment(SwingConstants.LEFT);
And if you have an image:
JLabel lab=new Jlabel("text");
lab.setIcon(new ImageIcon("path//img.png"));
lab.setHorizontalTextPosition(SwingConstants.LEFT);
But, I believe you want to make the label such that there are only 2 words on 1 line.
In that case try this:
String urText="<html>You can<br>use basic HTML<br>in Swing<br> components,"
+"Hope<br> I helped!";
JLabel lac=new JLabel(urText);
lac.setAlignmentX(Component.RIGHT_ALIGNMENT);