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 667079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:53:20+00:00 2026-05-13T23:53:20+00:00

How do we show the gridline in GridLayout? in Java? JPanel panel = new

  • 0

How do we show the gridline in GridLayout? in Java?

JPanel panel = new JPanel(new GridLayout(10,10));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

for (int i =0; i<(10*10); i++){
   panel.add(new JLabel("Label"));
}
  • 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-13T23:53:21+00:00Added an answer on May 13, 2026 at 11:53 pm

    I would try to do it by adding borders to the components as they are added. The simple way to do it is just using BorderFactory.createLineBorder(), like this:

    JPanel panel = new JPanel(new GridLayout(10,10));
    panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
    
    for (int i =0; i<(10*10); i++){
        final JLabel label = new JLabel("Label");
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panel.add(label);
    }
    

    However, that will give you thicker borders between the cells than at the edges of the panel, because the outer edges will only have a one-pixel thick border and the inside edges will have two one-pixel thick borders together. To work around that, you can use BorderFactory.createMatteBorder() to only draw one-pixel-wide borders everywhere:

    final int borderWidth = 1;
    final int rows = 10;
    final int cols = 10;
    JPanel panel = new JPanel(new GridLayout(rows, cols));
    panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
    
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            final JLabel label = new JLabel("Label");
            if (row == 0) {
                if (col == 0) {
                    // Top left corner, draw all sides
                    label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                }
                else {
                    // Top edge, draw all sides except left edge
                    label.setBorder(BorderFactory.createMatteBorder(borderWidth, 
                                                                    0, 
                                                                    borderWidth, 
                                                                    borderWidth, 
                                                                    Color.BLACK));
                }
            }
            else {
                if (col == 0) {
                    // Left-hand edge, draw all sides except top
                    label.setBorder(BorderFactory.createMatteBorder(0, 
                                                                    borderWidth, 
                                                                    borderWidth, 
                                                                    borderWidth, 
                                                                    Color.BLACK));
                }
                else {
                    // Neither top edge nor left edge, skip both top and left lines
                    label.setBorder(BorderFactory.createMatteBorder(0, 
                                                                    0, 
                                                                    borderWidth, 
                                                                    borderWidth, 
                                                                    Color.BLACK));
                }
            }
            panel.add(label);
        }
    }
    

    This should give you borders of width borderWidth everywhere, both between cells and along the outside edges.

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

Sidebar

Related Questions

Please show me how i should correctly execute system proccess in java. I would
SHow me where and what to add code for below query $randitems = $DB->getAll(SELECT
to show a background process i used ProgressDialogBox . My code is as add_button.setOnClickListener(new
I show date ranges as follows: public static void main(String[] args) { Locale.setDefault(new Locale(nl,
I show/hide few images basing on the button clicks in update panel. I use
Show me another way to write so it finds the first A: $(UL.tab LI
show grants for charm@'localhost'; ---------------------+ | Grants for charm@localhost | +-----------------------------------------------------------------------------------------------------------------+ | GRANT USAGE
I show UIAlertView with UIAlertViewStylePlainTextInput style. Then I try to validate for empty field
inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into
Please show me how to rewrite toString in a functional way. The code is

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.