JPanel components don't set in the correct position

152 views Asked by At

I have a JPanel that has 3 components in it. When I add the JPanel to a JFrame and make the frame visible the component's don't fit in the right place.

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class ProgressPanel extends JPanel{
    private final ProgressBar progressBar = new ProgressBar();
    private final JLabel downloadDesc = new JLabel("downloading...");

    public ProgressPanel() {
        setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
        setBackground(ClientGui.BACKGROUND_COLOR);

        JLabel instructionsLabel = new JLabel("<html>Downloading your requested file. Please stand by, This might take a while<html>");
        instructionsLabel.setFont(new Font("Arial", Font.BOLD, 25));
        instructionsLabel.setBorder(new EmptyBorder(20, 0, 10, 0));
        instructionsLabel.setForeground(ClientGui.TEXT_COLOR);
        instructionsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        downloadDesc.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
        downloadDesc.setBorder(new EmptyBorder(10 , 0 , 0 , 10));

        add(progressBar);
        add(downloadDesc);
        add(instructionsLabel);
    }

    public void setText(String text){
        downloadDesc.setText(text);
    }

    public void fill(){
        progressBar.fill();
    }

    public void setMax(int max) {
        progressBar.setMax(max);
    }

    private static class ProgressBar extends JProgressBar{
        private int max;
        private int currentValue = 0;

        public ProgressBar(){
            setValue(0);
            setStringPainted(true);
        }

        public void fill(){
            currentValue++;

            setValue(calculatePercentage());
        }

        public void setMax(int max) {
            currentValue=0;
            this.max = max;
        }

        private int calculatePercentage(){
            return ((currentValue / max) * 100);
        }
    }
}

https://i.stack.imgur.com/Lg4Ny.png

The JLabel should be at the far right, even without setting a border it still doesn't work. It even doesn't let me resize the ProgressBar

0

There are 0 answers