Solved

button fills whole frame :(

Posted on 2010-11-09
13
1,118 Views
Last Modified: 2012-05-10
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
Comment
Question by:hipal
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 125 total points
ID: 34099593
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 34099598
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
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 34099909
set the layout manager of the container you are adding it to null

panel.setLayoutManager(null);
panel.add(button);
0
 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 125 total points
ID: 34099910
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
 
LVL 16

Expert Comment

by:Valeri
ID: 34099919
@objects : sorry! :-) exactly in the same time!
btw, adding button is not enought. setBounds must be invoked too to position the component
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 34100399
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:ksivananth
ID: 34100507
null layout mostly lead into problems, should avoid unless and otherwise needed! here null layout should be avoided!
0
 

Author Comment

by:hipal
ID: 34102039
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 34102060
>>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
 

Author Comment

by:hipal
ID: 34102077
Can you please elaborate .. what kind of issues it can lead to ?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34102092
when you resize the window, componenets won't be resized/re-aligned!
0
 

Author Comment

by:hipal
ID: 34102182
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34104112
>>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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Cipher Configuration on Apache HTTPD 4 53
session migration servlets 2 26
eclipse apache tomcat admin console 39 46
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now