Java Forum / GUI / January 2004
Center text in a JLabel?
willy - 29 Dec 2003 23:19 GMT Hi,
I'm not sure what I'm missing here, but I can't get my text to center in a JLabel: Note my 2 different attempts...
JLabel statusPane = new JLabel( ); // this doesn't work? //statusPane.setAlignmentX( JLabel.CENTER_ALIGNMENT );
// this doesn't work? statusPane.setHorizontalTextPosition( SwingConstants.CENTER );
statusPane.setBackground( new Color( 0x00aa00 ) ); statusPane.setOpaque( true ); statusPane.setText( "STATUS" );
Thanks for any tips.
T-
Knute Johnson - 29 Dec 2003 23:47 GMT > Hi, > [quoted text clipped - 16 lines] > > T- JLabel(String text, int horizontalAlignment)
 Signature Knute Johnson email s/nospam/knute/ Molon labe...
VisionSet - 29 Dec 2003 23:58 GMT > I'm not sure what I'm missing here, but I can't get my text to center in a > JLabel: [quoted text clipped - 3 lines] > // this doesn't work? > //statusPane.setAlignmentX( JLabel.CENTER_ALIGNMENT ); No, it is a JComponent method, it may (depends on other factors) affect alignment of the component itself, rather than its contents.
> // this doesn't work? > statusPane.setHorizontalTextPosition( SwingConstants.CENTER ); but only relative to the labels image, if it has one.
but oh so close! Only the method above in the docs.
statusPane.setHorizontalAlignment(CENTER);
Read the docs.
 Signature Mike W
