[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1153
  • Last Modified:

button fills whole frame :(

I am just trying to add a button to a frame... even when I setting the size and location for button, it is still occupying the whole frame.  :(  I mean it appears as the size of frame.

:annoyed
0
hipal
Asked:
hipal
  • 5
  • 3
  • 2
  • +2
4 Solutions
 
ksivananthCommented:
you need to add it to a panel and then add to frame for more flexibility. frame has border layout so if you add it to center( by default ) it will occupy the whole frame unless components added to other sides( NORTF,SOUTH... )
0
 
ksivananthCommented:
here is an example,

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * TYPE DESCRIPTION HERE
 *
 */
public class TestGridbaglayout{
      
      private JPanel buildComponent(){
            JPanel pnlMain = new JPanel( new GridBagLayout() ) ;
            
            //30%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 0, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //70%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //None - only component display area
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //50%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //50%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 1, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //None - only component display area
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 2, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 2, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~35%
            pnlMain.add( new JButton( "Button 3" ), new GridBagConstraints( 2, 2, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 3, 1, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 1, 3, 2, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );

            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 4, 2, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 4, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            
            //~35%
            pnlMain.add( new JButton( "Button 1" ), new GridBagConstraints( 0, 5, 2, 1, 1.0, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            //~30%
            pnlMain.add( new JButton( "Button 2" ), new GridBagConstraints( 2, 5, 1, 1, 0.3, 0.0,
                    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );

            return pnlMain ;
      }
      
      /**
       * @param args
       */
      public static void main( String[] args ){
            TestGridbaglayout test = new TestGridbaglayout() ;
        JFrame frame = new JFrame("TestGridbaglayout");
       
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
 
        frame.getContentPane().add( test.buildComponent(), BorderLayout.CENTER);
        frame.setSize( 400, 400 ) ;
//        frame.pack();
        frame.setVisible(true);
      }
      
}
0
 
objectsCommented:
set the layout manager of the container you are adding it to null

panel.setLayoutManager(null);
panel.add(button);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ValeriCommented:
The default content pane of JFrame will have a BorderLayout manager set on it. That's way your button is expanded. I know you are using absolute positioning, so you have to set layout manager to null in this way : yourFrame.setLayout(null);
Here is an example :
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TFrame extends JFrame {

    public TFrame() throws HeadlessException {
        this.setLayout(null);
    }

    public static void main( String [] args ) {
        JButton button = new JButton("My Button");

        JFrame frame = new TFrame();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.add( button  );
        button.setBounds(40, 40, 100, 20);

        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}
0
 
ValeriCommented:
@objects : sorry! :-) exactly in the same time!
btw, adding button is not enought. setBounds must be invoked too to position the component
0
 
CEHJCommented:
>>I know you are using absolute positioning, so you have to set layout manager to null in this way

If that's the case, i suggest you don't use it. It will only lead to other problems. You can use a null layout when you've mastered layout management in Java. But you won't want to then.

0
 
ksivananthCommented:
null layout mostly lead into problems, should avoid unless and otherwise needed! here null layout should be avoided!
0
 
hipalAuthor Commented:
Hi all thanks a lot for all the responses for my silly mistake.

But, I need do to place different buttons at different places on screen .. so I think I have no other option but to use null layout.
0
 
ksivananthCommented:
>>so I think I have no other option but to use null layout

thats a bad idea as I said earlear, check with the various layout managers and choose the best fit for you!
0
 
hipalAuthor Commented:
Can you please elaborate .. what kind of issues it can lead to ?
0
 
ksivananthCommented:
when you resize the window, componenets won't be resized/re-aligned!
0
 
hipalAuthor Commented:
But my app will never cause resizing the window .. I am even using it as undecorated .. so no option to resize..


But still there are no work arounds ? so that they get resized on resizing window..

and I still confused, I mean placing components are desired places should be such a common thing ..how can we work with pre defined layouts everytime.  

I need button to be on border of panel and also want few here and there .. can you please suggest something.. as I will really like to use layouts .. because it is better practice like you all experts suggested
0
 
CEHJCommented:
>>and I still confused, I mean placing components are desired places should be such a common thing ..how can we work with pre defined layouts everytime.  

Layout managers lay out components in a predictable way and allow resizing
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now