Results 1 to 20 of 28
- 05-05-2012, 08:37 PM #1
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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
-
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.
-
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??
- 05-05-2012, 08:49 PM #4
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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?
- 05-05-2012, 08:51 PM #5
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
- 05-05-2012, 08:56 PM #6
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
-
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.
- 05-05-2012, 09:03 PM #8
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
-
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?
- 05-05-2012, 09:32 PM #10
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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).
-
Re: Passing an array to another class in a JFrame
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?
- 05-05-2012, 09:35 PM #12
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
-
Re: Passing an array to another class in a JFrame
- 05-05-2012, 09:43 PM #14
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
- 05-05-2012, 09:46 PM #15
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
Re: Passing an array to another class in a JFrame
how would I get the already filled array to the coll?
-
Re: Passing an array to another class in a JFrame
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.
- 05-05-2012, 09:58 PM #17
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
Re: Passing an array to another class in a JFrame
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.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 ); } } }
-
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.
- 05-05-2012, 10:08 PM #19
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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.
- 05-05-2012, 10:13 PM #20
Member
- Join Date
- May 2012
- Posts
- 20
- Rep Power
- 0
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 ); } } }
Similar Threads
-
Passing 2 dementional array to a new class
By anonymous in forum New To JavaReplies: 4Last Post: 03-15-2011, 11:07 PM -
Passing array to model class
By keshaba in forum IntelliJ IDEAReplies: 0Last Post: 04-20-2010, 01:43 PM -
Passing data from one JFrame to another JFrame. - need help.
By Unsub in forum New To JavaReplies: 6Last Post: 04-12-2010, 11:33 AM -
Passing data from one JFrame to another JFrame
By tarami in forum New To JavaReplies: 3Last Post: 08-06-2009, 05:44 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks