0

I am trying to re-size my progress bars to better suit how i wish my interface to look, however no matter what I change the height and width to, no change happens at all.

Below is the code for one of my progress bars, there are 10 in total... I have set the width/height to 5px to 300px and so on, just to see if any change is happening.

 barPanel.setLayout(new GridLayout(0 , 4));
 healthBar = new JProgressBar (0,100);
 healthLabel = new JLabel ("Health: ");
 healthBar.setPreferredSize( new Dimension (width, height));
 healthBar.setMaximumSize(new Dimension(width,height));
 healthBar.setMinimumSize(new Dimension(width, height));
 barPanel.add(healthLabel);
 healthLabel.setForeground(Color.white);
 barPanel.add(healthBar);

I have tried switching between grid and border layout, but I am pretty new to interface designing... very new, if anybody can help it would be greatly appreciated and if any more code fragments are required, I'll be glad to add them.

I would fully upload my solution, but its for a university assignment, at a later date I will fully update it.

It is just an adapted version of the solution before, but I created 4 panels and placed them within one parent panel.

Two panels for the JLabels and 2 panels for the progress bars. Then within these panels I added my JLabels and progress bars and sized and position them using the bounds method ie. barPanel.setBounds(10,10,300,25); (X, Y, width, height)

Sorry I cant give the full solution at the moment.

user3383620
  • 31
  • 1
  • 6
  • 1) What do you want to happen. 2) What actually happens? – Duncan Jones Apr 22 '14 at 17:31
  • I want their size to change, but no matter what I set the height and width too, nothing happens to their size, sorry will specify that in post now. – user3383620 Apr 22 '14 at 17:33
  • Have you read [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)? Layout managers are a complex topic and worthy of some background reading before you attempt to use them. – Duncan Jones Apr 22 '14 at 17:47
  • Thanks for the reference, I'll give that a read. – user3383620 Apr 22 '14 at 18:19
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Apr 23 '14 at 01:21

1 Answers1

3

If you want to Resize the Progress bar in GridLayout, then it may help here :

barPanel.setLayout(new GridLayout(0 , 4,10,10));//Change Horizontal and Vertical gap like this

but may not give size what you want exactly it simply increase the gap between components.

Or other way

If you really interested in making ui, more better then create panel which will keep the progressbar only and set the label and new panel with existing barpanel. Still the size of the Progressbar will be streched becs default layout of the panel is FlowLayout , and you can resize it using setPreferredSize( new Dimension (width, height)). You need to use the utility of Layout manager to design effectively.

Your modified Code should look like this:

    barPanel.setLayout(new GridLayout(0 , 4));
     JPanel newPanel=new JPanel();

     healthBar = new JProgressBar (0,100);
     healthLabel = new JLabel ("Health: ");
      newPanel.add(healthBar);
      healthBar.setPreferredSize( new Dimension (width, height));
     //healthBar.setMaximumSize(new Dimension(width,height));
     //healthBar.setMinimumSize(new Dimension(width, height));

     barPanel.add(healthLabel);
     healthLabel.setForeground(Color.white);

 barPanel.add(newPanel);
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
  • Thank you, your first option did improve the look, but I'm going to attempt the second option, might take a little while but I'll feedback if I can do it :) – user3383620 Apr 22 '14 at 18:10
  • @Andrew Thompson , i was mistaken about layout compatibily, thanks i have edited my answer. – Raju Sharma Apr 23 '14 at 06:40
  • 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 Apr 23 '14 at 06:41
  • Yeah, I got there in the end, I'll add what I did to my post. This really helped me get there in the end, many thanks. – user3383620 Apr 23 '14 at 20:22