Results 1 to 6 of 6
  1. #1
    paul4everyone is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Question adding dynamic rows to jtable

    please somebody help me. i want to be adding rows to my jtable component dynamically but i am still not getting something right.

    The following chunk of code is an extract from the project i am doing.

    Java Code:
    NumberFormat nf = NumberFormat.getNumberInstance();
         NumberFormat ns = NumberFormat.getNumberInstance();
    		nf.setMinimumFractionDigits(6);
                    ns.setMaximumFractionDigits(4);
            Vector<String> RowData = new Vector<String>();
            Vector<Vector> Rows = new Vector<Vector>();
           
     //adding item to my row with the methods i have designed previously
     
    int c = 2;
    RowData.addElement(ns.format(c));
            RowData.addElement(ns.format(mar));
            RowData.addElement(ns.format(msr));
            RowData.addElement(nf.format(TrafficInt(msr,mar,c)));
            RowData.addElement(nf.format(ProbUS(prs,msr,mar,c)));
            RowData.addElement(nf.format(ExpectUS(prs,msr,mar,c)));
            RowData.addElement(nf.format(ExpectUQ(prs,msr,mar,c)));
            RowData.addElement(nf.format(WaitingTS(prs,msr,mar,c)));
            RowData.addElement(nf.format(WaitingTQ(prs,msr,mar,c)));
            RowData.addElement(ns.format(expectedTimeSpent));
             
           while (c < 6) {c++;
    
       Rows.addElement(RowData);
            }
                     Vector<String> columnNames = new Vector<String>();
            columnNames.addElement("N. S. C.");
            columnNames.addElement("M. A. R.");
            columnNames.addElement("M. S. R.");
            columnNames.addElement("T. I. S.");
            columnNames.addElement("P. U. S.");
            columnNames.addElement("E. U. S.");
            columnNames.addElement("E. U. Q.");
            columnNames.addElement("E. W. S.");
            columnNames.addElement("E. W. Q.");
            columnNames.addElement("M. W. Q.");
            JTable table = new JTable();
                    table = new JTable(Rows, columnNames);
    the RowData supposed to be returning different values as c is either increasing or decreasing.
    i have tried all the methods i know but my c, always for reasons i dont know always remains 2.
    please help me, what do i need to do, or what are my wrong? thanks in anticipation
    Last edited by Fubarable; 05-08-2011 at 04:46 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by paul4everyone View Post
    the RowData supposed to be returning different values as c is either increasing or decreasing.
    Going to be hard for the rows to hold different data when you give them all the same data:

    Java Code:
    while (c < 6) {
       c++;
       Rows.addElement(RowData);
    }
    So in essence what your code does is this (in pseudo-code):

    Java Code:
    declare an int index variable c, and set it equal to 2.
    create RowData and fill it with data corresponding to some index of 2.
    
    loop from c = 3 to 6
       add the same RowData data into the Rows vector. 
       Hope that by changing the c index variable's value, the RowData information will magically change
    end loop
    You change c but never change RowData in the loop, so it makes sense that all the rows are identical. If you want different data in the rows, you're going to have to change RowData inside of the while loop.

    Also, I added code tags to your post above to help make the pasted code readable. To learn how to do this yourself, please see my link below. Also, you're going to want to use a consistent indentation style if you want others to be able to read and understand your code and help you.

    Luck.
    Last edited by Fubarable; 05-08-2011 at 05:02 AM.

  3. #3
    paul4everyone is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot, i really appreciate it. i will visit the links to learn more. but how do i make RowData change in the while loop?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by paul4everyone View Post
    Thanks a lot, i really appreciate it. i will visit the links to learn more. but how do i make RowData change in the while loop?
    By creating a new RowData vector inside of the while loop and filling it with data inside the while loop just as you did before the while loop, and then adding this vector into Rows.

  5. #5
    paul4everyone is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    i corect my while loop to the following chunk of code and guess what? it works correctly. thank you very very much.
    Java Code:
    while (c < 6) {
    RowData = new Vector<String>();
                 RowData.addElement(ns.format(c));
            RowData.addElement(ns.format(mar));
            RowData.addElement(ns.format(msr));
            RowData.addElement(nf.format(TrafficInt(msr,mar,c)));
            RowData.addElement(nf.format(ProbUS(prs,msr,mar,c)));
            RowData.addElement(nf.format(ExpectUS(prs,msr,mar,c)));
            RowData.addElement(nf.format(ExpectUQ(prs,msr,mar,c)));
            RowData.addElement(nf.format(WaitingTS(prs,msr,mar,c)));
            RowData.addElement(nf.format(WaitingTQ(prs,msr,mar,c)));
            RowData.addElement(ns.format(expectedTimeSpent));c++;
    Rows.addElement(RowData);
               }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Note that by convention, method and variable names in Java start with a lowercase letter.

    More here: Code Conventions for the Java(TM) Programming Language: Contents

    db

Similar Threads

  1. Adding Rows to a JTable
    By mlad in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2011, 02:09 AM
  2. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  3. Listening rows in JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 04:45 PM
  4. JTable insertRows() just adding blank rows
    By Jacob_ in forum AWT / Swing
    Replies: 4
    Last Post: 10-10-2009, 03:56 AM
  5. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 01:13 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
  •