Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDialog and repaint() / revalidate()

843806Jun 15 2008 — edited Nov 28 2009
Bah!!! how many times i face this same issue. JDialog not painting properl. Wonder when is Sun finally going to fix this.
So the present problem. I have a JDialog with a JLabel in the center and some buttons in the BorderLayout.SOUTH
Periodically i update the center component which need not be a JLabel always (Smart people you got it right this is a splash screen)

Everytime i update the center component the old components are still hanging around looking as if it is just scribbling labels over the labels again

Following is the shortened code. Could someone please help me get the Label render properly.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class ProgressDialog extends JDialog {
    private static final Dimension DEFAULT_DIMENSION = new Dimension(400, 120);
    private JProgressBar progress;
    private JButton btnExit;

    public ProgressDialog(JFrame parent,
            String title,
            boolean isModal
//            Canceler canceler
            ) {

        super(parent, title);
        
//        this.canceler = canceler;
        setModal(isModal);
        initComponents();
        placeComponents();
        addListeners();
        
        setResizable(true);
        setSize(DEFAULT_DIMENSION);
    }

    public ProgressDialog(JFrame parent, String title) {
        this(parent, title, true);
    }

    private void initComponents() {
        progress = new JProgressBar();
        progress.setIndeterminate(true);
        progress.setStringPainted(true);
        progress.setString("Test in Progress");
        progress.setValue(0);
        progress.setVisible(true);

        btnExit = new JButton("Cancel");
    }
    
    private void placeComponents() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(btnExit);
        
        JPanel southPanel = new JPanel(new BorderLayout());
        southPanel.add(buttonPanel, BorderLayout.NORTH);
        southPanel.add(progress, BorderLayout.SOUTH);
        
//        JPanel contentPane = (JPanel) getContentPane();
        JPanel contentPane = new ProgressPane();
        setContentPane(contentPane);
        contentPane.add(southPanel, BorderLayout.SOUTH);
        
        this.setLocationRelativeTo(getParent());
    }

    private void addListeners() {
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                cancel();
            }
        });
        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt) {
                cancel();
            }
        });
    }

    private void cancel() {
        dispose();
    }
    
    private void initDeterminateProgress() {
        progress.setMaximum(100);
        progress.setMinimum(0);
        progress.setValue(5);
    }
    
    public void setIndeterminate(boolean value) {
        progress.setIndeterminate(value);
        if(value == false) {
            initDeterminateProgress();
        }
    }

    public void setString(String string) {
        progress.setString(string);
    }
    
    public void setDisplayComponent(JComponent component) {
        getContentPane().add(component, BorderLayout.CENTER);
        ((JPanel)getContentPane()).revalidate();
    }

    private void setProgress(int i) {
        progress.setValue(i);
        if(i >= progress.getMaximum()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // Do nothing if the sleep is interrupted 
            }
            dispose();
        }
    }

    private void setMaximum(int i) {
        progress.setMaximum(i);
    }

    private void setMinimum(int i) {
        progress.setMinimum(i);
    }
    
    public static void main(String[] args) throws InterruptedException {
        ProgressDialog dialog = new ProgressDialog(null, "Hello", false);
        dialog.setIndeterminate(false);
        dialog.setVisible(true);
        
        Thread.sleep(1000);
        dialog.setMinimum(0);
        dialog.setMaximum(75);
        dialog.setDisplayComponent(new JLabel("Hello do you want"));
        dialog.setProgress(25);
        Thread.sleep(1000);
        dialog.setDisplayComponent(new JLabel("What a crap!! "));
        dialog.setProgress(40);
        Thread.sleep(1000);
        dialog.setDisplayComponent(new JLabel("Hello do you want to really proceed with "));
        dialog.setProgress(50);
        Thread.sleep(1000);
        dialog.setDisplayComponent(new JLabel("If you are still insane enough to say then then only lord can save you !!"));
//        dialog.setProgress(75);
        
    }

}

class ProgressPane extends JPanel {
    
    private static final long serialVersionUID = 1L;

    public ProgressPane() {
        super(new BorderLayout());
    }
    
    public void revalidate() {
//        super.validateTree();
//        super.validate();
//        super.repaint();
        super.revalidate();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2009
Added on Jun 15 2008
7 comments
1,258 views