Results 1 to 11 of 11
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default can't resize my JLabel

    Hello,

    I'm programming a clock. It consists of a JFrame and a JLabel displaying the time. Here is a screen capture:



    As you can see, the entire background is black. I don't want it this way. I set the background color of the JLabel black (and made it opaque), but for some reason it seems to want to take up all the space in the JFrame.

    I can't control the size of the JLabel with calls to setSize(int,int) or setMaximumSize(Dimension d) as they seem to do nothing.

    The layout of the JFrame (or rather its contentpane) is borderlayout and I told it to center the JLabel when adding it.

    I've verified that the black background belongs to the JLabel specifically by placing two more JLabels to the WEST and EAST areas of the borderlayout. Both were not opaque and so the grey background that shows through belongs to the JFrame's contentpane:



    Here is the code that initializes all this:

    Java Code:
    	public ClockJLabel() {
    
    		C = new Clock();
    
    		setText(C.getCurrTime()); // should be 00:00:00:00 
    		setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    		// need to figure out how to set the size so that it sticks
    		setForeground(new java.awt.Color(255, 0, 0));
    		setBackground(new java.awt.Color(0, 0, 0));
    		setOpaque(true);
    	}
    Does the borderlayout always and unconditionally maximize the size of its contents if it can? Is there no way to override this? Am I better off using another layout manager?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Does the borderlayout always and unconditionally maximize the size of its contents if it can?
    Yes. Read the Swing tutorial on Using Layout Managers for more information about each layout manager.

    Am I better off using another layout manager?
    Yes. If you only have a single component try the FlowLayout.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks both. I'll report back on my success/failure after reading up on and trying out some other layouts.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It may take sometime, but much better to read it first of all, and workout all the examples there.

  6. #6
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Okay, FlowLayout seems to work for my purposes, but I did encounter some strange runtime errors.

    This threw an IllegalArgumentException:

    getContentPane().add(CJL, FlowLayout.CENTER);

    ...where CJL is my ClockJLabel. Taking out FlowLayout.CENTER seems to work as it seems to center content by default. I don't know why it doesn't like FlowLayout.CENTER. Could it be that it only makes sense to center components if there are at least two others already that are on either side? My research suggests that the alternative would be to pass in a constraints object in addition to the above which would entail creating one and I'm too unfamiliar with constraint objects to take on that task.

    Another thing I'd like is to center my ClockJLabel vertically which FlowLayout doesn't seem to support, so I'm still reading up on different layouts. I may (or may not) report back on this. If not, I'll consider this issue solved and thanks to all who pitched in to help me.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I don't know why it doesn't like FlowLayout.CENTER.
    Because its not a valid constraint. A FlowLayout does not require any constraints.

    FlowLayout.CENTER is used when you create a FlowLayout. Somewhere in your code you must have

    Java Code:
    getContentPane().setLayout( new FlowLayout() );
    Read the FlowLayout API you can specify CENTER (default), LEFT or RIGHT.

  8. #8
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Because its not a valid constraint. A FlowLayout does not require any constraints.
    Well, according to this I should be able to pass in FlowLayout.CENTER (which is an int) as the second argument to getContentPane().add(Component,int). The add() method below it in the link I provided is probably the one you're thinking of.

    FlowLayout.CENTER is used when you create a FlowLayout. Somewhere in your code you must have
    Java Code:
    getContentPane().setLayout( new FlowLayout() );
    Yes, I have this:

    getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));

    which isn't giving me any errors.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Just a tip.

    I think it's more clear if you could added import statement separately, rather adding them inline. Code is more readable I guess, for me it is actually.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Well, according to this I should be able to pass in FlowLayout.CENTER (which is an int) as the second argument to getContentPane().
    No, that is not how that method is used. It is NOT used to control alignment of the layout. It is used to control the position of the component in the "flow layout".

  11. #11
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Just a tip.

    I think it's more clear if you could added import statement separately, rather adding them inline. Code is more readable I guess, for me it is actually.
    Yes, I agree. I plan to change that.

    Quote Originally Posted by camickr
    It is used to control the position of the component in the "flow layout".
    Right, which is exactly what I'm trying to do. I'm trying to control the position (centering) of CJL (a JLabel) in the flow layout.

    Is it giving me an error because that's an invalid move when the LayoutManager is a FlowLayout (I can see this because, say for example, you're adding your fifth component; if the LayoutManager is a FlowLayout, it will want to put it at the end (or beginning) of the row, not the center. The latter would make no sense with a FlowLayout - am I getting it yet?).

Similar Threads

  1. How to resize images to fit JLabel?
    By Krooger in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 01:22 PM
  2. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  3. how to resize an image
    By marodia in forum AWT / Swing
    Replies: 2
    Last Post: 08-21-2009, 04:10 AM
  4. Image Resize
    By jithan in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 09:26 AM
  5. Resize frame
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •