• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Save & Load From Jtable and Auto Increment Problem  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello im new to java , and i would like to know how can i save information from jtable into the application itself so when you open application again you can see your saved information inside the application here is my add codes :



and for Delete button :


Also i have problem in auto increment in first ID it start like this : 1-1-2-3-4-5-etc. when you delete for example 2 it be like this 1-1-3-4-5 when you delete another one it reset and goes back to 1-2-3-4-5 .
how can i make auto increment choose select row and reset the numbers that when you add a new row it add +1 to the last row .
 
Marshal
Posts: 60803
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using the old date classes? Start using the newer classes, which are much better.
There are all sorts of ways you can persist information. You can save it into a database when you close your app. You can write all the information into a text file or an XML file. You can have an object that encapsulates all the information and serialise it to an object file. You can write the data to a binary file, e.g. a random access file.
I am afraid I don't understand what the bit about autoincrement means. Please explain what the rules are.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply Campbell Ritchie , Here is what i mean by auto increment problem :

it's normaly when you add rows it add +1 into the row like this :



Here when you delete some rows then add another row it doesn't choose the last row to add +1 to it it keeps adding the last number of rows you added before :



can you send a tutorial how can i save jtable to a binary file because i don't want to use sql for an offline application , i am sorry i am new to java . ^^
 
Campbell Ritchie
Marshal
Posts: 60803
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the Java™ Tutorials about files. It has another section about tables; maybe that will say something about numbering. i think you mean that you always want the row numbers to be consecutive whenever you insert or delete a row. I didn't see anything about numbering in the DefaultTableModel documentation. Otherwise don't know, sorry.
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it's normaly when you add rows it add +1 into the row like this :



Row numbers: 1,2,3,4,5,6,7

Here when you delete some rows then add another row it doesn't choose the last row to add +1 to it it keeps adding the last number of rows you added before :



Row numbers: 1,2,3,8,9,10,11

What are the rows (row numbers) you deleted in the above example.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:

it's normaly when you add rows it add +1 into the row like this :



Row numbers: 1,2,3,4,5,6,7

Here when you delete some rows then add another row it doesn't choose the last row to add +1 to it it keeps adding the last number of rows you added before :



Row numbers: 1,2,3,8,9,10,11

What are the rows (row numbers) you deleted in the above example.



deleted row numbers : 4,5,6,7
for example when you add rows it stars normally : 1,2,3,4,5,6 etc.
when you delete for example row number 2 , rows reset to 1,2,3,4,5 but when you add a row it add number 7 instant of 6 ( 1,2,3,4,5,7) .
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cannot solve this for 3 days , can someone post a code that auto increment the first coloumns with last row detection ?

also how can i save my files in binary files and load it again in jtable .
 
Sheriff
Posts: 5268
142
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to decide is how you're going to persist data to the disk.

Campbell Ritchie wrote:There are all sorts of ways you can persist information. You can save it into a database when you close your app. You can write all the information into a text file or an XML file. You can have an object that encapsulates all the information and serialise it to an object file. You can write the data to a binary file, e.g. a random access file.


Once we know that, we'll know how to "auto increment". Many databases have auto increment functions you can use.  Do you know SQL?  If so, I'd choose an SQL database and use JDBC to communicate with it.  If you're only going to have a small number of rows then a database might be overkill.  Do you know XML?  Then you can use one of two XML file manipulators built into Java.  If not, that leaves serialization or a binary file.  I think serialization is easier, but Campbell might disagree with that.
 
Campbell Ritchie
Marshal
Posts: 60803
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . Campbell might disagree with that.

Only if I am feeling awkward

Knute is right that serialisation is easy to use for something straightforward.
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cannot solve this for 3 days , can someone post a code that auto increment the first coloumns with last row detection ?
also how can i save my files in binary files and load it again in jtable



for example when you add rows it stars normally : 1,2,3,4,5,6 etc.
when you delete for example row number 2 , rows reset to 1,2,3,4,5 but when you add a row it add number 7 instant of 6 ( 1,2,3,4,5,7) .



Yes, I understand what you mean. There is a problem. There are problems.

