3

I don't know why the background color is not showing on my Jframe. Below is the code that I tried.

When I call

AnimatedDialogBox animatedDialogBox = new AnimatedDialogBox("Saving TransSet form", dataSheetTable);

Its not showing the exact color that I needed. It shows without any background color. The AnimatedDialogBox class is per below :

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import net.miginfocom.swing.MigLayout;


public class AnimatedDialogBox {

    private JFrame progressDialog ;
    private JProgressBar bar;
    private Task task;
    private ResourceManager resourceManager = new ResourceManager();

    public AnimatedDialogBox(String message, JComponent parentComponent) {
        progressDialog = new JFrame( message);
        progressDialog.setLayout(new MigLayout());
        progressDialog.setUndecorated(true);
          progressDialog.setBackground(resourceManager.getColor("error.Panel.background")); // RGB = 243, 255, 159
        progressDialog.setPreferredSize(new Dimension(300, 100));
        JLabel label = new JLabel(message);
        label.setBackground(resourceManager.getColor("error.Panel.background"));
        progressDialog.add(label, "gapbefore 80,gapbottom 30, wrap");
        bar = new JProgressBar(0, 100);
        bar.setIndeterminate(true);
        bar.setBackground(resourceManager.getColor("error.Panel.background"));
        progressDialog.add(bar, "gapbefore 80, gapbottom 30, wrap");
        progressDialog.setFocusableWindowState(false);
        Point point = progressDialog.getLocation();
        Dimension cDim = parentComponent.getSize();

        progressDialog.setLocation((int) (cDim.getWidth() / 2)-100,
                    (int) cDim.getHeight() + 350);

        progressDialog.pack();

        task = new Task();
        task.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equalsIgnoreCase("progress")) {
                    int progress = task.getProgress();
                    if (progress == 0) {
                        bar.setIndeterminate(true);
                    } else {
                        bar.setIndeterminate(false);
                        bar.setValue(progress);
                        progressDialog.dispose();
                    }
                }
            }
        });
        task.execute();
        progressDialog.setVisible(true);
    }

    class Task extends SwingWorker<Void, Void> {
        private static final long SLEEP_TIME = 1000;

        public Task() {
        }

        @Override
        public Void doInBackground() {
            setProgress(0);
            try {
                Thread.sleep(SLEEP_TIME);// imitate a long-running task
            } catch (InterruptedException e) {
            }
            setProgress(100);
            return null;
        }

        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
        }
    }
}

2 Answers2

3

The background color of a JProgressBar is determined by its UI delegate, ProgressBarUI. For example,

UIManager.put("ProgressBar.background", Color.red);

Not all implementations use the color; for example, com.apple.laf.AquaProgressBarUI ignores the setting in favor of the appearance seen here. As an alternative, you may want to consider a tinted background or Border on the enclosing panel, as suggested here.

Also note that you can update the GUI from the process() method, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

A few ideas:

  1. You're not supposed to add components directly to a JFrame. Instead, your content should go in the "content pane". See JFrame.setContentPane(), JFrame.getContentPane(). You may want refer to the JFrame documentation or a tutorial.

  2. If a component is not opaque, then its background will not be rendered. See JComponent.setOpaque(), Component.isOpaque().

JimN
  • 3,120
  • 22
  • 35
  • `JFrame#add()` _forwards_ to the `contentPane`. – trashgod Nov 20 '12 at 03:05
  • 1
    +1 to idea related to `contentPane`, though I guess the only thing missing is `JFrame` overrides only three methods for `contentPane` i.e. `setLayout(), remove() and add()`. So I guess inorder to change the colour what one needs is `frame.getContentPane().setBackground(Color.RED);`. This will change the background colour of the said thingy. – nIcE cOw Nov 20 '12 at 05:10
  • 1
    @GagandeepBali: Good insight; for [`setBackground()`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setBackground%28java.awt.Color%29), "It is up to the look and feel to honor this property, some may choose to ignore it." – trashgod Nov 20 '12 at 05:50