Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Passing an array to another class in a JFrame

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Array extends JFrame {
    
       private JTextField number = new JTextField(10);
       private JLabel result;
       int num;
       ArrayList<Integer> list = new ArrayList<>();
       ArrayList coll;
    
       public static void main(String[] args) {
    
          // create a frame
          Array frame = new Array();
          frame.setTitle("Collections");
          frame.setSize(700, 300);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
    
       }
    
       public Array() {
    
          // add labels and text fields to the frame
          add(new JLabel("You can add 50 numbers."), BorderLayout.NORTH);
          setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
          add(new JLabel("Enter a number and click the enter button:"));
          add(number);
    
          // add Enter button
          JButton jbtEnter = new JButton("Enter");
          jbtEnter.setToolTipText("This will store your number in the program");
          add(jbtEnter);
    
          // create actionListener for the Enter button
          jbtEnter.addActionListener(new EnterListener());
    
          // create Display Button
          JButton jbtDisplay = new JButton("Display");
          jbtDisplay
                .setToolTipText("This will display all the integers in numberical order");
          add(jbtDisplay);
    
          // create actionListener for Display button
          jbtDisplay.addActionListener(new DisplayListener());
    
          result = (new JLabel(""));
          add(result);
       }
    
       // this is what happens when the Enter button is clicked
       class EnterListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
    
             // loop so user cannot enter more than 50 numbers
             for (int i = 0; i < 50; i++) {
                if (i < 50) {
                   num = Integer.parseInt(number.getText());
                   list.add(num);
                   i++;
                } else
                   throw new IllegalArgumentException(
                         "You have entered all 50 numbers.");
             }
             number.setText(null);
          }
       }
    
       // this is what happens when the Display button is selected
       class DisplayListener implements ActionListener {
    
          @Override
          public void actionPerformed(ActionEvent e) {
    
             // trying to make the variable coll equal to the array list in the
             // class above
             coll = Collections.sort(coll);
             result.setText("The numbers you have entered, listed in numerical order is:"
                   + coll);
          }
    
       }
    
    }

    In the DisplayListener, how do I bring the array from EnterListener to the variable coll in DisplayListener.
    Last edited by Fubarable; 05-05-2012 at 09:25 PM. Reason: code cleaned up

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

    Default Re: Passing an array to another class in a JFrame

    Your code is *very* hard to read. Please edit the post above and add [code] [/code] tags above and below your code blocks so that they retain their formatting. Also, feel free to flesh out your question, some, to add any detail that might help us understand your problem better. Also let us know what compiler errors you might be seeing (because surely you are seeing some based on the code abve), and the lines that are causing the errors.

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

    Default Re: Passing an array to another class in a JFrame

    And are you absolutely sure that you want to use that for loop in your EnterListener? 100% completely sure??

  4. #4
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    I would like to so the user can click enter to submit the entry. What would be your suggestion with EnterListener and without EnterListener?

  5. #5
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    The program looks like it would work if I can get the array to the variable coll in the DisplayListener.

  6. #6
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Sorry, I misunderstood your question. If the loop can be changed and have the same out come, it does not matter if the for loop is there or not.

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

    Default Re: Passing an array to another class in a JFrame

    Again, please don't ignore my request that you edit your post so that we can read your code. If we can't read your code, we can't understand you code. If we can't understand your code, we can't help you. It's up to you.

    Regarding your EnterListener, your for loop goes against the rules of getting data in an event-driven way. You can't use that for loop, but rather need to wait for the user to click the button each time he wants to enter another number. In other words, the listener code should be written to accept one new entry and that's it. He must click it 30 times to enter 30 numbers.

    Regarding your other question, I'll await your edit to your original post.

  8. #8
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Just figured out what you were talking about. I am new to this forum.
    Last edited by George5432; 05-05-2012 at 09:16 PM.

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

    Default Re: Passing an array to another class in a JFrame

    I have re-edited your original post. You only need one set of code tags per program or large block of code.

    Your code as posted won't compile, and you'll need to fix the compilation errors first. Any questions about these?

  10. #10
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Thanks for helping with the edit. I was having a hard time with that. I am trying to figure out line 82. That piece of code is incorrect. It should be coll = But I do not know what goes after the equals. I want coll to equal the array from the class above (EnterListener). Then coll would be the same array. Then I can enter the code Collections.sort(coll).

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

    Default Re: Passing an array to another class in a JFrame

    Quote Originally Posted by George5432 View Post
    Thanks for helping with the edit. I was having a hard time with that. I am trying to figure out line 82. That piece of code is incorrect. It should be coll = But I do not know what goes after the equals. I want coll to equal the array from the class above (EnterListener). Then coll would be the same array. Then I can enter the code Collections.sort(coll).
    Collections.sort(...) is not working as you expect it does. For one it modifies the collection passed into it, and for another it doesn't return anything (per the API, it returns void). So you simply pass the collection to be sorted into this method and call it and don't set it on the right side of any assignment statement.

    But your other problem is that coll is empty. Why not use the other ArrayList, the one you've already filled up?

  12. #12
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    That is what I am trying to do. How do I bring that ArrayList down to the class DisplayListener. Bringing down what a user entered can be done by, an example name.getText(). I do not know how to do this with an ArrayList.
    Last edited by George5432; 05-05-2012 at 09:40 PM.

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

    Default Re: Passing an array to another class in a JFrame

    Quote Originally Posted by George5432 View Post
    That is what I am trying to do. How do I bring that ArrayList down to the class DisplayListener
    What happens when you try to use it there?

  14. #14
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Quote Originally Posted by Fubarable View Post
    What happens when you try to use it there?
    This is what I am trying to figure out how to use. coll = list.getText(); does not work

  15. #15
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    how would I get the already filled array to the coll?

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

    Default Re: Passing an array to another class in a JFrame

    Quote Originally Posted by George5432 View Post
    This is what I am trying to figure out how to use. coll = list.getText(); does not work
    No, what I'm suggesting is what happens if you don't even use a "coll" variable, but instead use list directly?

    Also, don't call methods that don't exist. Instead you'll want to be very familiar with the Java API and use it frequently to check to see what methods are available for the class you are using. ArrayLists for instance have no getText() method that I know of, and you can't make one up and try to use it.

  17. #17
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Array extends JFrame{
        
        private JTextField number = new JTextField(10);
        private JLabel result;
        int num;
        ArrayList<Integer> list = new ArrayList<>();
        
        
        public static void main(String[] args){
            
            Array frame = new Array();
            frame.setTitle("Collections");
            frame.setSize(700, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            
            
        }
        
        public Array() {
        
           
            add(new JLabel("Enter 50 numbers."), BorderLayout.NORTH);
            setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
            add(new JLabel("Enter a number and click the enter button:"));
            add(number);
            
            JButton jbtEnter = new JButton("Enter");
            jbtEnter.setToolTipText("This will store your number in the program");
            add(jbtEnter);
            
            jbtEnter.addActionListener(new EnterListener());
            
            
            JButton jbtDisplay = new JButton("Display");
            jbtDisplay.setToolTipText("This will display all the integers in numberical order");
            add(jbtDisplay);
            
            jbtDisplay.addActionListener(new DisplayListener());
            
            result = (new JLabel(""));
            add (result);
    }
        class EnterListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e){
                
                for (int i = 0; i < 50; i++){
                if (i < 50){
                    i++;
                num = Integer.parseInt(number.getText());
                list.add(num);
                 
                } else
                    throw new IllegalArgumentException("You have entered all 50 numbers.");
            }
                number.setText(null);
            }
        }
        
        class DisplayListener implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent e) {
                
                Collections.sort(list);
                result.setText("The numbers you have entered, listed in numerical order is:" + list );
            }
    
            
            }
            
        }
    I did use your advice and just used the list. I did not see that before. thank you for that. This code runs but but the program will allow the numbers to exceed 50 entries.

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

    Default Re: Passing an array to another class in a JFrame

    Yes, and you're also still using that for loop and thus adding each number entered 50 (!!) times.

  19. #19
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Can I have some advice on how to correct that? I am working on it as well. Just letting you know I am not just asking you to do this for me.

  20. #20
    George5432 is offline Member
    Join Date
    May 2012
    Posts
    20
    Rep Power
    0

    Default Re: Passing an array to another class in a JFrame

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Array extends JFrame{
         
        private JTextField number = new JTextField(10);
        private JLabel result;
        int num;
        ArrayList<Integer> list = new ArrayList<>();
         
         
        public static void main(String[] args){
             
            Array frame = new Array();
            frame.setTitle("Collections");
            frame.setSize(700, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
             
             
        }
         
        public Array() {
         
            
            add(new JLabel("Enter 50 numbers."), BorderLayout.NORTH);
            setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
            add(new JLabel("Enter a number and click the enter button:"));
            add(number);
             
            JButton jbtEnter = new JButton("Enter");
            jbtEnter.setToolTipText("This will store your number in the program");
            add(jbtEnter);
             
            jbtEnter.addActionListener(new EnterListener());
             
             
            JButton jbtDisplay = new JButton("Display");
            jbtDisplay.setToolTipText("This will display all the integers in numberical order");
            add(jbtDisplay);
             
            jbtDisplay.addActionListener(new DisplayListener());
             
            result = (new JLabel(""));
            add (result);
    }
        class EnterListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e){
                 
                int i = 0;
                if (i < 50){
                    i++;
                num = Integer.parseInt(number.getText());
                list.add(num);
                  number.setText(null);
                } else
                    throw new IllegalArgumentException("You have entered all 50 numbers.");
    
            }
        }
         
        class DisplayListener implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent e) {
                 
                Collections.sort(list);
                result.setText("The numbers you have entered, listed in numerical order is:" + list );
            }
     
             
            }
             
        }

Page 1 of 2 12 LastLast

Similar Threads

  1. Passing 2 dementional array to a new class
    By anonymous in forum New To Java
    Replies: 4
    Last Post: 03-15-2011, 11:07 PM
  2. Passing array to model class
    By keshaba in forum IntelliJ IDEA
    Replies: 0
    Last Post: 04-20-2010, 01:43 PM
  3. Replies: 6
    Last Post: 04-12-2010, 11:33 AM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

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