Right to left (backwards) JProgressBar

My app needs to display a graphical representation of a value ranging from -1 to 1. Negative values should be in one color, with positive values in another. Zero is in the center and shows nothing. (If it helps, the particular use here is to display the relative weight of buy and sell orders in a financial application.)

I would ideally like to use a pair of JProgressBars for this, however the Swing control starts (at its minimum) at the left. The standard control only supports two orientations, left-right or bottom-top. Passing in negative values doesn't help. My question is, what is the quickest way to achieve this effect?

Subclassing JProgressBar would involve re-implementing it almost entirely. Using a JFreeChart bar chart seems like a great deal of code (and effort) for a relatively trivial task. I could perhaps make a small, square-celled JTable and fill it in, but again that's a lot of code. What would you suggest?


Umm, maybe this won't work at all, but how about making a sub-class of JProgressBar and overriding paintComponent() to something like this:

protected void paintComponent(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;

I'd test it if I was more lucid and awake.

Edit: While rotating it might work too, I found it easier to scale it and then translate, as follows:

protected void paintComponent(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.scale(-1, 1); //Flips over y-axis
  g2d.translate(-getWidth(), 0); //Moves back to old position.

Using JFreeChart or subclassing JProgressBar seems like overkill, but overriding paintTrack() in BasicSliderUI may be effective. Here's an example.

I had a look at JFreeChart the other day and for what you want to do it looks like complete overkill.

As this seems as if it would be fairly easy to draw procedurally, I'd just make a class extending JComponent, override the paint method to draw a rect in it, and add it as a custom widget.

Swap the background and foreground colors and the regress will look like a progress to the left.

Following code does the job:


Need Your Help

Setting up behavior on a method appears to be calling the method

autofac typemock

I posted this on the TypeMock forums, but am too impatient to wait for a response there. This is a very n00b question.

Why when I create new xml layout the control in palette change to old version of control in Android?

android android-layout palette android-version android-controls

First I create my project that minSdkVersion="13" and targetSdkVersion="15". But when I create new xml for layout, the control of the Palette at the left side show the old version of control. And W...