Solved

Changing names of checkboxs without changing labels

Posted on 2004-04-09
7
174 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Drop_of_Rain
Comment Utility
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
Comment Utility
P.S I have another question up if you could help?

 linking two sets of code together
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now