Results 1 to 11 of 11
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post [SOLVED] Reading values from a jtable

    Hi pple,
    am writing a program whereby i read some values from a file and write them on a table.
    The proble is i want to get the last value recorded in the table. anyone who can help me with the logic?

    thanks

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    Go thru the Javadoc.This method is used for fetching the rec.

    public Object getValueAt(int row,
    int column)
    Returns the cell value at row and column.
    Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.


    Parameters:
    row - the row whose value is to be queried
    column - the column whose value is to be queried
    Returns:
    the Object at the specified cell
    Ramya:cool:

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    Go thru the Javadoc.This method is used for fetching the rec.

    public Object getValueAt(int row,
    int column)
    Returns the cell value at row and column.
    Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.


    Parameters:
    row - the row whose value is to be queried
    column - the column whose value is to be queried
    Returns:
    the Object at the specified cell
    Thanks for the reply but i think i wasnt clear enough for u t understand what i want :-(
    The table has 50 rows and the user can fill it upto whatever row. my problem is,i have no specific row and column because the data varries depending on the user.
    I want to use the last value keyed in or rather recorded for the purpose of initializing my calculations from previously recorded values.

    Any idea now???

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    You are having method called getRowCount().
    Say if it returns rowcount as four but,java starts counting from index 0.

    Ex:
    RowCount is 4 and column count is 1

    That means for retrival of last row and 1 column value then
    you have to pass
    table.getValueAt(3,0) and it will return the last row value.Is it clear?

    -Regards
    Ramya
    Ramya:cool:

  5. #5
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    You are having method called getRowCount().
    Say if it returns rowcount as four but,java starts counting from index 0.

    Ex:
    RowCount is 4 and column count is 1

    That means for retrival of last row and 1 column value then
    you have to pass
    table.getValueAt(3,0) and it will return the last row value.Is it clear?

    -Regards
    Ramya
    Please expound abit coz i have tried to use getRowCount() and this gives me the total number of rows in the table which is not my interest.
    The table has 50 rows but it might be filled upto the 10th row,that depends on the amount of data available.

    How can i get the value of that cell which i dont know??

    cheerz!

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    One small suggestion.Keep a counter during fileread.when enters value into table and comes out,then u keep that as a table rowcount.
    Please send ur piece of code if u have not got what iam saying
    -Regards
    Ramya
    Ramya:cool:

  7. #7
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Sounds like a good idea but i dont know how to go about it.
    The code below shows how i read the file into the table.
    check it out and tell me where i can fix the counter.

    Java Code:
     File f = new File("Xenon.temp");
     try {
     String aLine;
     int ro = 0;
     String t, p;
     FileInputStream fis = new FileInputStream(f);
     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
          
     //Read each line of the file
     while ((aLine = br.readLine()) !=null) {
              
     //Tokenize line into field values      
     StringTokenizer st =   new StringTokenizer(aLine, " ");
           
     if (st.countTokens() >= 2){
     t= st.nextToken();
     p= st.nextToken();
     } else {
     break;
     }
     Xenon.setValueAt(t,ro ,0);
     Xenon.setValueAt(p,ro ,1);
     ro++;
     } 
     br.close();
     fis.close();
             
     }catch (IOException e) {
     e.printStackTrace();    
       }
    Thanks

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    Ur variable ro will have know the no of lines in file.U can take that many number of rows will be in table know?
    -Regards
    Ramya
    Ramya:cool:

  9. #9
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    when i print it out on the screen i get a null :confused:

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    What you printed?

    I asked you to print "ro" value.

    Check the table whether all the file content ort loaded or not.

    -Regards
    Ramya
    Ramya:cool:

  11. #11
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Thumbs up

    Thanks alot i finally figured it out. This is how i was doing it :
    Java Code:
    String num = (String) Xenon.getValueAt(ro,1);
     System.out.print(num );
    But then i realised that getValueAt starts from index 0
    so the value i want must be ro-1

    Thanks for shading some light :)

Similar Threads

  1. Problem in printing JTable values
    By shanssat in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2009, 08:15 AM
  2. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 05:07 PM
  3. Getting integer values from a JTable
    By zikojam in forum New To Java
    Replies: 1
    Last Post: 12-11-2008, 02:06 PM
  4. Reading int values using BufferedReader
    By Java Tip in forum Java Tip
    Replies: 2
    Last Post: 01-24-2008, 07:26 PM
  5. reading values from config file
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 08-10-2007, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •