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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:47:30+00:00 2026-05-26T03:47:30+00:00

I have a javax.swing.JPanel called calcResPanel (using a java.awt.GridLayout with 1 column and indefinite

  • 0

I have a javax.swing.JPanel called calcResPanel (using a java.awt.GridLayout with 1 column and indefinite (0) rows) which is to receive and display a set of BHSelectableLabels (which extend javax.swing.JTextField) with collectively represent the text stored in the list of Strings called results. I figured that I might as well give it the following behavior:

  1. The first time, it will only add new ones
  2. Following times, it will:
    1. Change the text of as many labels that are already added as possible to be that of as many of the values in results as possible
    2. If there are any labels left that haven’t been changed, remove those, as they are not necessary. Else, add as many new labels as needed.

This makes sense to me. If this algorithm is not what I should be doing, then stop reading now and post an answer with a better algorithm. However, if you agree, then tell me what I’ve done wrong with my code:

  int i, r, l;
  for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
    if (i < l)
      ((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
    else
      calcResPanel.add(new BHSelectableLabel(results.get(i)));
  for (;i < l; i++)//If there are excess, unused lables, remove them
    calcResPanel.remove(i);

The problem with this code is that it inconsistently leaves excess labels in calcResPane. If you think this algorithm is good in concept, then please tell me what is wrong with my code that makes it leave excess labels?

Answer


Such a simple answer, too. I feel SO smart ^^;

  int i, r, l;
  for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
    if (i < l)
      ((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
    else
      calcResPanel.add(new BHSelectableLabel(results.get(i)));
  for (;i < l; i++)//If there are excess, unused lables, remove them
    calcResPanel.remove(r);
  • 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-26T03:47:31+00:00Added an answer on May 26, 2026 at 3:47 am
    for (;i < l; i++)//If there are excess, unused lables, remove them     
        calcResPanel.remove(i); 
    

    You can never do a remove like that because you skip every 2nd item. Lets say you have 5 items and you try to delete them all:

    The first time through the loop i = 0, so you remove item 0 and you are left with 1, 2, 3, 4.

    Next time throught the loop i = 1, so you remove item 2 and you are left with 1, 3, 4.

    I hope you get the pattern.

    The solution is to remove items from the end, one at a time.

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

Sidebar

Related Questions

I have a simple problem with my JPanel / Gridbaglayout: import javax.swing.*; import java.awt.*;
Made a custom ListCellRenderer: import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel;
import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Frame; import java.awt.Graphics; import java.awt.Color; import java.util.Random; public class
I have the following code: package example; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JFrame; import
I have a Java Swing application, developed on Mac OS X 10.5 using Java
I have the following: import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.BorderFactory;
I have created a class called SimpleCanvas which extends the JPanel class, and I
// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
I have a class that extends javax.swing.JPanel , it contains a single JButton. I've
I'm trying to create a SWING application using Java 1.6 and I have a

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.