How to move text within Jlabels to the right

Discussion in 'Java' started by clusardi2k@aol.com, Apr 12, 2013.

  1. Guest

    I have two JLabels with different lenght of text. One JLabel is below the other. I'd like to get the two "colons" in them to line-up vertically.

    I tried padding one with "space" characters, but that didn't line up perfectly. My guess is that certain characters have different widths.

    (I tried the align vertical and horizontal properties, etc but they didn't work.)

    Eg, In the below text of two JLabels, the addition of a space character didn't line up the two colons.

    Label1 My First Name:
    Label2 Last Name:

    Thanks,
    , Apr 12, 2013
    #1
    1. Advertising

  2. Nigel Wade Guest

    On 12/04/13 14:12, wrote:
    > I have two JLabels with different lenght of text. One JLabel is below the other. I'd like to get the two "colons" in them to line-up vertically.
    >
    > I tried padding one with "space" characters, but that didn't line up perfectly. My guess is that certain characters have different widths.
    >
    > (I tried the align vertical and horizontal properties, etc but they didn't work.)
    >
    > Eg, In the below text of two JLabels, the addition of a space character didn't line up the two colons.
    >
    > Label1 My First Name:
    > Label2 Last Name:
    >
    > Thanks,
    >


    Place them in a JPanel, and set a layout manager which allows you to align them. GridBagLayout will do this. Align them
    with java.awt.GridBagConstraints.EAST, or LINE_END so they are at the right of the JPanel.

    The Tutorial has all the gory details: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

    --
    Nigel Wade
    Nigel Wade, Apr 12, 2013
    #2
    1. Advertising

  3. bob smith Guest

    On Friday, April 12, 2013 8:12:18 AM UTC-5, wrote:
    > I have two JLabels with different lenght of text. One JLabel is below the other. I'd like to get the two "colons" in them to line-up vertically.
    >
    >
    >
    > I tried padding one with "space" characters, but that didn't line up perfectly. My guess is that certain characters have different widths.
    >
    >
    >
    > (I tried the align vertical and horizontal properties, etc but they didn't work.)
    >
    >
    >
    > Eg, In the below text of two JLabels, the addition of a space character didn't line up the two colons.
    >
    >
    >
    > Label1 My First Name:
    >
    > Label2 Last Name:
    >
    >
    >
    > Thanks,


    Maybe try a fixed-width font like Courier?

    Thanks.
    bob smith, Apr 12, 2013
    #3
  4. Roedy Green Guest

    On Fri, 12 Apr 2013 06:12:18 -0700 (PDT), wrote,
    quoted or indirectly quoted someone who said :

    >I have two JLabels with different lenght of text. One JLabel is below the other. I'd like to get the two "colons" in them to line-up vertically.


    see http://mindprod.com/jgloss/jlabel.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Computer programming is the best remedy for pain (physical or emotional)
    I have encountered. It requires so much concentration there is nothing left
    over to pay attention to the pain. They should teach this in AA.
    Roedy Green, Apr 12, 2013
    #4
  5. On 4/12/2013 6:12 AM, wrote:
    > I have two JLabels with different lenght of text. One JLabel is below
    > the other. I'd like to get the two "colons" in them to line-up
    > vertically.
    >
    > I tried padding one with "space" characters, but that didn't line up
    > perfectly. My guess is that certain characters have different
    > widths.
    >
    > (I tried the align vertical and horizontal properties, etc but they
    > didn't work.)
    >
    > Eg, In the below text of two JLabels, the addition of a space
    > character didn't line up the two colons.
    >
    > Label1 My First Name: Label2 Last Name:
    >
    > Thanks,
    >


    Here are two examples;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class test2 extends JPanel {
    public test2() {
    super(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 1; c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;

    Border b = BorderFactory.createLineBorder(Color.BLUE,1);

    // both labels will be the same size and the text will
    // be right aligned
    JLabel l1 = new JLabel("My First Name:",JLabel.RIGHT);
    l1.setBorder(b);
    JLabel l2 = new JLabel("Last Name:",JLabel.RIGHT);
    l2.setBorder(b);

    add(l1,c);
    add(l2,c);

    setPreferredSize(new Dimension(100,75));
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLayout(new GridBagLayout());
    f.add(new test2());
    f.setSize(400,300);
    f.setVisible(true);
    }
    });
    }
    }



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class test extends JPanel {
    public test() {
    // single label with two lines of text right aligned with css
    JLabel l = new JLabel(
    "<html><div style=float: right;>This is very long:<br>This
    short:");
    l.setBorder(BorderFactory.createLineBorder(Color.BLUE,1));
    add(l);
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.add(new test(),BorderLayout.CENTER);
    f.setSize(400,300);
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    Knute Johnson, Apr 13, 2013
    #5
  6. In article <>,
    wrote:

    > I have two JLabels with different lenght of text. One JLabel is below
    > the other. I'd like to get the two "colons" in them to line-up
    > vertically.
    >
    > I tried padding one with "space" characters, but that didn't line up
    > perfectly. My guess is that certain characters have different widths.
    >
    > (I tried the align vertical and horizontal properties, etc but they
    > didn't work.)
    >
    > Eg, In the below text of two JLabels, the addition of a space
    > character didn't line up the two colons.
    >
    > Label1 My First Name:
    > Label2 Last Name:


    This example uses `GroupLayout`:

    <https://groups.google.com/d/msg/comp.lang.java.gui/PWvVYawy__U/erjchzfASioJ>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Apr 13, 2013
    #6
  7. In article <>,
    bob smith <> wrote:

    > > Eg, In the below text of two JLabels, the addition of a space
    > > character didn't line up the two colons.
    > >
    > > Label1 My First Name:
    > > Label2 Last Name:

    >
    > Maybe try a fixed-width font like Courier?


    Using an appropriate layout is more reliable.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Apr 13, 2013
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rob bell
    Replies:
    1
    Views:
    426
    Chris Smith
    Sep 24, 2003
  2. Arnaud
    Replies:
    2
    Views:
    342
    Andrew Thompson
    Jan 15, 2004
  3. Pestry

    grid of JLabels

    Pestry, Jan 4, 2006, in forum: Java
    Replies:
    2
    Views:
    470
    Tris Orendorff
    Jan 6, 2006
  4. Jason Cavett

    JLabels Are Big

    Jason Cavett, Mar 4, 2008, in forum: Java
    Replies:
    9
    Views:
    491
    Mark Space
    Mar 6, 2008
  5. KDawg44

    Array of JLabels

    KDawg44, Apr 2, 2008, in forum: Java
    Replies:
    7
    Views:
    2,597
Loading...

Share This Page