Solved

Changing names of checkboxs without changing labels

Posted on 2004-04-09
7
175 Views
Last Modified: 2010-03-31
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
}


0
Comment
Question by:Drop_of_Rain
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10793599
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?
0
 

Author Comment

by:Drop_of_Rain
ID: 10793784
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.


0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 10793847
>>The names to be the same as the the actioncommand just not the .wav at the end of the file

The first line in the loop below will do that:

for(int i = 0; i < checkboxes.length; i++) {
      checkboxes[i].setName("kp" + (i + 1));
      checkboxes[i].setActionCommand("kp" + (i + 1) + ".wav");
      choices.add(checkboxes[i]);//go through checkboxs and add them to choices
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Drop_of_Rain
ID: 10793905
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?
0
 

Author Comment

by:Drop_of_Rain
ID: 10793911
P.S I have another question up if you could help?

 linking two sets of code together
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10793931
>>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 ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10793955
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 69
@SBGen Method 3 36
egit plugin on eclipse 8 31
windows explorer path to command prompt 5 29
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now