Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

Changing names of checkboxs without changing labels

Hello everyone out there hope all is going well. I have 2 questions here almost.

Question 1:
How could I change the names of the checkboxs 1-50 Code below: Code #1,  but not change the labels of the checkboxs named 1-50


Code #1

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {

ArrayList myArrayList = new ArrayList();

// Constructor
public MyClass4() {
// JPanel myPanel = new JPanel(); // No JPanel - why do we need it? I'M a JPanel...
setLayout(new GridLayout(0,3)); // Setting MY layout...
looprow:
for (int row=0; row<20; row++) {
for(int col=0; col<3; col++) {
int n=(20*col+row+1);
if (n>50) break looprow;
JCheckBox check = new JCheckBox("" + n);

myArrayList.add(check);
add(check); // Adding it to ME
}
}
// myPanel.setLayout(new FlowLayout()); Why would I change the layout back to FlowLayout? All hell would break loose...
// add(myPanel); Don't need it at it's ME...
}
}



Question 2:


All of the following checkboxs I have added set ActionCommands to be audio files. The code below works for one set of checkboxs named 1-50.  How would this code be changed to work with the checkboxs name changes from the  Question above.



for(int i = 0; i < checkboxes.length; i++) {
     checkboxes[i].setActionCommand("kp" + (i + 1) + ".wav");
     choices.add(checkboxes[i]);//go through checkboxs and add them to choices
}


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

1. you can use setName(String name) to give a component a name - this won't affect the label
2. Depends on what you want the names to be. Do you want the name and the action command to be connected in some way?
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

2. Depends on what you want the names to be. Do you want the name and the action command to be connected in some way?

The names to be the same as the the actioncommand just not the .wav at the end of the file. This way all the checkbox names and files will be the same. I think this would be good code writing.

I can see from your question I didn't express what i was tring to ask for.  I'm wanting to be able to write as little repetitive code as possible. Like setName for each one. more like the for statement. I am learning and and to think in these terms because my mind does that already in other ways.


ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland 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
That is great. I can see it, as I look at it. I just don't know how to write it yet. I more I see the code you write with such experience the more my mind is being able to think that way.  You guys are great techers for me!

Thanks again.

If I was to use variables in place of kp and choices, would be able to use that same code for other checkbox names and arraylists?
P.S I have another question up if you could help?

 linking two sets of code together
>>If I was to use variables in place of kp and choices, would be able ...

Not quite sure what you mean by that, but basically any Component can be given a name and that name, if set in a loop, can take on an identifier based on its index

>>P.S I have another question up if you could help?

Yes, i'm working my way there - hoping to build on my knowledge of what you're doing ;-)
8-)

I fear i might have to be back tomorrow to help with the others, although there's a slim possibility in a couple of hours