![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
how should I...
So little background first:
I have setted in GUI JTextFields in matrix-form, the alignment depends on the selected size of the matrix. For example: size = 2 tf0 tf1 tf2 tf3 size = 3 tf0 tf1 tf2 tf3 tf4 tf5 tf6 tf7 tf8 and the problem is how to read the number in the textfield and set it into the right index on the matrix -> size=3, tf5 gets value 9 -> matrix[1][2] = 9. Can someone come up with a way to do this easily or am I just going to do long idiot code with switch-case. If you know a better way to do this please tell me. Thanks |
|
|
|
|
|
#2 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Quote:
and maybe matrix[n/size][n%size] for indexing? I'm not sure that I'm understanding the question correctly... |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
Well 9 is just some value that the users enters.
Main point in this part of the program, is to collect the values to a cipher matrix. hmm, how could I say this problem is that I have put a running numbering to the JTextFields with .setActionCommand(). The matrix is always a square matrix = 2x2, 3x3 etc. So the numbering goes 0 - > size*size -1. Exampel size=3 0 1 2 3 4 5 6 7 8 But the matrix where I have to enter the values, that user puts into the textfields, is indexed this way 0/0 0/1 0/2 1/0 1/1 1/2 2/0 2/1 2/2 Of course I could just code some checking method that enters values, by making a different case to each size and so on. But I just think there might be easier (smarter) way to do this. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Perhaps you could post up some code? I'm still unsure of what exactly it is you mean, or why placing the values in a dynamically sized matrix is so difficult. As Jimbo says, matrix[n/size][n%size] should convert from a one-dimensional list to a two-dimensional matrix.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
well it's not hard actually. I just hoped there would have been one method that works for all cases.
I don't have any code done regarding this problem yet and the actual _code_ isn't the problem I believe, but the idea of how the hell it should be done well I have time so I'll just code it with all the different cases if I don't come up with a better way. E: As Jimbo says, matrix[n/size][n%size] should convert from a one-dimensional list to a two-dimensional matrix. I'll try that, thanks |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
thanks for that tip and more help needed
Now the thing is that how am I supposed to read both the index number and the given value of all the text fields. So how to name the textfields with a running number or something. Some code, that's not right at all: //this is where JTFs are inserted into a panel
private void valuepanel(){
valuePanel.setLayout(new GridLayout(size,size,15,25));
for (int i = 0; i < (size*size); i++) {
values = new JTextField();
values.setActionCommand(""+i);
values.addActionListener(new valueListener());
valuePanel.add(arvot);
}
}
//listener
class valueListener implements ActionListener {
public void actionPerformed (ActionEvent e){
int tf = Integer.parseInt(e.getActionCommand());
String textfield = values.getText();
valueMatrix[tf/koko][tf%koko] = Integer.parseInt(textfield);
}
}I have to go now so I won't be able to answer question at the moment, but hopefully you understand what I'm trying to say here |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|