0

I'm having an issue with the JProgressBar, when it is printed in the GridBagLayout it is too little and the setSize method doesn't have effect, just to test I wrote 100 and 20 but the size remains something about 10x10 and I don't understand why, where is wrong? Thank you!

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ProgressDialog {
    private final JDialog dialogFrame;
    private JProgressBar progressBar;

    public ProgressDialog() {

        dialogFrame = new JDialog();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException ex) {
            System.err.println(ex.toString());
        }

        dialogFrame.setSize(200, 50);
        dialogFrame.setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.insets = new Insets(5, 5, 5, 5);

        JLabel headingLabel = new JLabel("Caricamento...");
        Font f = headingLabel.getFont();
        f = new Font(f.getFontName(), Font.BOLD, f.getSize());
        headingLabel.setFont(f);
        headingLabel.setOpaque(false);
        dialogFrame.add(headingLabel, constraints);
        dialogFrame.setUndecorated(true);

        // Button
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 0;
        constraints.weighty = 0;

        JButton xButton = new JButton("X");
        xButton.setMargin(new Insets(1, 4, 1, 4));
        xButton.setFocusable(false);
        dialogFrame.add(xButton, constraints);

        // Progress bar
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.gridwidth = 2;

        progressBar = new JProgressBar();
        progressBar.setMaximum(100);
        progressBar.setMinimum(0);
        progressBar.setSize(100, 20);
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(true);
        dialogFrame.add(progressBar, constraints);

        dialogFrame.setShape(new RoundRectangle2D.Double(1, 1, 200, 50, 20, 20));
        dialogFrame.setVisible(true);


        Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
        Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(dialogFrame.getGraphicsConfiguration());
        dialogFrame.setLocation(scrSize.width - 5 - dialogFrame.getWidth(), scrSize.height - 5 - toolHeight.bottom
                - dialogFrame.getHeight());

        xButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialogFrame.dispose();
            }
        });

    }

    public void destroy() {
        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    for (float i = 1.00f; i >= 0; i -= 0.01f) {
                        dialogFrame.setOpacity(i);
                        Thread.sleep(15);
                    }
                    dialogFrame.dispose();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        }.start();

    }

    public void set(int n) {
        progressBar.setValue(n);
        progressBar.setString(n + "");
    }

    public static void main(String[] args) {
        new ProgressDialog();
    }

}

The main is to test it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kaos
  • 107
  • 1
  • 1
  • 10
  • Most layout managers respect the preferred size, not the size. – Hovercraft Full Of Eels Mar 27 '15 at 19:03
  • I tried it but with something like `Dimension dim = new Dimension(); dim.width = 100; dim.height = 20; progressBar.setPreferredSize(dim);` still not working... – Kaos Mar 27 '15 at 19:08
  • 1
    You could try setting the minimum size. – javac Mar 27 '15 at 19:16
  • 2
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 28 '15 at 02:28
  • Nice.. then I should try to change layoutmanager since that I don't see a solution without using setminimum size. However thank you for that info, I didn't know it! – Kaos Mar 28 '15 at 10:14

0 Answers0