jtable change values problem

Discussion in 'Java' started by Jacques Chaurette, Oct 14, 2005.

  1. Hello All, and thanks for any help.

    I have a jTable that is populated with an array of strings. During the
    course of the program these values get changed depending on the selection of
    a jcombobox. What I have noticed is that I am unable to set the initial
    values back in the table. I can set the values to any other of my data
    string arrays except the initial ones. I am using the setValue method of the
    table model. Here is the jtable tablemodel constructor:

    class MyTableModel extends AbstractTableModel {
    String[] columnNames = {"Nom. dia.","Dia. (in)",
    "Instal. Cost",
    };
    Object [][] data = pipeschedlight;


    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    return data[row][col];
    }

    /*
    * JTable uses this method to determine the default renderer/
    * editor for each cell. If we didn't implement this method,
    * then the last column would contain text ("true"/"false"),
    * rather than a check box.
    */
    public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
    }

    /*
    * Don't need to implement this method unless your table's
    * editable.
    */
    public boolean isCellEditable(int row, int col) {
    //Note that the data/cell address is constant,
    //no matter where the cell appears onscreen.
    //if (col < 2) {
    //return false;
    //} else {
    return true;
    //}
    }

    /*
    * Don't need to implement this method unless your table's
    * data can change.
    */
    public void setValueAt(Object value, int row, int col) {
    if (DEBUG) {
    System.out.println("Setting value at " + row + "," + col
    + " to " + value
    + " (an instance of "
    + value.getClass() + ")");
    }

    data[row][col] = value;
    fireTableCellUpdated(row, col);

    if (DEBUG) {
    System.out.println("New value of data:");
    printDebugData();
    }
    }

    private void printDebugData() {
    int numRows = getRowCount();
    int numCols = getColumnCount();

    for (int i=0; i < numRows; i++) {
    System.out.print(" row " + i + ":");
    for (int j=0; j < numCols; j++) {
    System.out.print(" " + data[j]);
    }
    System.out.println();
    }
    System.out.println("--------------------------");
    }
    }

    Cheers,

    Jacques
     
    Jacques Chaurette, Oct 14, 2005
    #1
    1. Advertising

  2. Jacques Chaurette

    Roedy Green Guest

    On Thu, 13 Oct 2005 21:42:14 -0400, "Jacques Chaurette"
    <> wrote or quoted :

    >I have a jTable that is populated with an array of strings. During the
    >course of the program these values get changed depending on the selection of
    >a jcombobox. What I have noticed is that I am unable to set the initial
    >values back in the table. I can set the values to any other of my data
    >string arrays except the initial ones.


    What happens when you try?

    1. you get an exception? What is it?

    2. the model itself does not change?

    3. the model does not repaint?

    Are you saying that

    model.setValueAt( "begin", 0,0 ); displays "begin"
    model.setValueAt( "different", 0,0,) displays "different"
    but
    model.setValueAt( "begin",0,0); does something odd?

    This line looks a little fishy:
    Object [][] data = pipeschedlight;

    are you sure that is not just a null?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 14, 2005
    #2
    1. Advertising

  3. Hi Roedy, there is no exception, the model will not repaint the initial
    values on the displayed jtable grid after a change of values. After the
    initial values are entered I make a change with

    setValueAt( "different", 0,0,) then when I return to

    setValueAt( "initial", 0,0,) "different" is still displayed.

    Object [][] data = pipeschedlight; is the initial set of values for
    the table.

    Jacques






    "Roedy Green" <> wrote in
    message news:...
    > On Thu, 13 Oct 2005 21:42:14 -0400, "Jacques Chaurette"
    > <> wrote or quoted :
    >
    >>I have a jTable that is populated with an array of strings. During the
    >>course of the program these values get changed depending on the selection
    >>of
    >>a jcombobox. What I have noticed is that I am unable to set the initial
    >>values back in the table. I can set the values to any other of my data
    >>string arrays except the initial ones.

    >
    > What happens when you try?
    >
    > 1. you get an exception? What is it?
    >
    > 2. the model itself does not change?
    >
    > 3. the model does not repaint?
    >
    > Are you saying that
    >
    > model.setValueAt( "begin", 0,0 ); displays "begin"
    > model.setValueAt( "different", 0,0,) displays "different"
    > but
    > model.setValueAt( "begin",0,0); does something odd?
    >
    > This line looks a little fishy:
    > Object [][] data = pipeschedlight;
    >
    > are you sure that is not just a null?
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
     
    Jacques Chaurette, Oct 14, 2005
    #3
  4. Jacques Chaurette

    Roedy Green Guest

    On Thu, 13 Oct 2005 22:52:33 -0400, "Jacques Chaurette"
    <> wrote or quoted :

    >Hi Roedy, there is no exception, the model will not repaint the initial
    >values on the displayed jtable grid after a change of values. After the
    >initial values are entered I make a change with
    >
    >setValueAt( "different", 0,0,) then when I return to
    >
    >setValueAt( "initial", 0,0,) "different" is still displayed.


    Let's look at your code for setValueAt.

    I suspect this won't work either:
    setValueAt( "a", 0,0);
    setValueAt( "b", 0,0);
    setValueAt( "c",0,0);

    if it does but
    setValueAt( "a", 0,0);
    does not work,
    I suspect you may have some filter in there to toss bad value and your
    initial bad value snuck in via pipeschedlight.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 14, 2005
    #4
  5. Nope, I have tried mixing up a, b, or c and always the initial values cannot
    be re-entered.

    Jacques

    "Roedy Green" <> wrote in
    message news:...
    > On Thu, 13 Oct 2005 22:52:33 -0400, "Jacques Chaurette"
    > <> wrote or quoted :
    >
    >>Hi Roedy, there is no exception, the model will not repaint the initial
    >>values on the displayed jtable grid after a change of values. After the
    >>initial values are entered I make a change with
    >>
    >>setValueAt( "different", 0,0,) then when I return to
    >>
    >>setValueAt( "initial", 0,0,) "different" is still displayed.

    >
    > Let's look at your code for setValueAt.
    >
    > I suspect this won't work either:
    > setValueAt( "a", 0,0);
    > setValueAt( "b", 0,0);
    > setValueAt( "c",0,0);
    >
    > if it does but
    > setValueAt( "a", 0,0);
    > does not work,
    > I suspect you may have some filter in there to toss bad value and your
    > initial bad value snuck in via pipeschedlight.
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming contracts.
     
    Jacques Chaurette, Oct 14, 2005
    #5
  6. OK, there`s probably a better solution but I found a workaround.

    I initialize the jTable grid with values that I will never use or call again
    and this solves the problem.

    Jacques

    "Jacques Chaurette" <> wrote in message
    news:pPL3f.6213$...
    > Nope, I have tried mixing up a, b, or c and always the initial values
    > cannot be re-entered.
    >
    > Jacques
    >
    > "Roedy Green" <> wrote in
    > message news:...
    >> On Thu, 13 Oct 2005 22:52:33 -0400, "Jacques Chaurette"
    >> <> wrote or quoted :
    >>
    >>>Hi Roedy, there is no exception, the model will not repaint the initial
    >>>values on the displayed jtable grid after a change of values. After the
    >>>initial values are entered I make a change with
    >>>
    >>>setValueAt( "different", 0,0,) then when I return to
    >>>
    >>>setValueAt( "initial", 0,0,) "different" is still displayed.

    >>
    >> Let's look at your code for setValueAt.
    >>
    >> I suspect this won't work either:
    >> setValueAt( "a", 0,0);
    >> setValueAt( "b", 0,0);
    >> setValueAt( "c",0,0);
    >>
    >> if it does but
    >> setValueAt( "a", 0,0);
    >> does not work,
    >> I suspect you may have some filter in there to toss bad value and your
    >> initial bad value snuck in via pipeschedlight.
    >> --
    >> Canadian Mind Products, Roedy Green.
    >> http://mindprod.com Again taking new Java programming contracts.

    >
    >
     
    Jacques Chaurette, Oct 14, 2005
    #6
  7. Jacques Chaurette wrote:
    >
    > Object [][] data = pipeschedlight;


    You don't define pipeschedlight anywhere, so this isn't the complete source.

    > public Class getColumnClass(int c) {
    > return getValueAt(0, c).getClass();
    > }


    Much better to not depend on the data. In general, there may be no rows,
    or there may be nulls or the classes may use polymorphism. Better to be
    explicit.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Oct 14, 2005
    #7
  8. Jacques Chaurette

    Vova Reznik Guest

    Thomas Hawtin wrote:
    > Jacques Chaurette wrote:
    >
    >>
    >> Object [][] data = pipeschedlight;

    >
    >
    > You don't define pipeschedlight anywhere, so this isn't the complete
    > source.
    >
    >> public Class getColumnClass(int c) {
    >> return getValueAt(0, c).getClass();
    >> }

    >
    >
    > Much better to not depend on the data. In general, there may be no rows,
    > or there may be nulls or the classes may use polymorphism. Better to be
    > explicit.
    >
    > Tom Hawtin


    If model has no records then model won't check class.
    But any way, better to have:
    Object o = getValueAt(0, columnIndex);
    return o == null ? Object.class : o.getClass();
     
    Vova Reznik, Oct 14, 2005
    #8
  9. Jacques Chaurette

    zero Guest

    "Jacques Chaurette" <> wrote in
    news:Y1E3f.7749$:

    > Hello All, and thanks for any help.
    >
    > I have a jTable that is populated with an array of strings. During the
    > course of the program these values get changed depending on the
    > selection of a jcombobox. What I have noticed is that I am unable to
    > set the initial values back in the table. I can set the values to any
    > other of my data string arrays except the initial ones. I am using the
    > setValue method of the table model. Here is the jtable tablemodel
    > constructor:
    >


    <snipped>

    When you try to reset the original data, do the debug lines output the
    correct info, something incorrect, or nothing at all?

    Some general remarks:

    - class MyTableModel extends AbstractTableModel
    it is better to extend DefaultTableModel and just override what needs to be
    different. Oh, and try to come up with a more original name for the class
    ;-)

    - Object [][] data = pipeschedlight;
    like Roedy Green I am very suspicious of this line. DefaultTableModel
    includes a protected Vector dataVector to store the data, wich will be more
    efficient. You can then use a constructor to set the data you want, eg
    MyTableModel(String[][] data). Don't forget to provide a no-argument
    constructor as well.
     
    zero, Oct 14, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic
    Replies:
    1
    Views:
    425
    Tor Iver Wilhelmsen
    Aug 27, 2003
  2. Ike
    Replies:
    1
    Views:
    5,131
  3. Tivo Escobar
    Replies:
    1
    Views:
    7,411
    manusa
    Apr 12, 2007
  4. Tintin92
    Replies:
    1
    Views:
    1,749
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    349
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page