please hep

hi, im trying to do this for my university assignment, and im not too good with Java so i thought i should ask here.

what im trying to do is this :

for (int i = 0 ; i < x ; i++)

{

for (int j = 0 ; j < y ; j++)

{

//set a text box with the name txt(i)(j) to visible

}

}

i've tried everything, and cant seem to get anything to work. i bet its really simple too. thanks

[434 byte] By [scene316] at [2007-9-30 23:27:34]
# 1
Have you tried JTable?You can do this by overridinging the DefaultCellEditor
Peter-Lawrey at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 2
I assume that you have more code than this!What is your framework? Where are you placing your code snippet?
sabre150 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 3
nope, havent tried using a JTable. would rather know how to do what im trying to do first though because it would make my whole program work.
scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 4
You would use a java.util.Map, with the name as key and the text box as value.
nasch_ at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 5

Try this:

for (int i = 0 ; i < x ; i++) {

for (int j = 0 ; j < y ; j++) {

//set a text box with the name txt(i)(j) to visible

((TextBox)textBoxMap.get("txt("+i+")("+j+")")).setVisible(true);

}

}

ehodges at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 6
the rest of the code isnt really relevant is it ? its within an RMI Client implementation for a remote matrix calculator. this bit of code it only for a bit of GUI, where when you press a button, it will display a number of text boxes, as defined by the numbers in a couple of combo boxes.
scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 7

> Try this:

>

> > for (int i = 0 ; i < x ; i++) {

>for (int j = 0 ; j < y ; j++) {

> //set a text box with the name txt(i)(j) to

> ) to visible

>

>

>

> ((TextBox)textBoxMap.get("txt("+i+")("+j+")")).setVis

> ible(true);

>}

> }

>

i tried that, but it gave two errors at compile - cannot resolve TextBox.- cannot resolve textBoxMap. what packages should i import ?

scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 8

[snip]

> i tried that, but it gave two errors at compile -

> cannot resolve TextBox.- cannot resolve

> textBoxMap. what packages should i import ?

That code was more a guideline than something you can copy and paste. TextBox would be JTextBox (or whatever TextBox you were talking about in your OP) and textBoxMap is the map of the things to the names that _nasch was talking about. You need to import java.util.HashMap or whatever map implementation makes you happy.

Good Luck

Lee

tsith at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 9
s/_nasch/nasch_(sorry)
tsith at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 10

not sure i understand. i will explain more of my code so maybe you can help. i have a series of text boxes named txtA00 txtA01 txtA02 txtA10 blah blah blah. i would like to make them visible when the button is pressed, yet only a certain amount of the text boxes depending on the numbers in the combo box. so if 1 and 1 were selected, it would loop through and display the text boxes txtA00, txtA01, txtA10 and txtA11.

scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 11

> not sure i understand. i will explain more of my code

> so maybe you can help. i have a series of text boxes

> named txtA00 txtA01 txtA02 txtA10 blah blah blah. i

> would like to make them visible when the button is

> pressed, yet only a certain amount of the text boxes

> depending on the numbers in the combo box. so if 1

> and 1 were selected, it would loop through and

> display the text boxes txtA00, txtA01, txtA10 and

> txtA11.

So, as nasch_ suggested and ehodges demonstrated, construct a Map where the keys are txtAXX (or whatever your names are) and the values are the references to the text boxes themselves, and then you can get the list of Strings that are the names of the text boxes you want to make visible (these names are the keys in the map), then go through your list of Strings, get the references to the textboxes that map to those keys and make it visible.

Make sense?

tsith at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 12
The requirements aren't very clear, but perhaps a two-dimensional array of text boxes would work?
DrClap at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 13
i kind of understand, but not sure how to edit that demonstration code to make mine work.
scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 14
all i basically want to do is txtAij.setVisible(true);where i and j are incrementing variables.
scene316 at 2007-7-7 14:03:18 > top of Java-index,Java Essentials,Java Programming...
# 15
i cant seem to get mapping to work, any tips?
scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 16
Post relevant parts of your code and tell us what happened and what you expected to happen.
tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 17

for (int i = 0 ; i < x ; i++) {

for (int j = 0 ; j < y ; j++) {

//set a text box with the name txt(i)(j) to visible

((TextBox)textBoxMap.get("txt("+i+")("+j+")")).setVisible(true);

}

}

this is the code that someone said to customise.

here are the things im using:

numerous text boxes with the naming structure txtAij (i and j are variables from the for loop)

line of code would i need to write instead of the one with TextBox and textBoxMap ?

i wish the String class just had a method that could convert a string into a statement.

here is what im trying to do in a language that doesnt exist:

*assume i and j are ints, both 0

txtA(i)(j).setVisible(true);

which would be

txtA00.setVisible(true);

scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 18
pleeeeease help
scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 19

I don't know what you are having trouble with. The code you posted won't compile. There are only so many times we can say "Make a Map" - though I like the Doc's idea of a two dimensional array indexed by the numbers you're talking about, unfortunately I don't know if that will work because in your example "1" & "1" mapped to things I wouldn't have expected... Maybe you meant "1" and "0"?

Please ask a specific question.

tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 20

the simple thing is that i do not know how to make a map, since i have never done one before. i will try and explain things with a more simple idea.

i make two JTextFields called txt1, and txt2, and i set both of them to not be visible.

i have a combo box, with two choices, 1 or 2.

i have an int, x, which is given the value of the selection in the combo box, either 1 or 2

then i have a for loop, as follows

for (int i = 1; i <= x ; i ++)

{

txt(i).setVisible(true);

}

so if 1 is chosen in the combo box, only txt1 will be visible, and if 2 is chosen, both txt1 and txt2 will be visible

