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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:10:40+00:00 2026-06-14T21:10:40+00:00

I have 5 labels in grid layout frame. I would like to change gridlayout

  • 0

I have 5 labels in grid layout frame. I would like to change gridlayout to border layout with using names of location from an array (e.g. String gborders[] = { “North”, “Center”, “South”, “East”, “West” }; ) and enumerate label location. Is it possible?

That is what I have:

package w1;

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

public class LayShow {

  private static JLabel lLabel;

  public static void main(String[] args) {


    int t = 15;

    String lmNames[] = {"Label 1", "Label 2", 
            "Label 3", "Label 4", "Label 5"};


    String gborders[] = { "North", "Center", "South", "East", "West" };

    Color colors[] = { new Color(11, 125, 155), new Color(155, 55, 200),
        new Color(201, 245, 145), new Color(255, 255, 140),
        new Color(161, 224, 224), new Color(11, 125, 155) };


    Font fonts[] = {new Font("SansSerif", Font.BOLD, t), 
            new Font("Arial", Font.BOLD, t+1), 
            new Font("SansSerif", Font.PLAIN, t+2), 
            new Font("SansSerif", Font.ITALIC, t+3),
            new Font("SansSerif", Font.ITALIC, t+4)};


    JFrame frame = new JFrame("Frame"); 
    frame.setLayout(new GridLayout(0, 2));


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


      final Border
      borderColor = BorderFactory.createLineBorder(colors[i+1]);    
      JPanel p = new JPanel();
      p.setBackground(colors[i]);  
      p.setBorder(BorderFactory.createTitledBorder(borderColor , lmNames[i])); 

      lLabel = new JLabel("label number: " + (i+1));
      p.add(lLabel);
      lLabel.setFont(fonts[i]);
      lLabel.setForeground(colors[i+1]);
      lLabel.setToolTipText("ToolTip for label number: " + (i+1));

      frame.add(p);
    }
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}
  • 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-14T21:10:41+00:00Added an answer on June 14, 2026 at 9:10 pm

    You can do the following

    String[] gborders =  { BorderLayout.NORTH,BorderLayout.CENTER, 
                          BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST };
    

    Add all inner panels in a single panel

    JFrame frame = new JFrame("Frame"); 
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());
    

    at the end of for loop

    for (int i = 0; i < lmNames.length; i++) {
      ....
      lLabel.setToolTipText("ToolTip for label number: " + (i+1));
      panel1.add(p,gborders[i]);
    }
    frame.add(panel1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    

    Done.

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

Sidebar

Related Questions

I want to have a 3x3 grid layout structured like this: _ _ _
I have a simple grid layout which is just hosting several labels and textboxes.
I would like to display data from a database onto a content page using
I have a border layout and I want to add a grid layout to
I have 10 labels on a panel - and 10 identical ClickEvents (change BackColor).
I have 10 labels named lbl1 , lbl2 , ... lbl10 I'd like to
I have created a grid that contains 10x10 buttons using 2d arrays. i tried
I have a panel in which i have specified with a grid bag layout.
I have the following SWT layout: TabItem SashForm Composite (GridLayout) Label (Fill W, Grab
I have created a java program that shows a grid of buttons and labels

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.