12 Replies - 2647 Views - Last Post: 09 August 2010 - 05:56 PM Rate Topic: -----

#1 rmetzger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-October 09

JLabel formatting issues

Posted 08 August 2010 - 05:58 PM

I have a JLabel
JLabel label = new JLabel ("test");


My first problem is I can't get the label to show up with an empty constructor so I have to put a string in it. My code sets the label text dynamically later on but it won't display unless a string value is put in the constructor first.

I center it using
label.setAlignmentX(JPanel.CENTER_ALIGNMENT);


When I change the text later dynamically to some longer text, it still looks centered, but only the first couple letters show up followed by "...". So when I try to change the text to "Solved!" it ends up showing "S..."

I've tried changing the size using label.setSize(100, 50); No luck.

Any ideas how to fix these two problems?

Is This A Good Question/Topic? 0
  • +

Replies To: JLabel formatting issues

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: JLabel formatting issues

Posted 08 August 2010 - 06:03 PM

validate() your JFrame and see if that does it. I would center your label using a BorderLayout in your JFrame instead of setAlignment() if my first suggestion doesn't solve it
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: JLabel formatting issues

Posted 08 August 2010 - 06:13 PM

The reason your label doesn't "show up" with an empty constructor is because the constructor takes as its argument the text for your label. An empty constructor means no text, thus no visible label, as labels' background colors are by default the same as a JFrame/JPanel's.

As for the label showing part of the text and not some, it means that your JLabel's width is too narrow to display the entire text. Reasons for this are various, but probably has something to do with your sizing. Did you use pack()? Did you set the size of your frame too small? Did you use any layouts?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JLabel formatting issues

Posted 08 August 2010 - 06:25 PM

View Postbcranger, on 08 August 2010 - 07:13 PM, said:

The reason your label doesn't "show up" with an empty constructor is because the constructor takes as its argument the text for your label. An empty constructor means no text, thus no visible label, as labels' background colors are by default the same as a JFrame/JPanel's.

A lot of inexactitudes is such a few sentences
The text of the JLabel can be empty but the label exists (without blank text)
In a GridLayout it would reserved it's slot area
If it is make opaque its background will show up in any layout
Was This Post Helpful? 1
  • +
  • -

#5 rmetzger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-October 09

Re: JLabel formatting issues

Posted 08 August 2010 - 08:44 PM

View Postbcranger, on 08 August 2010 - 05:13 PM, said:

The reason your label doesn't "show up" with an empty constructor is because the constructor takes as its argument the text for your label. An empty constructor means no text, thus no visible label, as labels' background colors are by default the same as a JFrame/JPanel's.

I'm using an empty constructor and then setting the label text dynamically during execution using label.setText("something"). But nothing shows up dynamically unless I put something in the constructor to begin with. Why is this?

Quote

Did you use pack()? Did you set the size of your frame too small? Did you use any layouts?

I'll have to look into pack() as i don't know what that is.

The frame size is plenty big enough for the text because when I set the label's text to something longer initially, it shows up fine. It is only if I start with a short string and try and fit a larger string in it that it doesn't work.

I've tried setting the label width and height using label.setSize(100, 50);, but then the text no longer stays centered.

I am using a BoxLayout along the Y_AXIS.

Thanks for the help so far.
Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: JLabel formatting issues

Posted 08 August 2010 - 09:01 PM

did you read my post? to change the gui dynamically, you must validate() the JFrame
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JLabel formatting issues

Posted 08 August 2010 - 09:51 PM

View PostLuckless, on 08 August 2010 - 10:01 PM, said:

did you read my post? to change the gui dynamically, you must validate() the JFrame

Wouldn't bet a paycheck on that one but
When you update/change a JComponet characteristic they usually call repaint() by themselve so

label.setText("asdsad");

should'nt need any repaint()/pack()/validate()
before the change is displayed
Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: JLabel formatting issues

Posted 09 August 2010 - 06:09 AM

after some digging, pbl is right. I tried your example and it works fine for me:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DIC {
   static JLabel label;
   
   public DIC(){
	   JFrame frame = new JFrame();
	   JPanel panel = new JPanel();
	   label = new JLabel("test");
	   
           frame.add(panel);
	   panel.add(label);
	   label.setAlignmentX(JPanel.CENTER_ALIGNMENT);
	  
           frame.pack();
	   frame.setVisible(true);
   }
   public static void main(String[] args){
	   new DIC();
	   label.setText("SOLVED!!!");
   }
}



nifty, eh?

This post has been edited by Luckless: 09 August 2010 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

#9 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: JLabel formatting issues

Posted 09 August 2010 - 06:42 AM

Well, if you want an empty JLabel at the beginning, why not write JLabel label = new JLabel("");? It would work.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10644
  • View blog
  • Posts: 39,515
  • Joined: 27-December 08

Re: JLabel formatting issues

Posted 09 August 2010 - 02:38 PM

My money is on that the JLabel no-args constructor does that by default. A good no-args constructor should initialize attributes to default values, and null-quotes are a good default for Strings. Now whether or not you will notice it is another story.
Was This Post Helpful? 0
  • +
  • -

#11 rmetzger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-October 09

Re: JLabel formatting issues

Posted 09 August 2010 - 04:39 PM

I still can't get the text to center after it is changed dynamically. I re-read the posts and realized I am not using a JFrame and other people are. Is a JFrame necessary?

Also, when I run the program and the label's text changes, if I resize the window the the label's text centers and the whole JPanel I have everything in gets slightly wider....

Same thing happens if I use an empty JLabel constructor. Later when I change the text, it doesn't show up unless I resize the window. How do I make it do this automatically?

This post has been edited by rmetzger: 09 August 2010 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#12 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: JLabel formatting issues

Posted 09 August 2010 - 04:46 PM

JFrame is the way to go
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: JLabel formatting issues

Posted 09 August 2010 - 05:56 PM

View Postmacosxnerd101, on 09 August 2010 - 03:38 PM, said:

My money is on that the JLabel no-args constructor does that by default. A good no-args constructor should initialize attributes to default values, and null-quotes are a good default for Strings. Now whether or not you will notice it is another story.

You should have bet higher Mac

JLabel() constructor without any parameter

   /**
     * Creates a <code>JLabel</code> instance with 
     * no image and with an empty string for the title.
     * The label is centered vertically 
     * in its display area.
     * The label's contents, once set, will be displayed on the leading edge 
     * of the label's display area.
     */
    public JLabel() {
        this("", null, LEADING);
    }



which calls this one
    /**
     * Creates a <code>JLabel</code> instance with the specified
     * text, image, and horizontal alignment.
     * The label is centered vertically in its display area.
     * The text is on the trailing edge of the image.
     *
     * @param text  The text to be displayed by the label.
     * @param icon  The image to be displayed by the label.
     * @param horizontalAlignment  One of the following constants
     *           defined in <code>SwingConstants</code>:
     *           <code>LEFT</code>,
     *           <code>CENTER</code>,
     *           <code>RIGHT</code>,
     *           <code>LEADING</code> or
     *           <code>TRAILING</code>.
     */
    public JLabel(String text, Icon icon, int horizontalAlignment) {
        setText(text);
        setIcon(icon);
        setHorizontalAlignment(horizontalAlignment);
        updateUI();
        setAlignmentX(LEFT_ALIGNMENT);
    }


So JLabel() is JLabel("", null, LEADING);

Text = "";
Icon = null;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1