Solved

JPanel setPreferred width

Posted on 2011-09-27
9
291 Views
Last Modified: 2012-05-12
Can you tell me the alternative to preferredSize()? It is deprecated in the current version of java.

I want to do this:
chartPanel.setSize(chartPanel.preferredSize().width, 100);
chartPanel is a JPanel.

Thanks!
0
Comment
Question by:dshrenik
  • 6
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36713400
setPreferredSize
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36713417
0
 

Author Comment

by:dshrenik
ID: 36713420
But I want to get the preferred size and use the width property, and change the height.

preferredSize() is deprecated.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 47

Expert Comment

by:for_yan
ID: 36713427

you can use
public Dimension getPreferredSize()

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36713430
and then Dimension.width
will give you the width
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36713436
after you get Dimension you can create a new Dimension(olddim.width+some, olddim.height)
and set it as a new new preferred szie
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36713444
In general don't be afraid so much of yusing deprecated if it is conveninent to you - it writes the compiler warning,
but it works
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36713452


I think this should be equivalent:
chartPanel.setSize(chartPanel.getPreferredSize().width, 100);
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 36713456
Avoid deprecated methods (they're not deprecated for fun ;))

Just add to the width or height as you see fit, having got the Dimension with getPreferredSize
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

776 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