Andrew Thompson - 30 Dec 2003 06:13 GMT ...
> I'm not sure what I'm missing here, An SSCCE for starters..
>..but I can't get my text to center in a > JLabel: > Note my 2 different attempts... <snip code snippets>
Your Label probably _is_ centred, and only taking up as much space as it _requires_.
The secret of getting the label to stretch across the available width of the parent component lies in the LayoutManager of that component.
So, please supply the SSCCE for further advice on Layouts..
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
willy - 30 Dec 2003 06:30 GMT Um WTF is an SSCCE ?
I looked it up on google and got a bunch of gibberish.
Jesus christ, what ever happened to newgroups where people weren't so f.cking condescending and patronizing?
Christ, I've been writing code for 14 years, and in such time I haven't experienced so much grief as I have in the last hour in this newsgroup.
WTF is with you people? Are you all un-employed anal pricks who think you know it all?
I guess when you get to be an old guy like me, you look for comfort and support in newsgroups where you can actually make friends and ask questions. Here I feel like you guys are just a bunch of know-it-alls.. with vague answers (i.e. this posters answer) that point to nowhere and comments to "RTFM".
Cheers.
WT=
> ... > > I'm not sure what I'm missing here, [quoted text clipped - 24 lines] > * http://www.1point1C.org/ 1.1C - Superluminal! > * http://www.AThompson.info/andrew/ personal site Andrew Thompson - 30 Dec 2003 13:30 GMT > Um WTF is an SSCCE ? Short, Self Contained, Compilable, Example.
(and, please do not top-post)
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Gregory A. Swarthout - 30 Dec 2003 23:04 GMT > > Um WTF is an SSCCE ? > > Short, Self Contained, Compilable, Example. > > (and, please do not top-post) Willy, you have been duly warned by the self-appointed Usenet Nazi. Proceed at your own peril. Failure to heed this warning will result in no action whatsoever.
Andrew Thompson - 31 Dec 2003 09:17 GMT ...
> > (and, please do not top-post) > > Willy, you have been duly warned by the self-appointed Usenet Nazi. Proceed at > your own peril. Failure to heed this warning will result in no action whatsoever. Nazis don't say please, the gun says it all.
(Oh, and BTW, you have contributed the sum total of 0 to the OP, so mull this over - I would prefer to be harsh than ..useless)
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
willy - 03 Jan 2004 04:00 GMT I only top-post because the engineers who deal with Outlook Express at MicroSoft insist on having my cursor be positioned at the top of the message as opposed to the bottom when responding to mail, as well as newsgroup messages.
I am just an end user.
> > > Um WTF is an SSCCE ? > > [quoted text clipped - 4 lines] > Willy, you have been duly warned by the self-appointed Usenet Nazi. Proceed at > your own peril. Failure to heed this warning will result in no action whatsoever. Andrew Thompson - 03 Jan 2004 10:48 GMT > I only top-post because the engineers who deal with Outlook Express at > MicroSoft insist on having my cursor be positioned at the top of the message > as opposed to the bottom when responding to mail, as well as newsgroup > messages. What a troll. Still, I can't resist
That's why the engineers who made your keyboard put arrow keys on it (as well as a 'delete' key lest it be missed by either top _or_ bottom posters).
> I am just an end user. You are just whinging, and failing to take responsibility for your own actions.
-- Andrew Thompson * http://www.PhySci.org/ PhySci software suite * http://www.1point1C.org/ 1.1C - Superluminal! * http://www.AThompson.info/andrew/ personal site
Todd Corley - 31 Dec 2003 13:42 GMT What is wrong with top posting? It works better for us google useneters.
willy, Have you gotten your answer? Though he did kinda dork out about posting, Andrew Thompson's comment about the layout is where I suspect your problem lies.
If you have set label.setHorizontalTextPosition(SwingConstants.CENTERED) and the the text does not look centered, the label may not be taking up the entire space you think it does.
You may have: -----------------------------
| Panel | | | | | |------------- | || Label | | |------------- | ------------------------------
When you want: -----------------------------
| Panel | | | | | |----------------------------| || Label || |----------------------------| ------------------------------
One way to debug layouts is to set the border of a normally borderless components to see the full area they cover.
label.setBorder( BorderFactory.createLineBorder(Color.black) );
Hope this helps, Todd
> > Um WTF is an SSCCE ? > > Short, Self Contained, Compilable, Example. > > (and, please do not top-post) Knute Johnson - 30 Dec 2003 19:24 GMT > Um WTF is an SSCCE ? > [quoted text clipped - 48 lines] >>* http://www.1point1C.org/ 1.1C - Superluminal! >>* http://www.AThompson.info/andrew/ personal site Willy:
You need to take it easy man, old guys like us could have a stroke if we get too wound up. I'll agree that some of the answers you get on this news group in particular are less than helpful. I also agree with the helpers that get tired of people not posting workable pieces of code that demonstrate their problems (not always easy to do) or bothering to read the manual. Sometimes though the manual isn't very clear and it is also sometimes very hard to describe a problem.
As to your problem. Your code makes no sense. That's why I posted the piece of the FM that shows how to control horizontal alignment in a JLabel. Since that is all you asked, I didn't go into any details of layout which is probably where your problem is if JLabel("string",JLabel.CENTER) doesn't fix it. If that doesn't fix it, please post a small compilable class that demonstrates your problem and attempt to better describe what isn't working the way you want it to. As an example of that, take a look at mine below that shows horizontal alignment of the text in a JLabel.
BTW, I don't care if you top post.
import java.awt.*; import javax.swing.*;
public class test4 extends JFrame { public test4() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane(); cp.setLayout(new GridLayout(4,1));
JLabel l = new JLabel("Test None"); cp.add(l);
l = new JLabel("Test Left",JLabel.LEFT); cp.add(l);
l = new JLabel("Test Right",JLabel.RIGHT); cp.add(l);
l = new JLabel("Test Center",JLabel.CENTER); cp.add(l);
pack(); setVisible(true); }
public static void main(String[] args) { new test4(); } }
 Signature Knute Johnson email s/nospam/knute/ Molon labe...
willy - 03 Jan 2004 02:45 GMT > > Um WTF is an SSCCE ? > > [quoted text clipped - 101 lines] > } > } Hi Knute,
I've seen some of your work, very impressive. And you are right, I need to take a chill pill before I have a stroke. My question was answered, and I suppose I did fly off the handle a bit, but I really cannot stand being patronized. As for reading the docs, we all know you can't do much in java without sample code, docs and a bunch of good books. I for one, do not intend to spend more than 20 minutes with the documentation on something so simple as centering text.... hence I am not too proud to come in here and say, "What am I doing wrong?". In my case I completely overlooked (not sure how, but I did) the obvious constructor. Apparently this doesn't happen to some of the other guys.
W-
Knute Johnson - 03 Jan 2004 23:35 GMT willy:
no problem - he top posts :0)
k...
> Hi Knute, > [quoted text clipped - 10 lines] > > W-
 Signature Knute Johnson email s/nospam/knute/ Molon labe...
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|