Link to home
Start Free TrialLog in
Avatar of Ajs135
Ajs135

asked on

[Java] Removing JLabel from an Array...

Hello, I am trying to get this program to remove a JLabel from the array until requirements met. but i keep getting this error.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 8
      at java.awt.Container.remove(Container.java:1136)
      at countingconstant.CountingConstant$1.actionPerformed(CountingConstant.java:81)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
      at java.awt.Component.processMouseEvent(Component.java:6289)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
      at java.awt.Component.processEvent(Component.java:6054)
      at java.awt.Container.processEvent(Container.java:2041)
      at java.awt.Component.dispatchEventImpl(Component.java:4652)
      at java.awt.Container.dispatchEventImpl(Container.java:2099)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
      at java.awt.Container.dispatchEventImpl(Container.java:2085)
      at java.awt.Window.dispatchEventImpl(Window.java:2478)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
      at java.awt.EventQueue.access$000(EventQueue.java:85)
      at java.awt.EventQueue$1.run(EventQueue.java:603)
      at java.awt.EventQueue$1.run(EventQueue.java:601)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
      at java.awt.EventQueue$2.run(EventQueue.java:617)
      at java.awt.EventQueue$2.run(EventQueue.java:615)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



What Im trying to do,  I have this array with this integers. when the user put in the Index Starting Point, and The Count. it will go thru the array removing those numbers.


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CountingConstant    {

         private JFrame MFrame;
         private JPanel Jp;
         private JTextField IT = new JTextField(25);
         private JTextField CT = new JTextField(25);
         private JButton button = new JButton("Begin");
         private JLabel[]  labels;
    
    
    
        public CountingConstant()
       {
           
            labels = new JLabel[10];
           MFrame = new JFrame("Pirate Constant Counting");
           MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           MFrame.setSize(500,500);
           MFrame.setVisible(true);
           Jp = new JPanel();
           Jp.setBackground(Color.yellow);
           
           
           
           Jp.setLayout(new GridLayout(10,1));
           
           labels[0] = new JLabel("0");
           labels[1] = new JLabel("1");
           labels[2] = new JLabel("2");
           labels[3] = new JLabel("3");
           labels[4] = new JLabel("5");
           labels[5] = new JLabel("4");
           labels[6] = new JLabel("7");
           labels[7] = new JLabel("6");
           labels[8] = new JLabel("9");
           labels[9] = new JLabel("8");
           
           for(int i = 0 ; i < labels.length; i++)
           {
           Jp.add(labels[i]);
           }
           
            Jp.add(IT);
            Jp.add(CT);

           
         
           button.addActionListener(new ActionListener()
                   {
                         @Override
                         public void actionPerformed(ActionEvent e) 
                           {
				
	                      int Indx = Integer.parseInt(IT.getText());
                              int Count = Integer.parseInt(CT.getText());
                              int changableLength = labels.length;
                   
                                  //Removes All ODD Numbers.
                                 for(int i = 0; i < changableLength; i++)
                                   {
                                     Count = ( Indx + (Count-1) ) % changableLength;
        
                                          labels[i].remove(Count);
                                         Jp.remove(labels[Count]);
                 
  
                                   }

                             }        
                   }
                );
          
           //Add Button To Panel
           Jp.add(button);
           
           
           //ADD Panel TO JFRAME
           MFrame.add(Jp);
       }
    
    
    public static void main(String[] args) {
        
        
        new CountingConstant();
        
    }
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

is ir full stack trace - which line of your code causes the problem?
what this means:
labels[i].remove(Count);

Open in new window



you are removint int frm Label - strange ?

perhaps Count is negative.
Print it befire this line
remove this line:
labels[i].remove(Count);

Open in new window


it does not remove any lable - it removes maybe some child from instance of  JLabel called
labels[i] 

Open in new window


as this instnce has no children it may also cause error

This does not cause error any longer

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CountingConstant    {

         private JFrame MFrame;
         private JPanel Jp;
         private JTextField IT = new JTextField(25);
         private JTextField CT = new JTextField(25);
         private JButton button = new JButton("Begin");
         private JLabel[]  labels;



        public CountingConstant()
       {

            labels = new JLabel[10];
           MFrame = new JFrame("Pirate Constant Counting");
           MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           MFrame.setSize(500,500);
           MFrame.setVisible(true);
           Jp = new JPanel();
           Jp.setBackground(Color.yellow);



           Jp.setLayout(new GridLayout(10,1));

           labels[0] = new JLabel("0");
           labels[1] = new JLabel("1");
           labels[2] = new JLabel("2");
           labels[3] = new JLabel("3");
           labels[4] = new JLabel("5");
           labels[5] = new JLabel("4");
           labels[6] = new JLabel("7");
           labels[7] = new JLabel("6");
           labels[8] = new JLabel("9");
           labels[9] = new JLabel("8");

           for(int i = 0 ; i < labels.length; i++)
           {
           Jp.add(labels[i]);
           }

            Jp.add(IT);
            Jp.add(CT);



           button.addActionListener(new ActionListener()
                   {
                       
                         public void actionPerformed(ActionEvent e)
                           {

	                      int Indx = Integer.parseInt(IT.getText());
                              int Count = Integer.parseInt(CT.getText());
                              int changableLength = labels.length;

                                  //Removes All ODD Numbers.
                                 for(int i = 0; i < changableLength; i++)
                                   {
                                     Count = ( Indx + (Count-1) ) % changableLength;

                //                          labels[i].remove(Count);
                                         Jp.remove(labels[Count]);
                                     //  Jp.repaint();


                                   }

                             }
                   }
                );

           //Add Button To Panel
           Jp.add(button);


           //ADD Panel TO JFRAME
           MFrame.add(Jp);
       }


    public static void main(String[] args) {


        new CountingConstant();

    }
}

Open in new window

Avatar of Ajs135
Ajs135

ASKER

I printed Count and it comes out to  0         if i use    index 7 , Count 110.

 labels[i].remove[Count]

Open in new window



It remove some labels, but it giving a wrong solution.   b/c   using those  index 7 and count 110.  it should remove all the odd numbers only and even should remain.

but im a getting a result of     1,3,4,6,8       .  


but i know using those numbers  , should take out the odds number.  when i do this  on a console application. it works just fine.
Avatar of Ajs135

ASKER

i did remove the
  labels[i].remove[Count]

Open in new window


>i did remove the

Good!
This line was not removing any labels - it was just weird thing to do, so in some cases (or maybe in all cases) it caused this error;
when I removed this line I no longer saw such error from your code.
And you should not see this error either.


This line
  Jp.remove(labels[Count]);

Open in new window


makes much more sense,
howvere waht you intended to do with that and waht you think you should see
on your frame, after you do it vs what you actually see - that's quite another
story, quite different than the issue with ArrayOutOfBounds exception

 
Do not know what was your goal, but this at least is removing some of them.
Of coure the numbers supplied are indexes of the array - not necessarily it removes
the same number as prinyed on the label.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CountingConstant    {

         private JFrame MFrame;
         private JPanel Jp;
         private JTextField IT = new JTextField(25);
         private JTextField CT = new JTextField(25);
         private JButton button = new JButton("Begin");
         private JLabel[]  labels;
      private JPanel[]  panels;



        public CountingConstant()
       {

            labels = new JLabel[10];
            panels = new JPanel[10];

           Jp = new JPanel();
           Jp.setBackground(Color.yellow);



           Jp.setLayout(new GridLayout(10,1));

           labels[0] = new JLabel("0");
           labels[1] = new JLabel("1");
           labels[2] = new JLabel("2");
           labels[3] = new JLabel("3");
           labels[4] = new JLabel("5");
           labels[5] = new JLabel("4");
           labels[6] = new JLabel("7");
           labels[7] = new JLabel("6");
           labels[8] = new JLabel("9");
           labels[9] = new JLabel("8");

           for(int i = 0 ; i < labels.length; i++)
           {
               panels[i] = new JPanel();
               panels[i].add(labels[i]);
           Jp.add(panels[i]);
           }

            Jp.add(IT);
            Jp.add(CT);
            Jp.add(button);





           button.addActionListener(new ActionListener()
                   {
                       
                         public void actionPerformed(ActionEvent e)
                           {

	                      int Indx = Integer.parseInt(IT.getText());
                              int Count = Integer.parseInt(CT.getText());
                              int changableLength = labels.length;
                               panels[Indx].remove(labels[Indx]);
                               panels[Count].remove(labels[Count]);
                               Jp.repaint();

                               /*
                                  //Removes All ODD Numbers.
                                 for(int i = 0; i < changableLength; i++)
                                   {
                                     Count = ( Indx + (Count-1) ) % changableLength;

                //                          labels[i].remove(Count);
                                         panels[Count].remove(labels[Count]);
                                  //   panels[Count].repaint();
                                     //  Jp.repaint();


                                   }
                               */


                             }
                   }
                );


             MFrame = new JFrame("Pirate Constant Counting");
           Container c = MFrame.getContentPane();
           c.add(Jp);
           MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           MFrame.setSize(500,500);
           MFrame.setVisible(true);

           //Add Button To Panel
         //  Jp.add(button);


           //ADD Panel TO JFRAME
          // MFrame.add(Jp);
       }


    public static void main(String[] args) {


        new CountingConstant();

    }
}

Open in new window

Avatar of Ajs135

ASKER

well i change the array into an ArrayList.  i use the remove method . and it remove the 5 integers from the array.  i know this b/c i print out the array size .

the only problem is now, is even tho it remove those integers, it still displaying those integers.  The issue is removing the Jlabel corresponding to that integer that has been removed is what i still cant figure out.

Look at the  code I posted above  ID:37228881 - I corrected some mistakes - and if you type in both boxes numbers - it will remove
some of the labels - actually the labels with the given iindexes in the array.
If you want to remove say odd's, or even's - it should not be difficult to figure out how to change.
Avatar of Ajs135

ASKER

Yea that does not work i get the error what i originally started with.

like i said redid it with an arrayList. i just need to know now how to remove the Jlabel or replace it with asomething else that correspond to the Integer Index thas has been removed.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CountingConstant    {

         private JFrame MFrame;
         private JPanel Jp;
         private JTextField IT = new JTextField(25);
         private JTextField CT = new JTextField(25);
         private JButton button = new JButton("Begin");
         private JLabel[]  labels;
         private JLabel  Display = new JLabel();
         private ArrayList<JLabel> ArrayLab;
    
    
        public CountingConstant()
       {
           
            labels = new JLabel[10];
           MFrame = new JFrame("Pirate Constant Counting");
           MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           MFrame.setSize(500,500);
           MFrame.setVisible(true);
           Jp = new JPanel();
           Jp.setBackground(Color.yellow);
           
           ArrayLab = new ArrayList<JLabel>();
           ArrayLab.add(0, new JLabel("0"));
           ArrayLab.add(1, new JLabel("1"));
           ArrayLab.add(2, new JLabel("2"));
           ArrayLab.add(3, new JLabel("3"));
           ArrayLab.add(4, new JLabel("5"));
           ArrayLab.add(5, new JLabel("4"));
           ArrayLab.add(6, new JLabel("7"));
           ArrayLab.add(7, new JLabel("6"));
           ArrayLab.add(8, new JLabel("9"));
           ArrayLab.add(9, new JLabel("8"));
        
           
           
           
           Jp.setLayout(new GridLayout(10,1));
           

           
           for(int i = 0 ; i < ArrayLab.size(); i++)
           {
           Jp.add(ArrayLab.get(i));
           }
           
            Jp.add(IT);
            Jp.add(CT);

           
         
           button.addActionListener(new ActionListener()
                   {
                         @Override
                         public void actionPerformed(ActionEvent e) 
                           {
				
	                      int Indx = Integer.parseInt(IT.getText());
                              int Count = Integer.parseInt(CT.getText());

                                  //Removes All ODD Numbers.
                                 for(int i = 0; i < ArrayLab.size(); i++)
                                   {
                                     Count = ( Indx + (Count-1) ) % ArrayLab.size();
        
                                
                                             
                                        ArrayLab.remove(Count);
                            
                                   }
                                      Display.setText(Integer.toString(ArrayLab.size()));
                             }        
                   }
                );
            Jp.add(Display);
           //Add Button To Panel
           Jp.add(button);
           
           
           //ADD Panel TO JFRAME
           MFrame.add(Jp);
       }
    
    
    public static void main(String[] args) {
        
        
        new CountingConstant();
        
    }
}

Open in new window

Avatar of Ajs135

ASKER

Also the Count is used to iterate through the list , once it gets to the Count position , it removes that element of the array. ("It would be an odd numer"). and so on until end.
>Yea that does not work i get the error what i originally started with

but you chnaged it - use my code - it works
and your original werror   was becsuse yiou were
renoving int from label  - you already know that


Avatar of Ajs135

ASKER

this is what im getting. with ur code


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 110
      at countingconstant.CountingConstant$1.actionPerformed(CountingConstant.java:78)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
      at java.awt.Component.processMouseEvent(Component.java:6289)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
      at java.awt.Component.processEvent(Component.java:6054)
      at java.awt.Container.processEvent(Container.java:2041)
      at java.awt.Component.dispatchEventImpl(Component.java:4652)
      at java.awt.Container.dispatchEventImpl(Container.java:2099)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
      at java.awt.Container.dispatchEventImpl(Container.java:2085)
      at java.awt.Window.dispatchEventImpl(Window.java:2478)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
      at java.awt.EventQueue.access$000(EventQueue.java:85)
      at java.awt.EventQueue$1.run(EventQueue.java:603)
      at java.awt.EventQueue$1.run(EventQueue.java:601)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
      at java.awt.EventQueue$2.run(EventQueue.java:617)
      at java.awt.EventQueue$2.run(EventQueue.java:615)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
where do you get this strange IDE that does not print you the most crucial line where error happens?

I just tried my code again (exactly as a pasted it) - if you put numbers into both windows and press begin it does not
make any error - try aagain
Don;'t leave any of the btwo boxes blank - we can address this later - at least we need to
be on the same page

Explain exactly waht you ar doing taht you are getting error  - Ill repeat it.
Avatar of Ajs135

ASKER

im using netbeans.      i put in the index number in the first textfield, and then put the count on the other textfield. then i press the begin button.
Avatar of Ajs135

ASKER

is it the number im putting in?     i put index 7,   count 110
I don't know what you mean by these nubers, and what is count - I thought those are
indexes of lables - put there small nuimbers - say 2 and 3 - you'll see that two of the numbers
will disappear - make sure it works this way.

with 110 it obviously goes array out of bounds in one of these:
     panels[Indx].remove(labels[Indx]);
                               panels[Count].remove(labels[Count]);

the main difficulty was to make these lables go away - and we'll esily produce correct number,
when you explain waht is the count.

For now make sure it works as i used it

Actually I was wrong - your stack trace now is good - I didn't notice - it has this line
  at countingconstant.CountingConstant$1.actionPerformed(CountingConstant.java:78)

  thsi is waht I get when I put 110:


why I'm getting at line 72 and you at line 78 - you
modified the code ?

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 110
      at CountingConstant$1.actionPerformed(CountingConstant.java:72)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
      at java.awt.Component.processMouseEvent(Component.java:6263)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
      at java.awt.Component.processEvent(Component.java:6028)
      at java.awt.Container.processEvent(Container.java:2041)
      at java.awt.Component.dispatchEventImpl(Component.java:4630)
      at java.awt.Container.dispatchEventImpl(Container.java:2099)
      at java.awt.Component.dispatchEvent(Component.java:4460)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
      at java.awt.Container.dispatchEventImpl(Container.java:2085)
      at java.awt.Window.dispatchEventImpl(Window.java:2478)
      at java.awt.Component.dispatchEvent(Component.java:4460)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Avatar of Ajs135

ASKER

Yes it does work when i put in 1 and 2 and it removes it.



but  i  will explain this better. what im trying to accomplish


i have  10 integers in an array.       0- 9   in different order.     0,1,2,3,5,4,7,6,9,8

2 textfield   -      Starting Index position in the array
                           Count -  iterate through the array looping over and over , (0-9,0-9, etc)

            Example  :
       Index  starting position is : 7
     Count:   110

  so i start  at   index  7 of the array,  then go around the array 110 times. and stop, delete that index,  ( in this case  7 is first index gets deleted. then new starting index is at 4, go around the 110 times, stops at 9, deletes.  and so on, until all odd numbers get deleted.)


   
>go around the array 110 times
what thsi  means?

then to determine the index to delete you'll do something liek that:

(7 +110)%10 = 7

the

(4 + 110)%9 = 6 (why do you have 4 - you now have only 9 left).

But anyway - not that it deeletes labels go ahaeda and implement these
numbers play as you want it  - your numbers should refere in the end
to original index in the original array

 
Avatar of Ajs135

ASKER

(4 + 110)%9 = 6 (why do you have 4 - you now have only 9 left).  

0 1 2 3 5 4 <-------   ( once 7 gets  removed , Index shifts one back which is  4)   6 9 8  

Given my solution, is there a way to delete the labels as well?
Avatar of Ajs135

ASKER

or if i cant delete them, can i just hide those labels  with an empty string ?  
Given my solution, is there a way to delete the labels as well?

what is the question ? - once you determine the index -
just do this Jp.remove(...) - what is the problem ?

 
but you can delete labels - you see yu are removing them - don't understand what is the problem
Just make sure that you detemrin the index correctly within the range, and then remove label - not Jp.remove() - I was wring with that
but with panels[indx].remove(labels[indx]) - after that you see that label disppaers and you no longer see the corresponding number.
Avatar of Ajs135

ASKER

K i try  using  panels[indx].remove(labels[indx])   i keep getting

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7

but what i need to remove is what count lands on.  

but i replace [indx] with [Count]   but it just does not do anything. the arraylist  did remove those 5 . but the jlabels remain the same still.
button.addActionListener(new ActionListener()
                   {
                         @Override
                     public void actionPerformed(ActionEvente) 
                           {
				
	              int Indx = Integer.parseInt(IT.getText());
                     int Count = Integer.parseInt(CT.getText());

                                  //Removes All ODD Numbers.
               for(int i = 0; i < ArrayLab.size(); i++)
                   {
                  Count = ( Indx + (Count-1) ) % ArrayLab.size();
                                    
                 ArrayLab.remove(Count);
                                       
                panels[Count].remove(ArrayLab.get(Count));
                               
                  panels[Count].repaint();
                            
                   }
                                       
           Display.setText(Integer.toString(ArrayLab.size()));
                       }        
                   }
                );

Open in new window

Look I don't want to go back - we alread have the code which removes and works - we ned just to calulate uindex which you want to remove
IIt is true ArrayList is in general  better than Array, but once we already have it working with arrays no sense to go back wit removal part

So I suggest to return to the code whwere deleteing was working.
to claculate correct numbers is always more simple.


OK, once again - explain to me the process, so  if I enetered

Indx 7 and Count 110 then I should remove array element  (7 +110 ) % 10
that I understand.

Waht happenes next - should i stop and wait for the next input of the user or not ?

Avatar of Ajs135

ASKER

what happens next is .     once u remove the array elements.  the one previous element is the new starting index. same process again. at count 110 . remove element. new index starting position is previous element to count.


all this is done in a for loop.  

so you have initial index which is 7. then after, index will always be one previous to count. ( 7 +(110-1) % array length.


i have it as a console application. it will show what  it does.
import java.util.ArrayList;

/**
 *
 * @author Alexander
 */
public class Constant {

    
    public static void main(String[] args) {
        
        ArrayList<Integer> num = new ArrayList<Integer>();
    num.add(0, 0);
    num.add(1, 1);
    num.add(2,2);
    num.add(3,3);
    num.add(4,5);
    num.add(5,4);
    num.add(6,7);
    num.add(7,6);
    num.add(8,9);
    num.add(9,8);
  
     
       int ii = 7; 
     
      ii = (ii + (110-1) ) % num.size();
     num.remove(ii);
     
    ii = (ii + (110 - 1) ) % num.size();
    num.remove(ii);
      
     ii = (ii + (110- 1) ) % num.size();
     num.remove(ii);
     
      ii = (ii + (110-1) ) % num.size();
     num.remove(ii);
    
     ii = (ii + (110-1) ) % num.size();
     num.remove(ii);
    
   //Print remaining elements in array.       
    System.out.println(num);
    
    }
}

Open in new window

OK, try this.
I'm still not sure what you want.

I straightend out the order of indexes in thebeginnig array
- don't know whenether you had it on purpose 5 before 4 etc.
and now when you pu 7, 110 - the left side goes away.
If you put odd count, say 7, 109 than right side will disappear

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CountingConstant    {

         private JFrame MFrame;
         private JPanel Jp;
         private JTextField IT = new JTextField(25);
         private JTextField CT = new JTextField(25);
         private JButton button = new JButton("Begin");
         private JLabel[]  labels;
      private JPanel[]  panels;



        public CountingConstant()
       {

            labels = new JLabel[10];
            panels = new JPanel[10];

           Jp = new JPanel();
           Jp.setBackground(Color.yellow);



           Jp.setLayout(new GridLayout(10,1));

           labels[0] = new JLabel("0");
           labels[1] = new JLabel("1");
           labels[2] = new JLabel("2");
           labels[3] = new JLabel("3");
           labels[4] = new JLabel("4");
           labels[5] = new JLabel("5");
           labels[6] = new JLabel("6");
           labels[7] = new JLabel("7");
           labels[8] = new JLabel("8");
           labels[9] = new JLabel("9");

           for(int i = 0 ; i < labels.length; i++)
           {
               panels[i] = new JPanel();
               panels[i].add(labels[i]);
           Jp.add(panels[i]);
           }

            Jp.add(IT);
            Jp.add(CT);
            Jp.add(button);





           button.addActionListener(new ActionListener()
                   {

                         public void actionPerformed(ActionEvent e)
                           {

	                      int Indx = Integer.parseInt(IT.getText());
                              int Count = Integer.parseInt(CT.getText());
                              int changableLength = labels.length;
                             


                               for(int i = 0; i < changableLength; i++)
                                   {
                                     Count = ( Indx + (Count-1) ) % changableLength;
                                              panels[Count].remove(labels[Count]);
                                   }



                               Jp.repaint();



                             }
                   }
                );


             MFrame = new JFrame("Pirate Constant Counting");
           Container c = MFrame.getContentPane();
           c.add(Jp);
           MFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           MFrame.setSize(500,500);
           MFrame.setVisible(true);

           //Add Button To Panel
         //  Jp.add(button);


           //ADD Panel TO JFRAME
          // MFrame.add(Jp);
       }


    public static void main(String[] args) {


        new CountingConstant();

    }
}

Open in new window

Avatar of Ajs135

ASKER

yea i did put the order index like that on purpose.  

 0 1 2 3 5 4 7 5 9 8 .   <-------- this becomes ----------> 0 2 4 6 8

the formula   Count = ( indx + (Count-1))  % Array.size          will get rid of the odd numbers.  
Well than change them back - does it do what you want then ?
Avatar of Ajs135

ASKER

so if we have  

0 1 2 3 5 4 7 6 9  8

if we start   0 1 2 3  5 4 7 <----- Starting Index

the count is 110   .      so if start 7 ,  go through array 110 times.   eventually u land on  indx 7 (gets removed.)

starting new index is  4 , go through array 110 times again.  land. on  indx 8 , removes.

this keeps going until all odd elements indx has been removed.


that is all i want the program to do.  
Avatar of Ajs135

ASKER

i mean it is removing the labels. but it is still removing the wrong indexes.  

i know this formula works. like i said , it works if you do it in a console application
OK, lets' count

 Count = ( Indx + (Count-1) ) % changableLength

on the fisrt iteration

   Count = (7 + 109) % 10 = 6
  we remove number with index 6, which has label 7

    now Count = 6, Indx = 7

       Count = (7 + 5 ) % 10 = 2

  so it removes lable with index 2, which has label  2

    then Count = 2, Indx = 7

    Count = (7 + 1) % 10 = 8

so it removes lable with index 8 , which has label 9

      Count = 8, Indx = 7

     Count = (7 + 7) % 10 = 4

so it removes lable with index 4 , which has label 5

    Count = 4 Indx = 7

   Count = (7 + 3) % 10 = 0 which has label 0

so it removes the whole left side - labels 7,2, 9, 5, 0

That is exactly what it is doing - what else coudl it do ?






Avatar of Ajs135

ASKER

Count is always Constant = 110.


and % 10 <---- this needs to be changining as the arrayList gets smaller.          
Avatar of Ajs135

ASKER

k i have to apologize . i wrote the formula wrong

its suppose to be    indx = (Indx + (Count-1) % Array.Size

to give an idea of the output. i  printed out the results from my console version .

Starting Index: 7 ArraySize: 10

      ELement: 7 has been removed

Starting Index: 6 ArraySize: 9

      ELement: 9 has been removed

Starting Index: 7 ArraySize: 8

      ELement: 5 has been removed

Starting Index: 4 ArraySize: 7

      ELement: 1 has been removed

Starting Index: 1 ArraySize: 6

      ELement: 3 has been removed

Starting Index: 2 ArraySize: 5

Remaining Elements in the Array: [0, 2, 4, 6, 8]



Now im trying to replicate this  with this gui i have .
Avatar of Ajs135

ASKER

k by fixing the formula is now working.  but i have one thing that i still needed it to do , and that is,.

is there a way to slow down the for loop ( in other words , pause for certain amount of seconds, after each loop?  )  if this is possible at all?

i try doing it without the forloop , but then i start given an incorrect solution.
what is Array.Size


Please refer always to the code in
ID:37230161
so that we speak abou the same things

but Array.Size does not make sense to me with respect to any variant of code
and this formula has no matching parentheses
 indx = (Indx + (Count-1) % Array.Size
and also indx and Indx - two different things ?

so I still can't understand this formula

Avatar of Ajs135

ASKER

ArrayLab.size()
 
 Indx = ( Indx + (Count-1) ) % ArrayLab.size();

Open in new window

Avatar of Ajs135

ASKER

the formula determines the next starting index position . in the array.

Please, refer to code:
ID:37230161

it does not have any ArrayLab
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ajs135

ASKER

i use the formula in your code, and it is not removing the right one. that why i had to use the one i have.

everything is working now. The only thing i have left is , Can I slow , or pause, or delay the for loop to see the removing of each element?    

or i end the post now, and create a new one if you want?
Sorry, no matter what I did, I could not make them disappear one by one.
It all goes through and then changes everything.
have no idea why it is doing so.
Seems, I have done similar things many times.
But probably I was always doing it my normal way.
Open another question, post your code, maybe someone could help you.


Avatar of Ajs135

ASKER

alright thanks for the help.