Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 702405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:43:02+00:00 2026-05-14T03:43:02+00:00

I put my buttons in a JPane with GridLayout. Then I put JPanel into

  • 0

I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?

ADDED:

I still cannot solve the problem. Here is the code of what I am trying to do:

import javax.swing.*;
import java.awt.*;

public class PanelModel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel firstPanel = new JPanel();
        firstPanel.setLayout(new GridLayout(4, 4));
        JButton btn;
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(100, 100));
                firstPanel.add(btn);
            }
        }

        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(5, 13));
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=13; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(40, 40));
                secondPanel.add(btn);
            }
        }

        mainPanel.add(firstPanel);
        mainPanel.add(secondPanel);
        frame.add(mainPanel);

        frame.setSize(400,600);
        frame.setVisible(true);
    }
}

The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:43:02+00:00Added an answer on May 14, 2026 at 3:43 am

    The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible

    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(4,4,4,4));
    
    for(int i=0 ; i<16 ; i++){
        JButton btn = new JButton(String.valueOf(i));
        btn.setPreferredSize(new Dimension(40, 40));
        panel.add(btn);
    }
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
    

    The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.

    Edit

    I’ve modified the provided code and I believe it now conforms to what is desired:

    JFrame frame = new JFrame("Colored Trails");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    JPanel firstPanel = new JPanel();
    firstPanel.setLayout(new GridLayout(4, 4));
    firstPanel.setMaximumSize(new Dimension(400, 400));
    JButton btn;
    for (int i=1; i<=4; i++) {
        for (int j=1; j<=4; j++) {
            btn = new JButton();
            btn.setPreferredSize(new Dimension(100, 100));
            firstPanel.add(btn);
        }
    }
    
    JPanel secondPanel = new JPanel();
    secondPanel.setLayout(new GridLayout(5, 13));
    secondPanel.setMaximumSize(new Dimension(520, 200));
    for (int i=1; i<=5; i++) {
        for (int j=1; j<=13; j++) {
            btn = new JButton();
            btn.setPreferredSize(new Dimension(40, 40));
            secondPanel.add(btn);
        }
    }
    
    mainPanel.add(firstPanel);
    mainPanel.add(secondPanel);
    frame.setContentPane(mainPanel);
    
    frame.setSize(520,600);
    frame.setMinimumSize(new Dimension(520,600));
    frame.setVisible(true);
    

    Basically I now set the preferred size of the panels and a minimum size for the frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subclass s of UIView. I want to put some buttons and
Put it another way: what code have you written that cannot fail. I'm interested
Put differently: Is there a good reason to choose a loosely-typed collection over a
We put all of our unit tests in their own projects. We find that
I put better in quotes because it's a qualitative question. I've been writing COM
Simply put, is there a way to create a 2D javascript array using similar
We put common prefixes on related tables to assure they display next to each
To put it simple, there's a simple java swing app that consists of JFrame
Simply put, MVC is the pattern for separating contents (model) from presentation (view), and
I put together a sample scenario of my issue and I hope its enough

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.