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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:20:36+00:00 2026-06-09T12:20:36+00:00

I have developed one frame on which I used GridBagLayout to arrange textfields of

  • 0

I have developed one frame on which I used GridBagLayout to arrange textfields of 12X12. i.e., total 144 textfields on frame. Now I want to differentiate these text fields with colored line after each 3 columns and three rows as shown in the following diagram. I tried in many ways, but I couldn’t find the solution. Please suggest. Below is the some part of my code. Thanks in advance.

1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12 
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
-------------------------------    
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
      |       |       |
1 2 3 | 4 5 6 | 7 8 9 | 10 11 12
--------------------------------    |
1 2 3 | 4 5 6................
. .
. .
.

Please consider the each number as one textfield in diagram.

JTextField jt[][]=new JTextField[12][12];


for(int i=0;i<jt.length;i++)
        {
            for(int j=0;j<jt.length;j++)
            {

                jt[i][j] = new JTextField(1);


                constraints.gridx=j;
                consraints.gridy=i;
                gridbag.setConstraints(jt[i][j],cons);
                c.add(jt[i][j]);
                                jt[i][j].setHorizontalAlignment(JTextField.CENTER);
                jt[i][j].setFont(new Font("TimesNewRoman",Font.BOLD,14));
                jt[i][j].setDocument(new JTextFieldLimit(2));
            }
        }
  • 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-06-09T12:20:38+00:00Added an answer on June 9, 2026 at 12:20 pm

    You could us a JSeparator or, break each group of 3×3 fields into there own separate panes and use a LineBorder.

    So long as you’ve setup you fields properly, you should be able to get the compound panels/LineBorder to work

    UPDATE

    Sorry, it should have been MatteBorder 😛

    Matte Border

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 0;
    gbc.gridy = 0;
    
    for (int row = 0; row < 4; row++) {
    
        gbc.gridx = 0;
    
        add(buildGroup(0, 0, 1, 1), gbc);
        gbc.gridx++;
        add(buildGroup(0, 0, 1, 1), gbc);
        gbc.gridx++;
        add(buildGroup(0, 0, 1, 1), gbc);
        gbc.gridx++;
        add(buildGroup(0, 0, 1, 0), gbc);
        gbc.gridy++;
    
    }
    
    public JPanel buildGroup(int top, int left, int bottom, int right) {
    
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(new MatteBorder(top, left, bottom, right, Color.RED));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
    
                JTextField field = new JTextField(8);
                gbc.gridx = col;
                gbc.gridy = row;
    
                panel.add(field, gbc);
    
            }
        }
    
        return panel;
    
    }
    

    Now, obviously, you need to figure out how you’d seed your fields, but basically, I’d just pass in the fields you want to be used (such as 2D array for example).

    Or with separators 😛

    Separators

    for (int row = 0; row < 9; row++) {
    
        gbc.gridwidth = 1;
        gbc.weightx = 0;
    
        int verSplit = 0;
    
        for (int col = 0; col < 12; col++) {
    
            gbc.gridx++;
    
            add(new JTextField(8), gbc);
    
            verSplit++;
            if (verSplit == 3) {
    
                verSplit = 0;
    
                gbc.gridx++;
    
                if (horSplit % 3 == 0) {
    
                gbc.gridheight = 3;
                gbc.fill = GridBagConstraints.VERTICAL;
                add(new JSeparator(JSeparator.VERTICAL), gbc);
    
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridheight = 1;
    
                }
    
            }
    
        }
    
        horSplit++;
    
        gbc.gridx = 0;
    
        if (horSplit == 3) {
    
            horSplit = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            add(new JSeparator(JSeparator.HORIZONTAL), gbc);
    
        }
    
        gbc.gridy++;
    
    }
    

    Or variations on the same theme

    UPDATED with field management

    // Build the array of fields, I used a col by row matrix
    JTextField fields[][] = new JTextField[12][12];
    for (int col = 0; col < 12; col++) {
    
        for (int row = 0; row < 12; row++) {
    
            fields[col][row] = new JTextField(col + "x" + row, 8);
    
        }
    
    }
    
    // Build the groups...
    for (int row = 0; row < 12; row++) {
    
        gbc.gridx = 0;
    
        int col = 0;
    
        add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);
    
        col += 3;
        gbc.gridx++;
        add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);
    
        col += 3;
        gbc.gridx++;
        add(buildGroup(fields, col, row, 0, 0, 1, 1), gbc);
    
        col += 3;
        gbc.gridx++;
        add(buildGroup(fields, col, row, 0, 0, 1, 0), gbc);
    
        gbc.gridy++;
        row += 2; // This is important, don't miss this ;)
    
    }
    
    public JPanel buildGroup(JTextField[][] fields, int startCol, int startRow, int top, int left, int bottom, int right) {
    
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(new MatteBorder(top, left, bottom, right, Color.RED));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
    
                // Get the field to use for this cell 
                JTextField field = fields[col + startCol][row + startRow];
                gbc.gridx = col;
                gbc.gridy = row;
    
                panel.add(field, gbc);
    
            }
        }
    
        return panel;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed one application in iPhone which contains videos. Now I want to
I have developed one app in which i have used the Google Place API.
One business jar which have been developed by us is present in war. but
in my project i have one registration form which is developed in C#.net.to this
I have developed one table having 3 comlumns and 10 rows, Now to insert
I have developed one application in android and now I wish to add one
I want to Reading Color from Android Application. Here I have developed one Application,
I have developed an application that has a list of items in one frame;
i have developed one application in which getting install app information like name,package name,
I have developed an application with mac and for one month now, Im trying

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.