You may consider this:
Where are you generating or getting the row numbers from? It must be some kind of function which creates row numbers in an incremental way. Lets say a method that returns a row number. Which also means an instance variable where one stores the last row number used.

What happens if you close your application. The row numbers will start from 1 again. For this you may have to persist (or store) the last used row number on the hard drive. In a file or a database.

Questions:

Lot questions arise when one looks at what you had posted. Its not clear to me. I don't know really where to start and what to help you with.

An application does something. In your case the app has a table with rows of data. Each row represents a data record with fields (like row number, date, etc.). You are adding data and deleting them from the table. What happens when you close the application. The data you had entered in the table disappears. So, you need to figure how to store the data. In a file or a database.

Some questions you need to answer to yourself and tell us to help you.

First, what is the data the application is handling? Is it a Person data or a Product data or Invoice data or something-else.
Second. What operations are you doing. Adding new data and deleting them. Are you modifying and updating them? Are you querying the data?
Third. Where are you storing them. In a database or a file.

These you need to be clear. First.


The Database:

In case you choose to use a database, a Java based database is a possibility. These are very small databases which run within the application's JVM. That is the database starts with the application and closes with the application. May not need any maintenance, otherwise. Some of these databases are the Apache Derby (also known as Java DB), HSQLDB and H2 database.

If your application is small you may just end up with using one or two database tables; but thats alright. These databases are also  meant to be used with small applications. When you use these databases you will have facilities like generating the row numbers automatically by the database itself in a correct manner. You do not have to worry or manage those. Also, these databases integrate well working with Java objects.


Finally:

I think you need to have a clear picture of some or all of the aspects of your application before you start coding and debugging the code.

Suppose you want to make a cup of tea and you will probably say - Get a tea bag, some water. Put them in vessel. Then put the vessel on a stove and let the water boil and the tea brew. Then add some sugar, cream etc as you wish. Serve and enjoy. See its so easy. One has to think about the program in a similar way. It has to be simple (even with lots of ingredients) and clear. Then the program writes itself.

Even if you are new to Java programming, you still need to consider all these to get the result (whatever it is) you are looking for.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what the program does it calculate values from table into jtextfields and it store jtable into .txt file using ObjectOutPutStream Then read it back Using ObjectInPutStream but when it read back it starts from 1 again after adding another row , yes my program does ( add , update , delete ) from table .

here is my add auto increment method :



another method i used and it resets but it had some issues it add first row like this : 1,1,2,3,4,5,6 when you delete for example number 2 rows reset like this : 1,1,3,4,5,6 but when you add another row it add the same last number like 1,2,3,4,5,5,6 :


i know how to solve this by jdeveloper database by creating table id and start_with : 1 , Auto_Increment : 1 ;

but my program is offline so it will be more useful to use .txt file instant of a database .
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the entire code you had developed so far. So that we can see where the problem is. If there are any latest (or new) screenshots of the application, attach them too.
 
Knute Snortum
Sheriff
Posts: 5268
142
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:Please post the entire code you had developed so far.


Or create a SSCCE (that's a link).  In doing so one often finds the solution to the problem.  If not, at least you have code that hones in on the problem.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:Please post the entire code you had developed so far. So that we can see where the problem is. If there are any latest (or new) screenshots of the application, attach them too.



i fixed my problem with this code you have to put :

out side of the add button then add for lope inside :


after that add Object ( i ) inside your row :



i was using if statement and while loop and didn't realize that for loop with <= sign will fix my problem ^^ .
 
Campbell Ritchie
Marshal
Posts: 60803
190
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to avoid <= and >= because < and > are easier to read. Please note the formatting and spacing for a loop, as well as indexing starting from 0:-Alternative version, moving the increment to later:-
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I try to avoid <= and >= because < and > are easier to read. Please note the formatting and spacing for a loop, as well as indexing starting from 0:-Alternative version, moving the increment to later:-



thank you both of your methods is working
 
Campbell Ritchie
Marshal
Posts: 60803
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure

Please don't quote all the preceding post; that simply makes the discussion longer and longer.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!