Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: How to set button to be pressed more than once

  1. #1
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default How to set button to be pressed more than once

    i have a button , that will do an action. it will do it once, but it wont do it again when i press it again. how do i get the button to repeat the action again, until it has fullfilled the requirements.

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    
    public class CountingConstant extends JFrame  {
    
              JFrame MFrame;
             
              
              
        
        
        
           CountingConstant()
           {
               
               final JLabel[] labels = new JLabel[10];
               MFrame = new JFrame("Pirate Constant Counting");
               MFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
               MFrame.setSize(500,500);
               MFrame.pack();
               MFrame.setVisible(true);
               
              
               MFrame.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++)
               {
               MFrame.add(labels[i]);
               }
               
               JButton button = new JButton("Begin");
               
               button.addActionListener(new ActionListener()
                       {
                @Override
                        public void actionPerformed(ActionEvent e) 
                        {
    				
    	           int ii = 7;
                       int changableLength = labels.length;
           
              for(int i = 0; i < changableLength; i++)
              {
            
           int iii = (ii + (110-1) ) % changableLength;
        
       
              labels[iii].setText("");
             
             }
    
                       }});
               MFrame.add(button);
           }
        
        
        public static void main(String[] args) {
            
            
            new CountingConstant();
            
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set button to be pressed more than once

    Quote Originally Posted by ajs1351 View Post
    i have a button , that will do an action. it will do it once, but it wont do it again when i press it again. how do i get the button to repeat the action again, until it has fullfilled the requirements.
    But your button does do its action again when the button is pressed -- it sets the same JLabel's text to "". It just so happens that after the button has been pressed once, this label already has no text display, but the button's action listener doesn't care. Perhaps you wish to explain what behavior you're trying to elicit that the button's listener isn't performing? Perhaps you wish to remove a label from the gui and from the array? perhaps you wish to change the value of the variable that currently never changes named "changableLength"? Who knows?

    Also sorry to be blunt, but this code has awful indentation and formatting:

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    
    public class CountingConstant extends JFrame  {
    
              JFrame MFrame;
             
              
              
        
        
        
           CountingConstant()
           {
               
               final JLabel[] labels = new JLabel[10];
               MFrame = new JFrame("Pirate Constant Counting");
               MFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
               MFrame.setSize(500,500);
               MFrame.pack();
               MFrame.setVisible(true);
               
              
               MFrame.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++)
               {
               MFrame.add(labels[i]);
               }
               
               JButton button = new JButton("Begin");
               
               button.addActionListener(new ActionListener()
                       {
                @Override
                        public void actionPerformed(ActionEvent e) 
                        {
    				
    	           int ii = 7;
                       int changableLength = labels.length;
           
              for(int i = 0; i < changableLength; i++)
              {
            
           int iii = (ii + (110-1) ) % changableLength;
        
       
              labels[iii].setText("");
             
             }
    
                       }});
               MFrame.add(button);
           }
        
        
        public static void main(String[] args) {
            
            
            new CountingConstant();
            
        }
    }
    So awful it is difficult to read and even harder to interpret your code if you're an outsider who didn't write it. I ask that if you want us to put in the effort to try to understand your code and try to help you, please put in the effort yourself to make it easy for us to do so by making your code us uniform and standard code style with uniform indentations. For instance:

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class CountingConstant extends JFrame {
    
       JFrame MFrame;
    
       CountingConstant() {
    
          final JLabel[] labels = new JLabel[10];
          MFrame = new JFrame("Pirate Constant Counting");
          MFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
          MFrame.setSize(500, 500);
          MFrame.pack();
          MFrame.setVisible(true);
    
          MFrame.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++) {
             MFrame.add(labels[i]);
          }
    
          JButton button = new JButton("Begin");
    
          button.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                int ii = 7;
                int changableLength = labels.length;
                for (int i = 0; i < changableLength; i++) {
                   int iii = (ii + (110 - 1)) % changableLength;
                   labels[iii].setText("");
                }
             }
          });
          MFrame.add(button);
       }
    
       public static void main(String[] args) {
          new CountingConstant();
       }
    }

  3. #3
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    yea im trying to remove label form the array. but if i use the remove method. it gives me an "Array index out of range".

    [code]
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class CountingConstant extends JFrame {

    JFrame MFrame;

    CountingConstant()
    {

    final JLabel[] labels = new JLabel[10];
    MFrame = new JFrame("Pirate Constant Counting");
    MFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    MFrame.setSize(500, 500);
    MFrame.pack();
    MFrame.setVisible(true);

    MFrame.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++)
    {
    MFrame.add(labels[i]);
    }

    JButton button = new JButton("Begin");

    button.addActionListener(new ActionListener()
    (
    {
    @Override
    public void actionPerformed(ActionEvent e)
    {
    int ii = 7;
    int changableLength = labels.length;
    for (int i = 0; i < changableLength; i++)
    {
    int iii = (ii + (110 - 1)) % changableLength;
    labels[iii]. remove(iii);
    }
    }
    }
    );
    MFrame.add(button);



    }

    public static void main(String[] args)
    {
    new CountingConstant();
    }
    }

  4. #4
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    at first i was trying to remove the label form the array. but i keep getting an error " Array index out of Range" . i use the remove method but it is not working. so how do i go about this?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set button to be pressed more than once

    I would use an ArrayList of JLabel or ArrayList<JLabel>, not an array, and then when the label has been selected, remove it from both the ArrayList and the container that holds it, and then call revalidate() and repaint() on the container that holds it.

  6. #6
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    if i have this arraylist m how do i add it to the container. b/c everytime i try to add it its not compatible.

    Java Code:
             ArrayList<JLabel> label = new ArrayList<JLabel>();
            
            String[] nums = {"0","1","2","3","5","4","7","6","9","8"};  
    
          
           for(int i = 0 ; i < nums.length; i++)
            {
                 label.add(new JLabel(nums[i]));
                 p.add(label[]);     // CONTAINER   . what needs to be added?    p.add(label.get(i))  ??
                 
            }
    Last edited by ajs1351; 12-01-2011 at 08:22 PM.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: How to set button to be pressed more than once

    What is the error you get when you try? You said "b/c everytime i try to add it its not compatible." That doesn't tell us what compile error you are getting. Posting a compile error will help get a faster response and honestly a compile error tells you exactly whats wrong. If you don't know how to fix it after you find out whats wrong, then asking for help is the best decision.

    tl;dr: Post the compiler error please.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set button to be pressed more than once

    Right now you're creating your JLabel on the same line that you add it to its container, so there's no way to get a reference to it and add it to the list. Instead you should create a JLabel within the for loop on its own line

    JLabel myLabel = new JLabel(foo[i]);

    and then add that same object to the container and to the ArrayList<JLabel>.

  9. #9
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    Is this what you mean ? creating the array is no problem. but when i add it to the jframe. it only displays the last array index.


    Java Code:
     ArrayList<JLabel> label = new ArrayList<JLabel>();
      
     String[] nums = {"0","1","2","3","5","4","7","6","9","8"}; 
     
     
    for(int i = 0 ; i < nums.length; i++)
     {
          label.add(new JLabel(nums[i]));  // adds the 10 contents.
                
     }
    
    
    for(int i = 0 ; i < label.size() ; i++)
     {
       p.add(label.get(i));      //   it only shows the last index of the array
     }

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to set button to be pressed more than once

    p.add(label.get(i)); // it only shows the last index of the array
    Check that the layout manager for p is appropriate for displaying multiple labels.

    [edit] You talk about adding the label "to the jframe", but you really need to be careful about the container that you are using - very likely a JPanel instance . Check out How to use Panels and Using Toplevel COntainers.
    Last edited by pbrockway2; 12-02-2011 at 12:38 AM.

  11. #11
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    k, so i redid the code using a jpanel . but i still cant it to remove the Jlable that correspond to the array element that was removed.

    when i press the button, it did remove 5 elements in the array. because i printed out the array size.

    but it is not removing the jlabel.

    So i still needed to remove the Jlabel or if i cant remove it replace with a text of some sort.

    that where im at now.

    Java Code:
    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()
           {
               
                
               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);     //removes the element of the array.
                                
                                       }
                                          Display.setText(Integer.toString(ArrayLab.size()));    //size of the array.
                                 }        
                       }
                    );
                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();
            
        }
    }

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to set button to be pressed more than once

    With the expression "ArrayLab.remove(Count);" you are removing a label at a given index position within the list of labels. You also have to remove that label from the panel (see Container API docs), or set its text (see JLabel API docs).

    [edit] Also it would be a very good thing if you rewrote the code to conform with Java coding practices: methods and variables start with a lowercase letter.

  13. #13
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    i used a container.remove for the labels. but it is not removing the right labels. its not matching the what the array has removed.

    Java Code:
    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);
                                            jp.remove (arraylab.get(count));   //container removing label
                                   
                                           jp.repaint();
                                
                                       }
                                          display.setText(Integer.toString(arraylab.size()));
                                 }        
                       }
                    );

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to set button to be pressed more than once

    Java Code:
    arraylab.remove(count);
    jp.remove (arraylab.get(count));   //container removing label
    That looks like the right idea, but think about what those lines do. The first removes a label (at index count) from the array, the second tries to get the label from the array (in order to remove it from the panel). But you can't expect to get() something that you have removed! The order of these lines is wrong.

    (In fact the remove() method actually returns the thing that was removed which is handy in cases like this where you want to do something further with that thing.)

  15. #15
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    So if the order of those lines are wrong. first remove the container label, then remove the array?


    wont it be the same thing?

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to set button to be pressed more than once

    wont it be the same thing?
    There's only one way to find out: try it.

    In case there's any confusion when you remove the labels from the list you do by index value. This has nothing to do with the text displayed on that label.
    Last edited by pbrockway2; 12-03-2011 at 08:41 AM.

  17. #17
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    yea i tried it , and it removes the labels. but still removing the wrong indexes of the array.

    but i know the formula i have there works in removing all odd numbers in the array with a starting index of 7 . constant count = 110.

    i can get this to work on a console application with no issues. but i cant get it to work on a gui interface.

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to set button to be pressed more than once

    We have something of a moving target here: "it is not removing the label" -> "it is not removing the right labels" -> "removes the labels, but still removing the wrong indexes of the array".

    It might be a good idea to alter your code so that it prints out the value of indx and, each time around the loop prints out the value of the count and the label being removed from the container. Focus your description of your problem on the behaviour (output) you observe and the behaviour you expect or intend. This is a continuation of the point Dark made before when you were getting compiler messages: we don't see your computer and need things described precisely - compiler messages, output, etc.

    Bear in mind that you have not said what the code should do. Or what the code currently is. Perhaps there is another way - besides keeping an array and a container in synch - of doing what you are trying to do.
    Fubarable likes this.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set button to be pressed more than once

    I wonder if you're trying to demonstrate the Josephus Problem or a variant thereof?

  20. #20
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: How to set button to be pressed more than once

    yes it similar to Josephus Problem. trying to get rid of the odds number elements in the array. with a initial index starting point. and constant count .

    but my arraylist is not in sequence 0-9 . rather 0,1,2,3,5,4,7,6,9,8

    in this case i have initial starting Index : 7 Count : 110

    the formula i use which works in the console version is
    Java Code:
      indx = (indx + (count-1) % arraylab.size();
    this will eleminate all the odd elements in the array.

    this is my output that i get from my console Version. which im trying to get similar with my array of Jlabels. which is not doing it.
    Java Code:
     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]
    Last edited by ajs1351; 12-04-2011 at 06:46 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Pausing and starting a loop when a button is pressed.
    By xan.amini in forum New To Java
    Replies: 16
    Last Post: 08-11-2011, 09:15 AM
  2. Kep Pressed
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 09:07 PM
  3. Checking if a button was pressed in a Window.
    By Valkyrie in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 06:28 AM
  4. get key pressed
    By prashant in forum Networking
    Replies: 1
    Last Post: 03-26-2009, 10:10 PM
  5. Waiting for a button to be pressed
    By SomeGuyOverThere in forum New To Java
    Replies: 6
    Last Post: 08-21-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •