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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:06:52+00:00 2026-06-05T21:06:52+00:00

First, I am new to programming and this is my first major assignment in

  • 0

First, I am new to programming and this is my first major assignment in java and programming in general so if I am doing some incredibly stupid please tell me so I can correct the bad habit.

Anyway to the problem, I am currently trying to create a gridLayout that has a variable number of rows which will be filled with a label that has text that comes from a file. My problem is specifically on gridLayout were the labels that I do add and are constants seem to be disappearing into one giant cell. So far none of the reasearch I have done has lead to anything so I thought I may as well pose the question.

public void fillTimetablePane(JPanel pane){
    int noOfRows = pref.getNoOFPeriods()+1; 
    pane.setLayout(new GridLayout(noOfRows,4));
    pane.setBorder(BorderFactory.createLineBorder(Color.black));
    JLabel label = new JLabel();
    int i=0;
    while (i<4){

        switch (i) {
        case 0: label.setText("Lesson");
                break;
        case 1: label.setText("Period");
                break;
        case 2: label.setText("Room");
                break;
        case 3: label.setText("Teacher");
                break;
        }
        i++;
        pane.add(label);
    }
}

here is an image of what happens when I add run the following code:
http://www.freeimagehosting.net/1hqn2

  • 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-05T21:06:55+00:00Added an answer on June 5, 2026 at 9:06 pm
    public void fillTimetablePane(JPanel pane){
        int noOfRows = pref.getNoOFPeriods()+1; 
        pane.setLayout(new GridLayout(noOfRows,4));
        pane.setBorder(BorderFactory.createLineBorder(Color.black));
        //JLabel label = new JLabel();   // from here
        int i=0;                         //   V
        while (i<4){                     //   V
            JLabel label = new JLabel(); // to here
            switch (i) {
            case 0: label.setText("Lesson");
                    break;
            case 1: label.setText("Period");
                    break;
            case 2: label.setText("Room");
                    break;
            case 3: label.setText("Teacher");
                    break;
            }
            i++;
            pane.add(label);
        }
    }
    

    Ok, why is it not working in your case but works fine in my case? The problem is that you add your label 4 times and change the text inbetween. In a Layout, a single component can only be existing once. So what happens is that when you add your label a second/third/fourth time, its location in the grid will be updated and not added again.

    In my case, I actually create a new JLabel in every iteration of the loop and therefore adding a different label to the JPanel.

    Hope this is clear enough. Just ask if something is not clear.

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

Sidebar

Related Questions

First, i'm new at Java-programming and my native lang is not english, but still
I am new to object oriented programming and writing some of my first classes
First of all, I'm new to objective-c programming so I've probably made some mistakes
First of all, i'm a Graphic designer so please ignore if this programming question
I am new to programming, so please accept my apologies if this question is
I am very new to programming, so please forgive me if this question seems
I'm new programming in shell and I need some help with this code... Buildname=
I'm new to Java, but not new to programming, so as my first project
Firstly, this is a homework assignment, and I am very new to programming in
I´m new to programming, and are doing som exercices. In this exercice I am

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.