By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,655 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,655 IT Pros & Developers. It's quick & easy.

using the nextToken() method with the jTable

P: 3
hi,
I have a jTable and I want to display in it a result of a command,then I used an object of the StringTokenizer class to separate between the lettres of the result and display the result in the table jTable1,but when I use system.out.println(st.nextToken()) it works correctly but in the table it display when I compile the last result
this is my code:

Expand|Select|Wrap|Line Numbers
  1. public Object[][] data;
  2.     public String  title[] = {"t1","t2","t3","t4","t5"};
  3.    private void init_tab() {
  4.  
  5.         data = new Object[5][5];
  6.              for(int i=0;i<5;i++){
  7.        try{ 
  8.         String      cmd = "the command "                        
  9.         ..... //command cmd traitment
  10.       }
  11.         String response = build.toString();  
  12.          StringTokenizer st = new StringTokenizer(response,":"); 
  13.            while (st.hasMoreTokens()) { 
  14.  
  15.                //System.out.println(st.nextToken()) ; 
  16.  
  17.  
  18.                data[i][0]= st.nextToken();
  19.  
  20.  
  21.        }
  22.         }catch(Exception e){
  23.     e.printStackTrace();}  
  24.  
  25.        jTable1= new JTable(data, title);
  26.         jScrollPane1.setViewportView(jTable1);
  27.                  }
  28.  
  29.              }
I don't know where is the fault,can some one help me and thanks for help
Mar 17 '12 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
In data[i][0]= st.nextToken(); you are just overwriting the same position with the same value all the time because the value of i never changes inside the while loop.
Mar 18 '12 #2

P: 3
thanks r035198x for your reply but how can I solve this problem,make a new loop or what?
Mar 18 '12 #3

P: 51
you dont need a while loop..
put an if statement inside the for-loop to check if st.hasMoreToken()
then try to print st.nextToken catch error.
Mar 19 '12 #4

P: 51
oops error..maybe its in your variable declaration.

in programming we should declare all the variable first before we used it..

but in this case..
your declaraion is inside your loop..

your code looks like this..
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<5;i++)
  2. {
  3. x=0;
  4. x++;
  5. System.out.println(x);
  6. }
  7. /*this code results
  8. 1
  9. 1
  10. 1
  11. 1
  12. 1
  13. because we declare x = 0 we add 1 then we print the x..
  14. then loop again.. 
  15. x = 0 we add 1 then we print x..
  16. all the results will be x=1*/
  17. //try to put your StringTokenizer declaraion outside..
  18.  
Mar 19 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.