Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #23428 > unrolled thread

How to move text within Jlabels to the right

Started byclusardi2k@aol.com
First post2013-04-12 06:12 -0700
Last post2013-04-13 15:16 -0400
Articles 7 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to move text within Jlabels to the right clusardi2k@aol.com - 2013-04-12 06:12 -0700
    Re: How to move text within Jlabels to the right Nigel Wade <nmw@ion.le.ac.uk> - 2013-04-12 15:07 +0100
    Re: How to move text within Jlabels to the right bob smith <bob@coolfone.comze.com> - 2013-04-12 11:47 -0700
      Re: How to move text within Jlabels to the right "John B. Matthews" <nospam@nospam.invalid> - 2013-04-13 15:20 -0400
    Re: How to move text within Jlabels to the right Roedy Green <see_website@mindprod.com.invalid> - 2013-04-12 13:51 -0700
    Re: How to move text within Jlabels to the right Knute Johnson <nospam@knutejohnson.com> - 2013-04-12 20:56 -0700
    Re: How to move text within Jlabels to the right "John B. Matthews" <nospam@nospam.invalid> - 2013-04-13 15:16 -0400

#23428 — How to move text within Jlabels to the right

Fromclusardi2k@aol.com
Date2013-04-12 06:12 -0700
SubjectHow to move text within Jlabels to the right
Message-ID<334f5820-444d-409b-ab37-913a2bc6a6d5@googlegroups.com>
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,

[toc] | [next] | [standalone]


#23429

FromNigel Wade <nmw@ion.le.ac.uk>
Date2013-04-12 15:07 +0100
Message-ID<asqipbF4ee0U1@mid.individual.net>
In reply to#23428
On 12/04/13 14:12, clusardi2k@aol.com 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

[toc] | [prev] | [next] | [standalone]


#23433

Frombob smith <bob@coolfone.comze.com>
Date2013-04-12 11:47 -0700
Message-ID<900bb005-f5ec-4456-a343-8f1203176185@googlegroups.com>
In reply to#23428
On Friday, April 12, 2013 8:12:18 AM UTC-5, clusa...@aol.com 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.

[toc] | [prev] | [next] | [standalone]


#23447

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-04-13 15:20 -0400
Message-ID<nospam-E8D395.15200913042013@news.aioe.org>
In reply to#23433
In article <900bb005-f5ec-4456-a343-8f1203176185@googlegroups.com>,
 bob smith <bob@coolfone.comze.com> 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>

[toc] | [prev] | [next] | [standalone]


#23438

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-04-12 13:51 -0700
Message-ID<ktsgm89ku6h9p4naubhgm2e1a3v2ujbd1n@4ax.com>
In reply to#23428
On Fri, 12 Apr 2013 06:12:18 -0700 (PDT), clusardi2k@aol.com 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.

[toc] | [prev] | [next] | [standalone]


#23439

FromKnute Johnson <nospam@knutejohnson.com>
Date2013-04-12 20:56 -0700
Message-ID<kkaks6$pa6$1@dont-email.me>
In reply to#23428
On 4/12/2013 6:12 AM, clusardi2k@aol.com 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

[toc] | [prev] | [next] | [standalone]


#23446

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-04-13 15:16 -0400
Message-ID<nospam-B0F430.15161113042013@news.aioe.org>
In reply to#23428
In article <334f5820-444d-409b-ab37-913a2bc6a6d5@googlegroups.com>,
 clusardi2k@aol.com 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>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web