Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

JPanel setPreferred width

Posted on 2011-09-27
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.

Question by:dshrenik
  • 6
  • 2
LVL 86

Expert Comment

ID: 36713400
LVL 47

Expert Comment

ID: 36713417

Author Comment

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

preferredSize() is deprecated.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 47

Expert Comment

ID: 36713427

you can use
public Dimension getPreferredSize()

LVL 47

Expert Comment

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

Expert Comment

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
LVL 47

Expert Comment

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
LVL 47

Accepted Solution

for_yan earned 250 total points
ID: 36713452

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

Assisted Solution

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
Custom Java Application connects to database: Which one of the 20? 4 35
ejb stateless example 2 23
passing enum to a method 4 20
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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