Results 1 to 7 of 7

Thread: Sizing Components? [resolved]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Sizing Components? [resolved]

    Does anyone know how i can go about adding buttons to a
    container in a single vertical column where all the buttons have
    the same size regardless of there text? I tried The setPerferredSize() and setMaximumSize() methods from the JComponent class but i dont get the results i want....

    Thanks....

    JFrame jf = new JFrame("TestFrame");


    Container content = jf.getContentPane();
    content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));

    JButton Big = new JButton("Big");
    JButton Bigger = new JButton("Bigger");
    JButton Even Bigger = new JButton("Even Bigger");
    JButton Really really Big = new JButton("Really really Big ");
    JButton = The Biggest of the Big new JButton(" The Biggest of the Big");

    content.add(Big);
    content.add(Bigger);
    content.add(Even Bigger);
    content.add(Really really Big);
    content.add(The Biggest of the Big);

  2. #2

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I just felt like posting this again....... I posted this a while ago so maybe ill get a response this time.

  4. #4
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    I think this is what you want!
    The important part is the this.pack() command. It packs the components down to the prefferedSize dimension.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class VBWorld_Test extends JFrame 
    {
    	
    	public VBWorld_Test() 
    	{
    		super ("This is the title");
    		
    		//create the panel we add to the root content pane
    		JPanel thePane = new JPanel(new GridLayout(5,1));
    		
    		//create all the buttons
    		JButton big = new JButton("Big"); 
    		JButton bigger = new JButton("Bigger"); 
    		JButton evenBigger = new JButton("Even Bigger"); 
    		JButton reallyBig = new JButton("Really really Big "); 
    		JButton biggest = new JButton(" The Biggest of the Big"); 
    		
    		//create the dimension that they are all going to have
    		Dimension allTheSame = new Dimension(200, 50);
    		
    		//set there preffered size to the dimension
    		big.setPreferredSize(allTheSame);
    		bigger.setPreferredSize(allTheSame);
    		evenBigger.setPreferredSize(allTheSame);
    		reallyBig.setPreferredSize(allTheSame);
    		biggest.setPreferredSize(allTheSame);
    		
    		//add the buttons to the panel
    		thePane.add(big); 
    		thePane.add(bigger); 
    		thePane.add(evenBigger); 
    		thePane.add(reallyBig); 
    		thePane.add(biggest);
    		
    		//add the panel to the content pane
    		this.getContentPane().add(thePane, BorderLayout.CENTER);
    		
    		//add in the window listener so it close properly
    		this.addWindowListener(new WindowAdapter() 
    		{
    			public void windowClosing(WindowEvent e) 
    			{
    				dispose();
    				System.exit(0);
    			}
    		});
    		//stop it resizing
    		this.setResizable(false);
    		//pack it down to preffered sizes
    		this.pack();
    		//show it
    		this.setVisible(true);
    	}
    
    	public static void main(String args[]) 
    	{
    		VBWorld_Test mainFrame = new VBWorld_Test();
    	}
    }
    Hope thats helped
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    hummmm. Ill give it a try. But im not quite sure if it will work.
    The public void pack() method from the java.awt.Window
    class resizes the window to encompass the perfered size of
    all the contained components, as placed by the current layout manager. So it sounds like it actually resizes the Window not the
    actual components. But ill try it. Thanks

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Didnt seem to work. But that's ok. What works is setting the Maximum size not the Perfered Size. Why this is so im not too sure. I dont think that the pack() method has anything to do with the resizing of components, it just resizes the parent container.

    Thanks.

    Code:
         import javax.swing.*; 
         import java.awt.*; 
    
       public class Test{
          public static void main(String[] args){
                 
       JFrame jf = new JFrame("TestFrame"); 
       jf.addWindowListener(new WindowMonitor());
    
       Container content = jf.getContentPane(); 
       content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS)); 
    
       JButton Big = new JButton("Big"); 
       JButton Bigger = new JButton("Bigger"); 
       JButton EvenBigger = new JButton("Even Bigger");  
       JButton ReallyreallyBig = new JButton("Really really Big "); 
       JButton TheBiggestoftheBig =  new JButton(" The Biggest of the Big"); 
      
      Dimension allTheSame = new Dimension(200, 50);
    		
    		
      Big.setPreferredSize(allTheSame);
      Bigger.setPreferredSize(allTheSame);
      EvenBigger.setPreferredSize(allTheSame);
      ReallyreallyBig.setPreferredSize(allTheSame);
      TheBiggestoftheBig.setPreferredSize(allTheSame);
    		
       content.add(Big); 
       content.add(Bigger); 
       content.add(EvenBigger); 
       content.add(ReallyreallyBig); 
       content.add(TheBiggestoftheBig);
    
       jf.pack();  
       jf.setSize(400,300);  
       jf.setVisible(true); 
    
      }
    }

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    yes i think being able to resize an interface is a bad idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width