473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.getColumnlCount() and JTable.getRowCount(). 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
Nov 27 '08 #1
5 5197
myusernotyours
188 100+
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.
Nov 27 '08 #2
JosAH
11,448 Expert 8TB
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
Nov 27 '08 #3
Expand|Select|Wrap|Line Numbers
  1.     public JScrollPane addCSVTable (File newFile) throws IOException
  2.     {
  3.  
  4.         CSVDoc csv = new CSVDoc(newFile);
  5.         String[] columnNames = csv.getHeaders();
  6.  
  7.         String[][] data = csv.getArray();
  8.  
  9.  
  10.  
  11.         final JTable table = new JTable(data, columnNames);
  12.         table.setAutoResizeMode(table.AUTO_RESIZE_OFF);    
  13.         JScrollPane scrollPane = new JScrollPane(table);
  14.  
  15.  
  16.         return scrollPane;
  17.  
  18.     }
  19.  
  20.  
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?
Nov 27 '08 #4
JosAH
11,448 Expert 8TB
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
Nov 27 '08 #5
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!
Nov 28 '08 #6

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

Similar topics

1
by: Ben Munday | last post by:
Hi all, I have a problem with a Checkbox in a JTable. When i add the Checkbox into the JTable it displays the String representation of the Checkbox, which is not what i want. So, i created a...
0
by: sivaprasad06 | last post by:
Hi, I have a editable combo box editor as a cell editor for JTable. I want use only key board to enter data into jtable. so i am using ALT+Down comobination for making cell editiable in the...
1
by: Andrea Sansottera | last post by:
Hi fellows, I was wondering if in the Java API there's a way to easily map a disconnected RowSet to a JTable... I have looked at the doc but i did not found anything. What I want is simply do...
1
by: The Garbage Collector | last post by:
Hey guys, Heres something I used to do very commonly in java, and I'm having a hard time coming up with a good paradigm for how to manage this in c#. In Java, to show data on a grid, you need to...
1
by: onsir | last post by:
I have code like this, but still wrong. how to show data from database in JTable. package my.JavaNetBean; import java.sql.*; import java.io.*; import java.sql.Connection; import...
7
blazedaces
by: blazedaces | last post by:
Hi guys, what I'm I have is a jtable with boolean variables in the first column and text in the next two (checkboxes). I want my tableChanged method to make it so when I check one of the checkboxes...
0
by: san1014 | last post by:
Hi to all' I am using Java swings. I written a code to display a table. In the table i used Boolean.class type column. when i click the checkbox it value to be change from false to true....
5
by: moizpalitanawala | last post by:
Hello friends, How to add data to JTable from a .txt file. I had seen this code from one of the tutorial from java.com. This code doesnt take any input. But shows what is written in the array....
1
by: emekadavid | last post by:
can anyone help me flesh out the problem? I wanted to insert a combo box into one of the columns of a JTable. although the combo box is drawn, the list of the data model fails to render. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.