set button to be full width of jpanel

im trying to make by clear button the full width of the keypad.

by clear button is placed at the bottom in separate jpanel but i cant get it to display correctly...

private void createKeyPad() {
        setLayout( new GridLayout( 5, 4, 0, 0 ) );
       
        String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
       
        for ( int i = 0; i <= 15; i++ ) {
            JButton button = new JButton( buttonLabels[ i ] );
            button.addActionListener( pro );
            add( button );
        }
       
        JPanel clearPanel = new JPanel();
        clearPanel.setLayout(null);
        JButton clearButton = new JButton( "Clear" );
        clearButton.setBounds(0, 0, 400, 50);
        clearButton.addActionListener( pro );
        clearPanel.add( clearButton );
        add( clearPanel );
    }

what type of layout will work for what im trying to do?
LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

riaancorneliusCommented:
clearPanel.setLayout(new BorderLayout() );
clearPanel.add( clearButton, BorderLayout.CENTER );
TimYatesCommented:
       JPanel clearPanel = new JPanel();
        clearPanel.setLayout( new BorderLayout() );
        JButton clearButton = new JButton( "Clear" );
        clearButton.addActionListener( pro );
        clearPanel.add( clearButton, BorderLayout.CENTER );
        add( clearPanel );
TimYatesCommented:
Sorry riaancornelius...  didn't refresh :-/
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

riaancorneliusCommented:
also,
this.setLayout( new BorderLayout() );
JPanel buttonPanel = new JPanel(new GridLayout( 5, 4, 0, 0 ));
this.add( buttonPanel, BorderLayout.CENTER );
clearPanel.setPreferredSize( new Dimension( 400, 50 ) );
this.add( clearPanel, BorderLayout.SOUTH );

This should do it :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ellandrdAuthor Commented:
didnt work - my layout is a grid for my keypad, so now my button is a square in the grid...  i want ito be separate from the grid...
riaancorneliusCommented:
>> Sorry riaancornelius...  didn't refresh :-/
No prob, I do it quite often too :)
riaancorneliusCommented:
>> didnt work - my layout is a grid for my keypad, so now my button is a square in the grid...  i want ito be separate from the grid...
See my second post.
riaancorneliusCommented:
       this.setLayout( new BorderLayout() );
        JPanel buttonPanel = new JPanel(new GridLayout( 5, 4, 0, 0 ));
        String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
       
        for ( int i = 0; i <= 15; i++ ) {
            JButton button = new JButton( buttonLabels[ i ] );
            button.addActionListener( pro );
            buttonPanel.add( button );
        }
        this.add( buttonPanel, BorderLayout.CENTER );

        JPanel clearPanel = new JPanel( new BorderLayout() );
        clearPanel.setLayout(null);
        JButton clearButton = new JButton( "Clear" );
        clearPanel.setPreferredSize( new Dimension( 400, 50 ) );
        clearButton.addActionListener( pro );
        clearPanel.add( clearButton );
        clearPanel.add( clearButton, BorderLayout.CENTER );

        this.add( clearPanel, BorderLayout.SOUTH );
TimYatesCommented:
Right..yeah, you need to do:

private void createKeyPad() {
        this.setLayout( new BorderLayout() ) ;
        jPanel buttonPanel = new JPanel( new GridLayout( 4, 4, 0, 0 ) );
       
        String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
       
        for ( int i = 0; i <= 15; i++ ) {
            JButton button = new JButton( buttonLabels[ i ] );
            button.addActionListener( pro );
            buttonPanel.add( button );
        }
       
        JButton clearButton = new JButton( "Clear" );
        clearButton.addActionListener( pro );
        add( buttonPanel, BorderLayout.CENTER );
        add( clearButton, BorderLayout.SOUTH );
    }
TimYatesCommented:
hahaha...

We appear to be almost sharing a mind this morning riaancornelius ;-D
riaancorneliusCommented:
That's off the top of my head. might need to fix typo's and debug mistakes, But that's the basic idea.

Set layout to borderLayout
    -> load new JPanel with gridlayout into center          (With buttons)
    -> load new JPanel with borderLayout into bottom   (With clearButton)
    -> set preferredSize for bottomPanel                       (Width is always full width of parent, but height is used)
riaancorneliusCommented:
>> We appear to be almost sharing a mind this morning riaancornelius ;-D
Seems like it. At least ellandrd knows it must be correct :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.