Solved

Changing names of checkboxs without changing labels

Posted on 2004-04-09
7
178 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 49
Java Restore security prompts not working 10 140
java imports not found 4 52
What is the use of Forwarding Class in java 1 37
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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