Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: jTable to Array

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jTable to Array

    I have five columns in my jTable and I want to retreive the data from the second column to an array

    my jTable can be like this:


    Column1 Column2 Column3
    50.45 45.56 34.56
    67.78 23.34 56.67
    45.34 12.34 23.34
    34.67 10.10 34.56

    The problem is how to retreive all of value in the second column (column2) so the output
    may be like:

    double[] data = {
    45.56,
    23.34,
    12.34,
    10.10
    }

    Can anyone please give me a sample code, class or function to solve this problem. Thank you very much....


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable to Array

    Have you looked at the API for JTable? It has handy methods for getting the value at a specific index.

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: jTable to Array

    You can read value of each cells you want, by this Jtable method getValueAt(int rowIndex, int columnIndex), then add values to the output array.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jTable to Array

    Thank you ...finally i get the solution
    Here it is:

    public Object extractTable(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index, col_index);
    }


    double[] data = new double[22];

    for(int i=1;i<22;i++)
    {
    data[i]=Double.parseDouble(extractTable(jTable1, i, 2).toString());
    }

  5. #5

    Default Re: jTable to Array

    Quote Originally Posted by nandosby View Post
    Can anyone please give me a sample code, class or function to solve this problem. Thank you very much....

    Use a for loop to run from first row to last row, then invoke jtable.getValueAt(row, column) to retrieve the value of each row at the specified column. Add all the values to an array.

Similar Threads

  1. JTable
    By gogiseq@gmail.com in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2011, 05:24 AM
  2. JTable
    By adra in forum Java Theory & Questions
    Replies: 5
    Last Post: August 30th, 2011, 07:05 AM
  3. JTable
    By timmin in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2011, 12:10 PM
  4. Hello everyone! I need help with JTable
    By hektor in forum AWT / Java Swing
    Replies: 6
    Last Post: October 11th, 2010, 10:11 AM
  5. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM