Results 1 to 11 of 11
Thread: can't resize my JLabel
- 06-23-2010, 11:47 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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:
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?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); }
- 06-24-2010, 02:47 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Yes. Read the Swing tutorial on Using Layout Managers for more information about each layout manager.Does the borderlayout always and unconditionally maximize the size of its contents if it can?
Yes. If you only have a single component try the FlowLayout.Am I better off using another layout manager?
- 06-24-2010, 04:25 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Layouts in Java gives you lots of trouble if you haven't use the correct or the most suitable layout.
Using Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
- 06-24-2010, 04:56 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Thanks both. I'll report back on my success/failure after reading up on and trying out some other layouts.
- 06-24-2010, 07:17 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It may take sometime, but much better to read it first of all, and workout all the examples there.
- 06-24-2010, 11:23 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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.
- 06-24-2010, 11:44 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Because its not a valid constraint. A FlowLayout does not require any constraints.I don't know why it doesn't like FlowLayout.CENTER.
FlowLayout.CENTER is used when you create a FlowLayout. Somewhere in your code you must have
Read the FlowLayout API you can specify CENTER (default), LEFT or RIGHT.Java Code:getContentPane().setLayout( new FlowLayout() );
- 06-25-2010, 02:24 AM #8
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
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.Because its not a valid constraint. A FlowLayout does not require any constraints.
Yes, I have this:FlowLayout.CENTER is used when you create a FlowLayout. Somewhere in your code you must have
Java Code:getContentPane().setLayout( new FlowLayout() );
getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));
which isn't giving me any errors.
- 06-25-2010, 02:51 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-25-2010, 03:32 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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".Well, according to this I should be able to pass in FlowLayout.CENTER (which is an int) as the second argument to getContentPane().
- 06-25-2010, 05:30 AM #11
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Yes, I agree. I plan to change that.
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.
Originally Posted by camickr
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
-
How to resize images to fit JLabel?
By Krooger in forum New To JavaReplies: 3Last Post: 11-24-2010, 01:22 PM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
how to resize an image
By marodia in forum AWT / SwingReplies: 2Last Post: 08-21-2009, 04:10 AM -
Image Resize
By jithan in forum New To JavaReplies: 4Last Post: 11-07-2008, 09:26 AM -
Resize frame
By lenny in forum AWT / SwingReplies: 1Last Post: 07-29-2007, 11:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks