Results 1 to 5 of 5
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default How to make JButtons as big as possible and rezise when resizing JFrame?

    Hi, I have a problem I can't solve, I have 3 JButtons on a JPanel. I want them all to have exactly the same size (filling up the entire JPanel), but at the moment they just small and dont resize when i change the size of the JFrame. I've tried to set them to maxSize but I can't get it to work.

    Heres my configuration of the buttons and panels:
    PHP Code:
    buttonLayout.fill = GridBagConstraints.BOTH;
    		progressLayout.fill = GridBagConstraints.BOTH;
    		
    		buttonLayout.weightx = buttonLayout.weighty = 1;
    		progressLayout.weightx = progressLayout.weighty = 1;
    		
    		progressLayout.gridwidth = 3;
    		buttonLayout.gridy = 1;
    
    
    		progressPanel.add(fileName);
    		progressPanel.add(progress);
    		btnPanel.add(play);
    		btnPanel.add(load);
    		btnPanel.add(stop);
    		
                   //JLabel
    		progress.setFont(new Font("sansserif", Font.CENTER_BASELINE, 32));
    		//JButtons
                    load.setFont(new Font("sansserif", Font.CENTER_BASELINE, 12));
    		play.setFont(new Font("sansserif", Font.CENTER_BASELINE, 12));
    		stop.setFont(new Font("sansserif", Font.CENTER_BASELINE, 12));
    		
    		
    		
    		this.add(btnPanel, buttonLayout);
    		this.add(progressPanel, progressLayout);
    Thanks for the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not simply use a GridLayout rather than a GridBagLayout. If this doesn't help, consider posting a very small program that is runnable and compilable and shows your problem, an SSCCE

  3. #3
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why not simply use a GridLayout rather than a GridBagLayout. SSCCE
    I need GridBagLayout because i want a Label displaying some text at the top of half of the window and the buttons on the bottom half of the window. I'm really new to GridBagLayout but there must be some way to make the buttons as big as possible as in a normal GridLayout?

    Pic displaying the problem of buttons not filling up: http://img19.imageshack.us/img19/369/img0005zz.jpg

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    An image is NOT a SSCCE.

    You can use multiple panels. Maybe a main panel with a BorderLayout. Add the label to the NORTH. Then create a second panel with a GridLayout. Add the buttons to this panel, then add the panel to the SOUTH of your main panel.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by equal View Post
    I need GridBagLayout because i want a Label displaying some text at the top of half of the window and the buttons on the bottom half of the window. I'm really new to GridBagLayout but there must be some way to make the buttons as big as possible as in a normal GridLayout?

    Pic displaying the problem of buttons not filling up: http://img19.imageshack.us/img19/369/img0005zz.jpg
    No, you don't need GridBagLayout for that. You could use a GridLayout for the overall GUI and a separate GridLayout for the buttons. But again, show us your SSCCE if you're still stuck. For e.g.,

    Java Code:
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Foo5b extends JPanel {
       private static final String TEXT = "Text";
       private static final float TEXT_POINTS = 40;
       private static final float BTN_POINTS = 28f;
       private static final int EB_GAP = 10;
       private String[] btnTexts = {"Play", "Load", "Stop"};
       
       public Foo5b() {
          JLabel textLabel = new JLabel(TEXT, SwingConstants.CENTER);
          textLabel.setFont(textLabel.getFont().deriveFont(TEXT_POINTS));
          textLabel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
          
          JPanel btnPanel = new JPanel(new GridLayout(1, 0));
          for (String btnText : btnTexts) {
             JButton btn = new JButton(btnText);
             btn.setFont(btn.getFont().deriveFont(Font.BOLD, BTN_POINTS));
             btnPanel.add(btn);
          }
          
          setLayout(new GridLayout(0, 1));
          add(textLabel);
          add(btnPanel);
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Foo5b");
          frame.getContentPane().add(new Foo5b());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Again, if this code doesn't help you, then create your own SSCCE and post it here.

Similar Threads

  1. JFrame resizing question
    By mellowcandle in forum AWT / Swing
    Replies: 2
    Last Post: 04-13-2011, 01:48 PM
  2. Help with JButtons into JFrame
    By g6pd in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 04:49 AM
  3. JFrame resizing problem
    By Bluefox815 in forum AWT / Swing
    Replies: 7
    Last Post: 02-24-2011, 07:56 PM
  4. Using frame.pack() for resizing JFrame
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 10-30-2010, 12:12 PM
  5. JFrame resizing
    By carderne in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 07:42 PM

Posting Permissions

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