[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fix height / width of a JComponent

Posted on 2004-11-04
11
Medium Priority
?
446 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:cathalmchale
  • 4
  • 4
  • 3
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 12492704
You should override

getMaximumSize
getMinimumSize
getPreferredSize
0
 

Author Comment

by:cathalmchale
ID: 12492721
?? example - how does this mean u only have to set 1 and not the other?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12492782
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cathalmchale
ID: 12492841
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12492896
>>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
 

Author Comment

by:cathalmchale
ID: 12493003
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12493155
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12493166
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12493171
Maybe you could post your panel's layout...
0
 

Author Comment

by:cathalmchale
ID: 12493411
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12493423
8-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question