How to create a JProgress panel with a gradient

I use swing and want my JProgress panel to look more attractive by adding a soft gradient to it that matches the application theme.

My progress bar is in a JTable cell in which I have work, but my current example draws a cell with a gradient instead of the JProgress panel itself. I would like the cell to be white, but the progress indicator is from light gray → to dark gray when it becomes more complete.

Open to not use the JProgress panel if something else does the trick. (Perhaps just using the cell renderer?)

My cell rendering

class JPBRenderer extends JProgressBar implements TableCellRenderer {

Batch batch;

public JPBRenderer() {
    super();
    setStringPainted(true);
    setBorderPainted(false);
    setOpaque(false); //EDIT 1
    setForeground(UIConfig.backgroundColor);
}

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    batch = ((BatchTableModel)table.getModel()).getBatchForRow(row);
    setMinimum(0);
    setMaximum(batch.getQuantityToProduce());
    setValue(batch.getQuantityCompleted());
    setString(GeneralUtils.formatNumber((batch.getQuantityCompleted()/batch.getQuantityToProduce())*100, 1) + " %");
    return this;
}

@Override
protected void paintComponent(Graphics g) {

    Color color2 = UIConfig.backgroundColor;
    Color color1 = UIConfig.backgroundColor.brighter().brighter();
    double value = 1;
    if(batch != null){
        value = batch.getQuantityCompleted()/batch.getQuantityToProduce();
    }
    int w = getWidth();
    int h = getHeight();
    Graphics2D g2d = (Graphics2D) g;

    GradientPaint gp = new GradientPaint(0, 0, color1, w, 0, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);

    super.paintComponent(g);
}
}

The My Batch class contains ...

public class Batch {
    private Integer id;
    private BigDecimal length;
    private int quantityToProduce;
    private int quantityCompleted;

    //Constructors and getters...
}

Thanks in advance!

+3
source share
1 answer

ProgressBarUI, , BasicProgressBarUI, . paintDeterminate() . A LinearGradientPaint, BasicSliderUI, .

ProgressIcon, .

+3

All Articles