Hey guys. I'm making a project for my university. It consists of Jframes and other JStuff.
I'm having problem with data. The problem is changing them.
I have 2D array of Strings - numbers and words (but the content doesn't matter).
I want to make a JFrame, where you can choose row&column and set new value to one string of the array.
Inputs will be made in JLabels, supposably. But i don't know, how to change them.
I have a method, where you can get data from an array like getData(int row, int column) - that's easy.
But how to set data?
I also have a JTable, where all the data are shown in the table.
when I change the data, it shows, that method is called, but when i open my JTable again, it is not changed (when i open data Jtable, it's method is recalled, so there isn't problem with that).
I guess that, when the method setData(int r, int c, String new_value) is called, it changes the value, but when i make the JTable, values are taken from the first initialized array at the beggining).

public void setData(int row, int col, String value) {
        pilns[row][col] = value;
        }
    public String getData(int row, int col) {
         String toReturn = pilns[row][col];
         return toReturn;
        }

code looks sth like that. If you need sth else, write. I really hope smb can help me.
I just registered for this site, because lots of answers for others have helped me too! "this site is a keeper" LOL :D

The code in setData() looks like it will change the contents of the pilns array.
If the change is not seen somewhere else in the program, it could be because that other location in the code has a copy of the array taken before the change was made.

Edited 4 Years Ago by NormR1

thanks for reply.
actually not. when I change the JTable, i go through all the array with double for loop.
and put data in with setData();
but maybe initializing is wrong.
can i write like this: ?

String[][] pilns = {{"thing","month", "1", "2", "3"},
                    {"toy", "January", "5", "4", "4"},
                    {"", "February", "23", "3", "45"}};

and setData () {//CODE}
also getData () {//CODE}

And later in this same file i make JFrame. In that there are 3 JLabels to set data in and JButton, that calls his own actionlistener method (written in other file - from which program comes back to this one to call it's methods).
maybe this JFrame should be made in other file?

Sorry, the small bits of code your posted do not show what you are doing or what any other parts of your code are doing.

Ok. here is the method, that's having OK's (for changing the cell) action listener:

   public void changeCell(JButton it, final JTextArea rinda, final JTextArea kolonna, final JTextArea jaunaa, final JLabel zinja) {
            it.addActionListener(new ActionListener() {//meneshu sarindosh 2_krit

       public void actionPerformed(ActionEvent event) {
           try{
            boolean isInt = true;

            int r = 1;
            int k = 1;
            String val = "new value";
            masivam change = new masivam(); //masivam = file, where data array is made. and setData and getData methods are.

            try{  //checks if numeric

                r = Integer.parseInt(rinda.getText());
                k = Integer.parseInt(kolonna.getText());
                Integer.parseInt(jaunaa.getText());
                val = jaunaa.getText();
                isInt = true;
                }catch( Exception e){
                    isInt = false;
                    }
            if (isInt==false) {zinja.setText("Nop. not correct");
            }else {
                change.setData(r, k, val);
                System.out.println(r+"   "+k+ "   " + val);
                zinja.setText("Yap. data changed");
                }
           } catch (Exception che){
            System.out.print("Couldn't change value!");

            }
      };
    });
        }

If you create a new instance of the Masivam class every time (on line 11) what happens to the data in the old instance of the class?

i guess, that old data are just overwritten. but what should i do to save them somewhere? make a new method or what?

If you want to save the contents of a class across method calls, create an instance of the class one time and use that same instance in all of the methods instead of creating a new instance of the class everytime the method is called.

You mean writing masivam change = new masivam(); somewhere higher in the class, not in the exact method? in methods class, where the ChangeCell() is placed, i wrote that line and in the method just deleted line 11. is that what you thought?
Still nothing...

Edited 4 Years Ago by migelitto: got the problem wrong

What does "Still nothing..." mean?

If you put something into the array, is it still there later in the program? If you have only one copy of the array, then what you put in the array should be there any time after you do it.

Can you add printlns to the code that print out the contents of the array before the changes are made to it and then again when it is changed and then again later when you look in it for the values?

"Still nothing" was ment - when i open the table, there are still old numbers. nothing new.
I put Syst.out.prints, but it shows, that changes are made. old value - january. new value 666.
but when i'm making the table or using data from that array in other places in my program, it shows old values. i really think that i am doing it right.
hope it is some dumb mistake. :D

Can you post what is printed out that shows:
1)the before change contents
2) the contents after a change
3) the contents later when trying to get the changes

using data from that array in other places in my program, it shows old values

Are you sure that there is only ONE instance of the array?

now i made it as methods class has only one instance of the array and it uses it in all the methods.

Outprints:
right before setting new value 104
After setting new value 777
later in program 104

The code must have two copies of the array. You change one copy and read from the other copy.
Print out the name of the array at the three places where you are printing out the contents.
It should look like this: [Ljava.lang.String;@19821f
If there are two arrays, the value after the @ will be different.

You will have to make a small testing program you can post that will compile, execute and show the problem. This can't be solved without code that shows the problem.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.