Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2006, 3:57 AM   #1
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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
säkki is offline   Reply With Quote
Old Apr 2nd, 2006, 4:06 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by säkki
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.
How did you decide that 9 goes in tf5? Or is that from some input source?

and maybe matrix[n/size][n%size] for indexing?

I'm not sure that I'm understanding the question correctly...
Jimbo is offline   Reply With Quote
Old Apr 2nd, 2006, 4:47 AM   #3
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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.
säkki is offline   Reply With Quote
Old Apr 2nd, 2006, 6:04 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Apr 2nd, 2006, 6:09 AM   #5
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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
säkki is offline   Reply With Quote
Old Apr 2nd, 2006, 8:09 AM   #6
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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
säkki is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC