4

I want to resize my JProgresbar when I resize my JFrame

the size of my JProgresBar is calculated like this:

progressBar.setPreferredSize(new Dimension(getWidth() - 100, 50));

but when I resize my JFrame the size of the progressbar will always stay the same.

UPDATE 1

This is my code:

public class MinimalExampleJProgresbar {
    public static void main(String[] args) {
        JFrame frame = new JFrame("SimpleFrame");
        JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JProgressBar progressBar = new JProgressBar();

        jPanel.add(progressBar);
        frame.add(jPanel, BorderLayout.PAGE_START);

        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        progressBar.setPreferredSize(new Dimension(frame.getWidth() - 100, 50));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
vanlooverenkoen
  • 2,121
  • 26
  • 50

2 Answers2

3

Use layout managers to your advantage. For example, a JFrame's contentPane uses BorderLayout. If you add your JProgressBar to this, to the BorderLayout.PAGE_END or the BorderLayout.PAGE_END position, it will automatically resize when its container resizes.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SimpleFrame {
   private static void createAndShowGUI() {
      final JProgressBar progressBar = new JProgressBar(0, 100);
      progressBar.setBorder(BorderFactory.createTitledBorder("My Progress"));
      new Timer(200, new ActionListener() {
         int value = 0;

         @Override
         public void actionPerformed(ActionEvent e) {
            value += 10;
            if (value > 100) {
               value = 0;
            }
            progressBar.setValue(value);
         }
      }).start();


      JFrame frame = new JFrame("SimpleFrame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(progressBar, BorderLayout.PAGE_START);
      frame.add(Box.createRigidArea(new Dimension(400, 300)));
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

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

public class MinimalExampleJProgresbar2 {
   public static void main(String[] args) {
       JFrame frame = new JFrame("SimpleFrame");
       JPanel wrapperPanel = new JPanel(new BorderLayout());

       JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
       JProgressBar progressBar = new JProgressBar();

       wrapperPanel.add(progressBar, BorderLayout.PAGE_START);
       wrapperPanel.add(jPanel, BorderLayout.CENTER);

       frame.add(wrapperPanel, BorderLayout.PAGE_START);

       frame.setExtendedState(Frame.MAXIMIZED_BOTH);
       frame.setVisible(true);
       progressBar.setPreferredSize(new Dimension(frame.getWidth() - 100, 50));
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
}

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import javax.swing.*;

public class MinimalExampleJProgresbar2 {
   public static void main(String[] args) {
       JFrame frame = new JFrame("SimpleFrame");
       JPanel wrapperPanel = new JPanel(new BorderLayout());

       JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
       final JProgressBar progressBar = new JProgressBar();

       new Timer(200, new ActionListener() {
          int value = 0;

          @Override
          public void actionPerformed(ActionEvent e) {
             value += 10;
             if (value > 100) {
                value = 0;
             }
             progressBar.setValue(value);
          }
       }).start();

       JPanel boxUser = new JPanel();
       boxUser.setLayout(new BoxLayout(boxUser, BoxLayout.LINE_AXIS));
       boxUser.add(Box.createHorizontalStrut(50));
       boxUser.add(progressBar);
       boxUser.add(Box.createHorizontalStrut(50));


       wrapperPanel.add(boxUser, BorderLayout.PAGE_START);
       wrapperPanel.add(jPanel, BorderLayout.CENTER);

       frame.add(wrapperPanel, BorderLayout.PAGE_START);

       frame.setExtendedState(Frame.MAXIMIZED_BOTH);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • my component is added to a JPanel that is set on the PAGE_START. The layoutmanager of the JPanel is FlowLayout.Left – vanlooverenkoen Oct 16 '15 at 11:26
  • 1
    @KoenVanLooveren: don't do that, don't add it to a FlowLayout-using container. You can nest JPanels, each using a different layout, and this way solve your problem. If you need further help, create and post your [mcve], similar to the code I've posted above. – Hovercraft Full Of Eels Oct 16 '15 at 11:32
  • @KoenVanLooveren: and so have I :) – Hovercraft Full Of Eels Oct 16 '15 at 12:18
  • No, this is still the same problem, I want a space of 50 on the right and on the left. If you run my code there is a space. In your sollution there isn't a space – vanlooverenkoen Oct 16 '15 at 12:23
  • Oke yes, I see, but in this example I can't set the height of the progressbar? thank you so much for helping me out – vanlooverenkoen Oct 16 '15 at 12:39
  • I want to make the height for now 50 (nice and big) :D, and maybe in the future lower or heigher – vanlooverenkoen Oct 16 '15 at 12:43
  • Hovercraft Full Of Eels, you are absolutely right - my solution is a kludge, and I respect all the work you've put into answering this question in the right way. Nowadays, I'm working in javascript much more than Java, and javascript is all about the right framework packages with a kludge on top :) As far as swing, I think by and large people are using this for fun and to learn programming (ie not for professional work) so sometimes, like here, I'll offer a shorter, "kludgy" solution. Just FYI – ControlAltDel Oct 16 '15 at 13:45
  • @ControlAltDel: call me a purist or a Luddite, but I do use Swing for some serious work. – Hovercraft Full Of Eels Oct 16 '15 at 13:53
  • @HovercraftFullOfEels ... And I respect that! – ControlAltDel Oct 16 '15 at 14:30
  • @HovercraftFullOfEels how can I change the height of the progresbar in the boxlayout? – vanlooverenkoen Oct 16 '15 at 15:00
  • @KoenVanLooveren (1) Add `boxUser` panel directly to the frame. (2) Set the maximum size of the progress bar with a height of 50. – user1803551 Oct 16 '15 at 17:57
  • @user18 most layout managers don't respect maximum or minimum sizes. Better to override getPreferredSize in a smart way. – Hovercraft Full Of Eels Oct 16 '15 at 20:56
1

Add a ComponentAdapter to your Frame

myFrame.addComponentListener(new ComponentAdapter() {
  public void componentResized(ComponentEvent ev) {
    progressBar.setPreferredSize(new Dimension(getWidth() - 100, 50));
    revalidate();
    repaint();
  }
});
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • @KoenVanLooveren: I didn't down-vote this answer, but it is more of a kludge when better solutions exist. For the best solution, again, show your code, your [mcve]. – Hovercraft Full Of Eels Oct 16 '15 at 11:33
  • yes Oke but in your solution, the progresbare lenght cannot be calculated like how I want it, and with the solution of @ControlAltDel it is possible – vanlooverenkoen Oct 16 '15 at 11:40
  • @KoenVanLooveren: with my solution, the progress bar length does not **need** to be calculated. Again, why not create and post your [mcve]? Again, why not lets work with your code to find an optimal solution? – Hovercraft Full Of Eels Oct 16 '15 at 11:45
  • I still see this as the best way to go, ... but thanks for your reactions – vanlooverenkoen Oct 18 '15 at 14:43