Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing names of checkboxs without changing labels

Posted on 2004-04-09
7
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 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