2 Replies Latest reply on Nov 12, 2011 8:37 AM by 889476

    Custom Swing Component - Gaussian shaped JProgressBar

    889476
      I've been using Java for a couple of years now, and I'd like to create a custom Swing component. In particular I want something like a "JProgressBar", with the following changes:

      1. In place of a flat "setForeground" color, I'd like a rainbow spectrum, showing only the red range for low "setValue"s, and the whole spectrum for values close to "setMaximum";
      2. Instead of a rectangualar box, I'd like the progress "bar" to take the shape of a normal distribution curve;
      3. I'd like progress to be displayed by area under the curve, rather than by a simple linear scale along the x axis.

      If anyone has already created such a component, or knows of one which has been created as part of an open source project, I'd be jolly grateful if they could point me to it.

      Failing that, I have created a "class" (which extends "JPanel"), which runs from the command line to display a nice rainbow filled normal distribution curve, but I am stumped when it comes to integrating it with Swing.

      In particular, when you use regular Swing Components in a "GridBagLayout", they size themselves to fit the layout. And when you change a parameter (like "setValue"), they redraw themselves with the new parameter without being explicitly told to do so.

      I know there is material out there, and I should probably plough through it before posting, but if anyone here has a favourite tutorial/article, specifically addressing the issues of resising in a "GridBagLayout", and redrawing, I'd be grateful for any pointers.

      Edited by: 886473 on 11-Nov-2011 01:12 - typo
        • 1. Re: Custom Swing Component - Gaussian shaped JProgressBar
          jduprez
          In particular, when you use regular Swing Components in a "GridBagLayout", they size themselves to fit the layout.
          Not exactly. The layout manager sizes them, and the component take their current size into account when painting themselves.
          So the problem is "only" the latter part: see http://download.oracle.com/javase/tutorial/uiswing/painting/index.html
          And when you change a parameter (like "setValue"), they redraw themselves with the new parameter without being explicitly told to do so.
          Most probably because the setter calls repaint(), or sends a PropertyChangeEvent that the component's UI handles by calling repaint().
          • 2. Re: Custom Swing Component - Gaussian shaped JProgressBar
            889476
            Many thanks.

            I'll work through that link and post a new thread if anything else arises.