View Full Version : Save data (dynamic) then clicked a button
I hava a Frame as follow:
http://quangnd0003.googlepages.com/Frame.JPG
I want then Next clicked, all infos of currently record will be saved. Then textboxes will be clear! Continue, I enter a new record, and save again..etc...
I also want when Previous button is clicked, all infos are showed on textboxes... :(
I try to use 2 dimensions array to save infos, but I can't control number times click button. So I can't contain record into array !
What must I do ? help me :(
yonni
06-03-2007, 08:48 AM
From the looks of what you're doing, wouldn't a database be much more appropriate?
Aradon
06-03-2007, 05:32 PM
Well you could do this in a couple ways. The most robust solution would be using a database. This way you could have a large amount of data stored, and not worry too much about overflow.
The other, less robust but eaiser, solution is to use an ArrayList. An ArrayList is a LinkedList that uses an Array Implementation. This becomes highly convienant when you want to keep track of indices'.
On solution, using an ArrayList, is to keep a counter of some sort, let's call it count. If your count is greater then the ArrayList size, you show blank textboxes to be entered. But if the count is less then the size of the ArrayList, then you show the previous data.
There are two things to watch out for in this. The first is to make sure that count ONLY increments to size. So that you are always just inserting in the last element on the ArrayList.
The second is to make sure that count never goes below 0 in the ArrayList, as you will get indexoutofbounds exceptions.
I hope I explained this well enough, if you have any questions, let us know.
With your advice, I try to use ArrayList, but maybe it's confuse with me..
Instead, I used 2 dimensions array (although it can't store dynamically).
1 dimension contain attributes and other contain value
(if you can, please show me the way to use ArrayList object clearly... or show me articles that relate to it...! :) )
Code:
String array[][] = new String[7][100]; // global variable
...
array[0][i] = txtId.getText();
array[1][i] = txtName.getText();
array[2][i] = txtAuthor.getText();
array[3][i] = txtPrice.getText();
array[4][i] = txtQuantity.getText();
array[5][i] = String.valueOf(chkNew.getState());
array[6][i] = txtNum.getText();
//i will auto increase 1 then click Next or decrease 1 then click Previous ! I processed Previous and Next button correctly !
It's successful ... to store in array ! But I can't catch exception (elements of array is -1 ), how to then click Previous Button at the last record, 1 message box showed, and program still continue...then I click Next button !
(I tried to use JOptionPane.showMessageDialog when array.length = 0 but then I can't run program with Next button or anything ... !
A question that I think it's important...What must i do to save values of array into a file, hix, I used FileWriter but not successfull ! What must I do ?
And I also want to Load all data into textboxes then Load button is clicked (Load button will open file which saved data above)
I can't use DB, it's so difficult with my knowledge at present... :(
thanks a lot !
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.