Saving JTable Data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rburke1125
    New Member
    • Nov 2008
    • 3

    Saving JTable Data

    Hello All! I'm a bit new to Java, and certainly to the community. I'd appreciate any and all pointers to the right direction!

    I have a JTable that I have populated with a 2 dimensional array. During the period of the data sitting there, it will be modified. When finished, I would of course like to export it back to a 2 dimensional array.


    I really have no clue on the best way of doing this. I assume JTables do not have a 2-dim array for it's backend, and probably uses a vector or something.

    My first attempt at a solution was to create a new 2 dim array with dimensions JTable.getColum nlCount() and JTable.getRowCo unt(). When I do this though, I'm continually getting out of bounds exceptions. I presume this comes from the rowCount and ColCount methods not actually being the size of everything I'm reading it.




    Does anyone know what's causing this, or better yet a superior way to export my data to an array?


    THANKS!

    Rob
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    Hi,

    Have you tried debugging the code? Step through if you are using an IDE with a debugger or use good old System.out and see the values at the critical places. You will probably get it over with this way.
    Also a JTable does not care how you store the data. It can be an array, vector or anything as long as you can supply the data as required. You actually manage the *back end*.

    And of course, seeing the code where you are stuck will help us answer you better.

    rgds,

    Alex.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      A JTable leaves the storage of the actual data to a TableModel which is an interface. Feel free to implement that interface by using a two dimensional array. There is no need to im/export anything then.

      kind regards,

      Jos

      Comment

      • rburke1125
        New Member
        • Nov 2008
        • 3

        #4
        Code:
        	public JScrollPane addCSVTable (File newFile) throws IOException
        	{
                
                CSVDoc csv = new CSVDoc(newFile);
                String[] columnNames = csv.getHeaders();
                 
                String[][] data = csv.getArray();
        
                
        
                final JTable table = new JTable(data, columnNames);
                table.setAutoResizeMode(table.AUTO_RESIZE_OFF);	
                JScrollPane scrollPane = new JScrollPane(table);
                
                
                return scrollPane;
        		
        	}
        this is how I create each table. Then the scrollPane is placed within a JSplitPane, which is then put in a tab.

        I see there's a few constructors for the JTable that takes a TableModel as a parameter. I guess my only question with implementing this is, how do I go about adding my header data?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Did you read the appropriate API documentation? It's all in there; in the sticky article "Read This First" you'll find a link which lets you download the entire documentation on your hard disk. Download and read it; it is worth it.

          kind regards,

          Jos

          Comment

          • rburke1125
            New Member
            • Nov 2008
            • 3

            #6
            I have read through the documentation for it - my confusion was coming from what appears to be only getter methods (at least documented) for the tablemodel.

            Nevertheless, as I'm sure you suspected, because of counters not matching up properly. I had my rowCount and colCount crossed at one point, and thus the array was populating the width as height and vice versa.


            Thanks for the link for the downloadable documentation, very handy.

            Thanks again all!

            Comment

            Working...