Fix height / width of a JComponent

How do I fix the height OR the width of a JComponent.
i.e. i know the height will never be more than 16 pixels, but i want to let the JComponent decide its own width?

the setSize insists you specify both
cathalmchaleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You should override

getMaximumSize
getMinimumSize
getPreferredSize
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cathalmchaleAuthor Commented:
?? example - how does this mean u only have to set 1 and not the other?
0
CEHJCommented:
Well it means that you should implement it as you need. If you know the height, you return the width value based on your application logic (in your case, i suspect the width necessary to render a polygon)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

cathalmchaleAuthor Commented:
>> If you know the height, you return the width value based on your application logic (in your case, i suspect the width necessary to render a polygon)

??
I have a jlabel with a 16*16 icon  and text which varies (but not too long)
so i could say the height is 16, or 17 or 18 - and fix the label panel at this height, then regardless of how the label is changing, the overall contentPane of my window wont be constantly shifting!
  Is it the same solution? i still dont fully understand?
0
CEHJCommented:
>>and fix the label panel at this height

Yes. Then you'd calculate the width based upon the width necessary to display the "text which varies"
0
cathalmchaleAuthor Commented:
this doesnt appear to do anything!

JPanel content = new JPanel();
      content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
      // file already exists / invalid name label
      JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
      fileProblem = new JLabel()
      {
        public Dimension getMinimumSize()
        {
          return new Dimension(50, 40);
        }
      }; //"<html><font color=black><b>" + "&nbsp" + "</font><html>"
      labelPanel.add(fileProblem);
      content.add(labelPanel);
      // the title page content

the screen still jumping about - doesnt seem to have had any effect
      content.add(titlePage);
0
zzynxSoftware engineerCommented:
This should fix the height to 17:

   yourLabel.setPreferredSize( new Dimension(100, 17) );
   yourLabel.setMinimumSize( new Dimension(10, 17) );
   yourLabel.setMaximumSize( new Dimension(Integer.MAX_VALUE, 17) );
0
zzynxSoftware engineerCommented:
But, as already said: you should adapt your layout in such a way that changing the text of your label doesn't make your panel/screen/whatever "jump around"
0
zzynxSoftware engineerCommented:
Maybe you could post your panel's layout...
0
cathalmchaleAuthor Commented:
using this thanks ;-)

JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT))
      {
        public Dimension getMinimumSize()
        {
          return new Dimension(fileProblem.getWidth(), 25);
        }
        public Dimension getPreferredSize()
        {
          return getMinimumSize();
        }
      };
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.