简体   繁体   中英

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:

@Override
protected void paintComponent(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.rotate(Math.PI);
  super.paintComponent(g2d);
}

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:

@Override
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.
  super.paintComponent(g2d);
}

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.

交换背景和前景色,回归看起来像左边的进度。

以下代码完成工作:

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM