[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

button fills whole frame :(

Posted on 2010-11-09
13
Medium Priority
?
1,147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 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 500 total points
ID: 34099909
set the layout manager of the container you are adding it to null

panel.setLayoutManager(null);
panel.add(button);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Assisted Solution

by:Valeri
Valeri earned 500 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 500 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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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