Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

setMaximumSize() has no effect

Thread view: 
Matthias Kaeppler - 08 Jun 2005 17:31 GMT
Hi,

I want to limit the size of a JLabel in another panel to X,Y. However,
calling setMaximumSize( new Dimension(X,Y) ); has no effect.

How come?

    private class StatusBar extends JPanel {
   
    StatusBar() {
       super( new GridLayout(1,1) );

       statusText = new JLabel();
       statusText.setHorizontalAlignment( SwingConstants.LEFT );

       // the follwing line does nothing
       statusText.setMaximumSize( new Dimension(50,20) );

       statusText.setBorder( new EtchedBorder() );

       add( statusText );

       //[...]
    }
        // [...]
    }

Signature

Matthias Kaeppler

Andrew Thompson - 08 Jun 2005 17:45 GMT
> I want to limit the size of a JLabel in another panel to X,Y. However,
> calling setMaximumSize( new Dimension(X,Y) ); has no effect.
[quoted text clipped - 5 lines]
>     StatusBar() {
>        super( new GridLayout(1,1) );

Probably because of that, see the constructor for
GridLayout(int, int, int, int)

"Creates a grid layout with the specified number of rows and columns. All
components in the layout are given equal size. "

Taken in conjunction with..
"In addition, the horizontal and vertical gaps are set to the specified
values. Horizontal gaps are placed at the left and right edges, and between
each of the columns. Vertical gaps are placed at the top and bottom edges,
and between each of the rows."

So.. ignoring the space between columns (there is only one), we are
left with the horizontal/vertical padding and the single component,
which gets all remaining space.

This is simialr to adding the component to the CENTER of a BorderLayout.
Try FlowLayout and it should work more as you expect.

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Matthias Kaeppler - 08 Jun 2005 18:03 GMT
>>I want to limit the size of a JLabel in another panel to X,Y. However,
>>calling setMaximumSize( new Dimension(X,Y) ); has no effect.
[quoted text clipped - 26 lines]
>
> HTH

Ah okay. Using FlowLayout however has other nasty side effects, in that
for example the JLabel is now not aligned left anymore.

So I ask right away:
If I want a JLabel and a JProgressBar to be next to each other in a
JPanel, with the JLabel's size being maxed at a specific value and the
ProgressBar taking all the remaining space, what layout would be best to
use for the Panel?

Signature

Matthias Kaeppler

Andrew Thompson - 08 Jun 2005 18:15 GMT
>>>I want to limit the size of a JLabel in another panel to X,Y. However,
>>>calling setMaximumSize( new Dimension(X,Y) ); has no effect.
...
>> Probably because of that, see the constructor for
>> GridLayout(int, int, int, int)
...
> Ah okay. Using FlowLayout however has other nasty side effects, in that
> for example the JLabel is now not aligned left anymore.

Well, ..duhh!  Have you discovered the JavaDocs yet, or
are you programming using 'The Force'?
<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FlowLayout.html#FlowLayout()>
"Constructs a new FlowLayout with a centered alignment ...."
<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FlowLayout.html#FlowLayout(int)>
..read on.

[ I was about to mention the JavaDocs specifically in my last post,
but resisted - hoping you would get the hint! ]

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Matthias Kaeppler - 08 Jun 2005 18:28 GMT
>>>>I want to limit the size of a JLabel in another panel to X,Y. However,
>>>>calling setMaximumSize( new Dimension(X,Y) ); has no effect.
[quoted text clipped - 18 lines]
> [ I was about to mention the JavaDocs specifically in my last post,
> but resisted - hoping you would get the hint! ]

Well, sometimes I have at least the feeling one needs "The Force" to get
a hang of this weird API, because it never does what it should. :D

I appreciate your hint, but it doesn't help with my problem, also see my
other post. I don't just want two widgets to be placed right after each
other, I need one at a fixed size (label), the other at a variable size,
filling all the space which is left (progress bar).

Regardless which constructor I use, setMinimumSize() still has no effect.

Signature

Matthias Kaeppler

mitch - 08 Jun 2005 18:17 GMT
A BorderLayout with the JLabel in WEST position and the ProgressBar in
CENTER should work.

- Mitch Gart
Matthias Kaeppler - 08 Jun 2005 18:21 GMT
> A BorderLayout with the JLabel in WEST position and the ProgressBar in
> CENTER should work.
>
> - Mitch Gart

Wouldn't that move the progress bar around each time the label's text is
altered (read: made longer)?
I need fixed positions, and for the label also a fixed size, like this:

+-------------------------+-------------------------------------------->
+ Label                   + Progress bar                             -->
+-------------------------+-------------------------------------------->

^-------------------------^ FIXED
                          ^-------------------VARIABLE-----------------^

Signature

Matthias Kaeppler

Andrew Thompson - 08 Jun 2005 18:38 GMT
mitch..(snip - BorderLayout solution)

> Wouldn't that move the progress bar around each time the label's text is
> altered (read: made longer)?

If you are altering the size of the text it might make sense for the
size of the label to increase as well.  

OTOH, if you are confident that your text will always fit within
a specific size, set that size (override getMinimum/MaximumSize as
needed) and use the BorderLayout as mitch suggested.  That should
have the effect you are after ( though I must admit I have not
carefully read the spec. of this UI as yet.. ;)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Roland - 08 Jun 2005 23:20 GMT
>>> I want to limit the size of a JLabel in another panel to X,Y.
>>> However, calling setMaximumSize( new Dimension(X,Y) ); has no effect.
[quoted text clipped - 37 lines]
> ProgressBar taking all the remaining space, what layout would be best to
> use for the Panel?

GridBagLayout, though this is not the easiest one. Here's a runnable
example:
import java.awt.*;
import java.text.*;
import java.util.Date;
import javax.swing.*;

public class Statusbar {
   public static final int FixedWidth = 120;
   public static void main(String[] args) {
      // final only for access in Runnable below
      final JLabel statusText = new JLabel();
      statusText.setText(" ");// for determining initial dimension
      statusText.setBorder(BorderFactory.createLoweredBevelBorder());

      Dimension d = statusText.getMinimumSize();
      d.width = FixedWidth;
      // leave min height as defined by the font
      statusText.setMinimumSize(d);

      d = statusText.getMaximumSize();
      d.width = FixedWidth;
      // leave max height as defined by the font
      statusText.setMaximumSize(d);

      d = statusText.getPreferredSize();
      d.width = FixedWidth;
      // leave prefd height as defined by the font
      statusText.setPreferredSize(d);

      JProgressBar statusProgress = new JProgressBar();
      statusProgress.setBorder(
            BorderFactory.createLoweredBevelBorder());
      statusProgress.setIndeterminate(true);

      JPanel statusBar = new JPanel();
      statusBar.setLayout(new GridBagLayout());

      // label on position 0,0
      statusBar.add(statusText, new GridBagConstraints(0, 0, 1, 1,
            0.0, 0.0,
            GridBagConstraints.SOUTHWEST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
      // progressbar on position 1,0
      statusBar.add(statusProgress, new GridBagConstraints(1, 0, 1, 1,
            1.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

      JFrame app = new JFrame();
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      app.getContentPane().setLayout(new BorderLayout());
      app.getContentPane().add(new JScrollPane(new JTextArea()),
            BorderLayout.CENTER);
      app.getContentPane().add(statusBar, BorderLayout.SOUTH);
      app.setSize(400, 300);
      app.setLocationRelativeTo(null); // center on screen;
      app.setVisible(true);

      // a thread to simulate changing status text
      new Thread(new Runnable() {
         public void run() {
            StringBuffer buf = new StringBuffer();
            DateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
            int dots = 0;
            while (true) {
               buf.setLength(0);
               buf.append(timeFormatter.format(new Date()));
               for (int i = 0; i < dots; i++) {
                  buf.append(" _");
               }
               statusText.setText(buf.toString());
               dots = (dots + 1) % 10;
               try {
                  Thread.sleep(100);
               } catch (InterruptedException ex) {
                  ex.printStackTrace();
                  return;
               }
            }
         }
      }).start();
   }
}

Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \



Free Magazines

Get 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 ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.