clearing a JLabel not working

82 Views Asked by At

I have read several posts suggesting to clear a JLabel (displayEntered) on a panel (display) with text by using the setText(" "). However, I have tried this and the outcome is it is just posting the array entered twice and does not clear the first set. I have an action shown below when a button is pressed both times; the first is to enter the data entered (I have the same code 4 times for the 4 different possible objects to enter but just put in the one since it's basically the same), which works fine and the second is to remove a specific one shown. My code is long, so am just putting that in. If someone wants something else please let me know. Thanks, I'd appreciate any input!

 //adds the Herb data to the Array and list
 enterHerbData.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Enter")){
            Name = NameTxt.getText();
            Colors = ColorTxt.getText();
            ID = (int) IDCmbo.getSelectedItem();
            Flavor = FlavorTxt.getText();
            if(((String) MedicinalCmbo.getSelectedItem()).equals("Yes"))
                Medicinal = true; 
            else
                Medicinal = false;
            if(((String) SeasonalCmbo.getSelectedItem()).equals("Yes"))
                Seasonal = true; 
            else
                Seasonal = false;

            plants[count] = new Herb(Name, ID, Colors, Flavor, Medicinal, Seasonal);
            String displayArraytemp = " ";

                if(plants[count] != null){
                    if(plants[count] instanceof Flower){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Flower)plants[count]).getColor() + ", " + ((Flower)plants[count]).getSmell() + ", Thorny: " + ((Flower)plants[count]).getThorns() + "\n");
                        }
                        else if(plants[count] instanceof Fungus){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Fungus)plants[count]).getColor() + ", Poisonous: " + ((Fungus)plants[count]).getPoisonous() + "\n");
                        }
                        else if(plants[count] instanceof Weed){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Weed)plants[count]).getColor() + ", Edible: " + ((Weed)plants[count]).getEdible() + ", Medicinal: " + ((Weed)plants[count]).getMedicinal() + ", Poisonous: " + ((Weed)plants[count]).getPoisonous() + "\n");
                        }
                        else if(plants[count] instanceof Herb){
                        displayArraytemp = ((count + 1) + ": " + plants[count].getID() + ", " + plants[count].getName() + ", " + ((Herb)plants[count]).getColor() + ", " + ((Herb)plants[count]).getFlavor() + ", Medicinal: " + ((Herb)plants[count]).getMedicinal() + ", Poisonous: " + ((Herb)plants[count]).getSeasonal() + "\n");
                        }
                    sb.append("<html>" + displayArraytemp).
                        append("<br> ");
                    displayArray = sb.toString();
                }

            displayEntered.setText(displayArray);
            count++;
            frameB.setVisible(false);
        }
    }
});

 //removes the data to the Array and panel
 ActionListener RemoveAction = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent RemoveAction){
        if(RemoveAction.getActionCommand().equals("Enter")){
        if((Btn1).isSelected()){
            String displayArraytemp2 = " ";
            if(count >= 1){
                for(int n = 0; n < count; n++){
                    plants[n] = plants[n+1];
                }
                count--;
                frameB.setVisible(false);
                displayEntered.setOpaque(true);
                for(int n = 0; n < 25; n++){
                    if(plants[n] != null){
                        if(plants[n] instanceof Flower){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Flower)plants[n]).getColor() + ", " + ((Flower)plants[n]).getSmell() + ", Thorny: " + ((Flower)plants[n]).getThorns() + "\n");
                            }
                            else if(plants[n] instanceof Fungus){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Fungus)plants[n]).getColor() + ", Poisonous: " + ((Fungus)plants[n]).getPoisonous() + "\n");
                            }
                            else if(plants[n] instanceof Weed){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Weed)plants[n]).getColor() + ", Edible: " + ((Weed)plants[n]).getEdible() + ", Medicinal: " + ((Weed)plants[n]).getMedicinal() + ", Poisonous: " + ((Weed)plants[n]).getPoisonous() + "\n");
                            }
                            else if(plants[n] instanceof Herb){
                            displayArraytemp2 = ((n + 1) + ": " + plants[n].getID() + ", " + plants[n].getName() + ", " + ((Herb)plants[n]).getColor() + ", " + ((Herb)plants[n]).getFlavor() + ", Medicinal: " + ((Herb)plants[n]).getMedicinal() + ", Poisonous: " + ((Herb)plants[n]).getSeasonal() + "\n");
                            }
                        sb.append("<html>" + displayArraytemp2).
                            append("<br> ");
                        displayArray = sb.toString();
                    }
                }
            }

            displayEntered.setText(" ");
            displayEntered.setText(displayArray);
        }
        }
    }};
1

There are 1 best solutions below

0
On

Your real problem is that you are re-using sb without clearing it.