aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes percentage based background scheme for a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "percentage based background scheme for a JPanel" Watch "percentage based background scheme for a JPanel" New topic
Author

percentage based background scheme for a JPanel

thushar kumar
Greenhorn

Joined: May 14, 2007
Posts: 9
Hi,

my requirement is as follows,

I would like to associate a JPanel with a value. This is in a visual way. For that the idea i had in mind is to have half of the JPanel set with a colour if the value is 50%, 1/4th set if value is 25% and so on. These are small JPanels grouped together to form a grid. So the idea is that the user can guess the value of a grid by looking at the percentage area of the panel thats been coloured. Is there such an instance of JPanel/or similar in Swings?

thanks in advance,
Thushar
Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Hi Thushar,

You need to create a subclass of JPanel and override the paint() method. Here is a quick example class:



Hope that helps!


Life is good on the Ponderosa,<br />but mind where you step.
thushar kumar
Greenhorn

Joined: May 14, 2007
Posts: 9
thanks Darrin...this looks really helpful
thushar kumar
Greenhorn

Joined: May 14, 2007
Posts: 9
hi darrin,

your code worked exquisitely...But i discovered a new problem. Let me explain. Consider a Jpanel to represent the duration of 1 hr. The percentage based design was to shade the Jpanel based on how much of the 1 hr is to be represented. The hitch now is that, it may be required to display several ranges within an hour itself. For example 10:10-10:15, 10:20-10:30, 10:45-10:50. Is there any way to implement this. If nothing works out, i am planning to have a combined shade for all these ranges and provide a mouse tool tip to display the details.

do help

thanks,
thushar
Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Hi Thushar,

You just need a way to represent the ranges in the hour time period. Use a different color for each, and change the paint() method to change colors as needed. This will be easier if you can specify that no ranges overlap.
I would probably create a new class to hold the range information. You can define a maximum number of ranges, an array of colors to use when drawing, add setters and getters for each range that verify they do not overlap, etc... Then, include an instance of the new range class in each panel, and access it in the paint method.

Good luck!

Darrin
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
 
subject: percentage based background scheme for a JPanel