Link to home
Start Free TrialLog in
Avatar of pigmentarts
pigmentartsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Storing data using ActionListener method

Hi

I'm playing a certain structure of clips and calling them from an array.The user has the option to click a button on a clip being played, at which point he/she is taken to another set of clips (All in the same array)

How would I store information about the clips chosen by a user in another array... hmmm and i guess I'd need to assign information to them as well.

Any offers?

Avatar of aozarov
aozarov

add a List (ArrayList or LinkedList) as memeber variable.
each time the user click a button that plays a clip add to the list the clip or the clip information.

>> hmmm and i guess I'd need to assign information to them as well.
Right in addition to the array of clips add an array of the names of the clips.
Avatar of pigmentarts

ASKER

where do I write the info about the clips...in here?

array_of_names_of_clips = new ArrayList[] {};

if so, I wanted to set two variables for each clip. As I was hoping to use that data to present back to the user. Is it possible?
Or I can just name my clips with the information I want, and then just print out to the user those clips he/she selected.
You add two member variables (in addition to index, and the array of clipse):
private String[] clipNames;
private List pickedClips = new LinkedList();

then at the same place you assigne the clips into the clips array you do:
clipsNames = new String[]{"clip", "clip2"}; // put here the name of any string info for each clip

when the user click (and you change the index) do:
pickedClips.add(clipNames[index])); // add to the picked clips list the name of the selected clip.
Ok I set that up as you said. But I'm having a problem inputting string info into here.

>>clipsNames = new String[]{"clip", "clip2"};// put here the name of any string info for each clip

What is the problem?
If you have a memeber vairable:
private String[] clipNames;
then you should be able to do:
clipNames = new String[]{"clip", "clip2"};
hmmm, not sure why I had that problem before.

ok so say i enter data into here, can I write. ... new String[]{"Well done!! Good choice you picked No5", You also chose No. 14"}

and then I have pickedClips.add(clipNames[index])); in the ActionListener method


Q. When the user gets to the end of the clips and has made the final selection how do I link straight to the pickedClips list.

Can i do it this way:-

else if (index < 8) {
           index = 8;
} else if (index < 22) {
               index=22;
} else if (index < 29) {   <<last selection
               index = println (pickedClips);




ok so say i enter data into here, can I write. ... new String[]{"Well done!! Good choice you picked No5", You also chose No. 14"}
and then I have pickedClips.add(clipNames[index])); in the ActionListener method
Sure, as long as clipNames have strings as much as clips (so index will not get out of bound).
and change: You also chose No. 14" to "You also chose No. 14"

>> Can i do it this way:-
Yes, though println(System.out.println) will not return a value (just a printout to the string)
what do you mean will not return a value, what value could it be outputting...data to make a chart or graph??

This is what I have under the ActionEvent method:

pickedClips.add(clipNames[index]);

 if (index < 8) {
           index = 8;
} else if (index < 22) {
               index=22;
} else if (index < 29) {
                 index = println(System.out.println) (pickedClips);  <<how do i add this?
                                                                   
                                                                   
                                                               
>> index = println(System.out.println) (pickedClips);  <<how do i add this?
What do you want to do?
If you want to display the picks
then do it this way:
System.out.println(pickedClips);
After the last clip has been selected, from the range 22 to 29 I want to print out the picked clips on the screen.

Using this I get an error  void, required: int

pickedClips.add(clipNames[index]);

 if (index < 8) {
           index = 8;
} else if (index < 22) {
               index=22;
} else if (index < 29) {
                 index = System.out.println(pickedClips);  
>> I want to print out the picked clips on the screen. (Using this I get an error  void, required: int)
I told you, System.out.println(pickedClips) does not return a value (so you can't assign it to index).
replacing
index = System.out.println(pickedClips);  
with just
System.out.println(pickedClips);  

will add a printout to the console, but I think in your case (GUI application and not console application) you want to add it to your JFrame.
So, add a JLabel to your JFrame (the same style you add your JButton, but without having an ActionListener for it) and then call
jLabel.setText(pickedClips.toString());
Assuming you added a JLabel named jLabel.
i'm a bit confused with what u mean by jLabel and JFrame

what i have done is to:

Label label = new Label ("testResults") {
};

then under that:

add(label, BorderLayout.CENTER);
Label.setText(pickedCLips.toString());

But there an error with   .setText         non-static method
I also set the size of the label
Change (there is no need for the {} in this case):
Label label = new Label ("testResults") {
};
to
Label label = new Label ("testResults");

JLabel is like Label but in the swing world (stick with Label, as you already did, for awt only)

>>add(label, BorderLayout.CENTER);
This should be added when you add the buttons

>> Label.setText(pickedCLips.toString());
label.setText(pickedCLips.toString()); // change L to l and do it in the section "else if (index < 29)..."



do it in the section "else if (index < 29)..."

Do you mean put it in the final "else if"??  This gives me an inner class error for 'label'

if (index < 8) {
           index = 8;
} else if (index < 22) {
               index=22;
} else if (index < 29) {
                 label.setText(pickedCLips.toString());
I still get the inner class error

I created the label under where I created the button and added the label afetr i added the button:

add(button, BL.CENTRE); add(label,.....);

The whole error is local variable label accessed from within inner class; needs to be declared final
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

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
ok...theres no errors..now i have to go through test it    80