473,790 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving JTable Data

3 New Member
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
Nov 27 '08 #1
5 5222
myusernotyours
188 New Member
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 Recognized Expert MVP
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
rburke1125
3 New Member
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 Recognized Expert MVP
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
rburke1125
3 New Member
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
8636
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 CheckBoxRenderer which extens Checkbox and implements TableCellRenderer. This seems to have done the job, in that the Checkbox is displayed and not the String representation. But, and it's a big 'but', the column that holds the Checkbox continually...
0
3147
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 jtable. when user clicks that combination what ever editor present in that cell will appear. I am using tab for traversing all cells in table.
1
6746
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 the follow: 1. Load: Database -> disconected Rowset -> automapping in a JTable 2. Editing of the data in the table/rowset
1
2591
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 extend AbstractTableModel, and simply define three methods: object getValueAt(int i, int j); int getColCount(); int getRowCount();
1
7392
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 java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException;
7
17223
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 then it would change whether that third column is editable or not. I have made a short array that would hold values of either 0 or 1, basically whether or not each cell is editable or not. Along with that is obviously a setEditable(int row, int...
0
1895
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. please help me . iam sending my code here CODE: package classes;
5
9853
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. But i want the table to display the data from a .txt file in which i had stored information in this format. Can anyone please modify the codes and explain me. Name:Mr.xyz Phone:111111222222
1
5844
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. import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TableAvecRenderer extends JPanel {
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.