i know that code wont work, but its what im trying to do. in this example it would obviously be easier to do an if statement, yet for a large number, it would be less code to use a for loop.

i know i must sound really dumb not understanding you, and i really appreciate the help.

scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 21

No, see - we understand your problem - what we don't understand is your question!

We know what you want to do, and it's been asked here a lot (search for "naming variables at runtime"). Look at the HashMap class (note that if this is a homework assignment you may not be allowed to use that) here:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html

This things stores relationships between objects. You want to be able to store a relationship between a String name ("txtA00", for instance) and a JTextField. This way you can use your existing code to build the String name by saying String name = "txtA" + i + j;

and then you can get a reference to the JTextField by saying JTextField tb = (JTextField)myMap.get(name);

and then you can set it visible by sayingtb.setVisible(true);

Yes we know there's no myMap, you have to make that yourself. It's a HashMap of names you have made to JTextField references you have made. Check the API docs for HashMap.

It's not that you sound really dumb, it's just that precise communication is important to get anything done on these fora.

So does that clear anything up?

tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 22
but im not trying to name the variables at runtime, im trying to alter the statement at runtime.
scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 23
Well, you can't really do either. Variables and statements are compile-time entities, period. So instead of trying to do one of those, we're suggesting that you use the Map solution instead. What part of the Map approach do you not understand?
nasch_a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 24

> but im not trying to name the variables at runtime,

> im trying to alter the statement at runtime.

For the Love of God, scene316! You are Killing me here!

Unless I am totally missing your question, which is admittedly a decent possibility, you are wanting to call a method on a reference that you will determine at runtime, correct? And since you can't do an eval or some other thing then use a frikkin' map like every other person who asks this frikkin' question!

(I was smiling the whole time I wrote that, just to let you know - though my eye is starting to twitch)

tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 25
"You're so bad that it makes me want to push my finger through my eye into my brain and swirl it around." FriendsJust reminded me of that. :-)
nasch_a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 26

im really sorry, but what do i actually have to put in the HashMap ? im just finding it really hard to get my head around it ! could one of you give me a basic outline of how this mapping works ? im feeling really stupid now. and im apparently one of the smartest people at my uni. you've been really helpful, and im sorry !

scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 27

dude (or dudette) - you put - there goes my eye - you put the names and the references to JTextFi...

You know, maybe the doc's 2-d array idea would be better. Do that - make a two dimensional array of JTextField references indexed the way you want (with your "i" and "j" values) and then you can get to a given txrAij reference by saying "yourJTextFieldArray[i][j]" Does that make more sense?

And no need to apologize, it's often hard to get your head around new concepts. No worries.

tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 28

yeah, it will probably be easier to try the other way, or another totally different GUI. the problem is i've got 25 text boxes for each matrix, A, B and the result C, hence the naming structure that i was implementing txtA00 (text box from matrix A, 00 being the co-ordinates) . so the map would have to have every name (txtA00 blah blah i guess ? ) yet i dont understand what you mean by reference ?

scene316a at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 29

When you say something like JTextField txtA00 = new JTextField();

The "thing" that's accessible through the name "txtA00" is what I mean by reference. So when you say myArrayOfTextFields[0][0] = new JTextField();

you are sticking a reference to a JTextField in the 0,0 element in your 2-D array of JTextFields.

tsitha at 2007-7-20 1:23:48 > top of Java-index,Java Essentials,Java Programming...
# 30

You'll do one of the following: JTextField[][] arr = new JTextField[5][5];

arr[0][0] = // text field 0,0 goes here

arr[0][1] = // text field 0,1 goes here

// etc.

...

// user selects "3" and "4"

int i = // get the 3 the user picked

int j = // the 4 the user picked

arr[i][j].doSomething();

OR

Map map = new HashMap();

map.put("0.0", /* text field 0,0 goes here */);

map.put("0.1", /* text field 0,1 goes here */);

// etc.

...

// user selects "3" and "4"

String i = // get the 3 the user picked

String j = // the 4 the user picked

JTextField jtf = (JTextField)map.get(i + "." + j);

jtf.doSomething();

Here's a tutorial on the Collections Framework, of which Map is part:

http://java.sun.com/docs/books/tutorial/collections/

jverda at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 31
i think im gonna do it differently. i'll just keep the textboxes visible at all times and just tell the user off if he makes stupid matrices.
scene316a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 32
> i think im gonna do it differently. i'll just keep> the textboxes visible at all times and just tell the> user off if he makes stupid matrices.Or you could take the copious advice you've been given and try to learn something by applying it.
jverda at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 33
jverd, that example makes 100% sense now ! thanks buddy
scene316a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 34
Cool! You're quite welcome. I'm glad the lightbulb finally popped on!
jverda at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 35

it has taken me ages to understand, but i just wanted to say how helpful everyone has been ! I can appreciate your frustrations at my slowness to understand, but i have finally got it working now. if i dont get an A+ for this i will sue my lecturer ! and yes, i'm doing a law degree aswell as computer science, so i know my rights ! anyway, thanks again, you've all been really helpful, i just hope i can help all the people that helped me someday.

scene316a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 36
can you do something about this tick? Got any pre-med friends?:-)
tsitha at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 37
Pre-Med ? im in England, guess the language barrier is in operation.
scene316a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 38
Y'all speak English over there still, right? :-) Pre-med is pre-medical, or an undergraduate degree preparatory to medical school.
nasch_a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...
# 39

We have always spoken English, but there is a problematic new language called text-speak (mobile(cell) phone language)... yet i digress....my Uni doesnt have any med students on campus, they are out in the community at large, eg at classrooms at the local hospital. Now the Media studies students....

scene316a at 2007-7-20 1:23:52 > top of Java-index,Java Essentials,Java Programming...