Jump to content

Working with JTable...

- - - - -

  • Please log in to reply
3 replies to this topic

#1
@Cody

@Cody

    Newbie

  • Members
  • Pip
  • 9 posts
Hi everyone, hopefully I'm posting this in the correct spot... I'm trying to use my clear button to clear data from the right side of my JTable, and can't figure it out. Here's what I have thus far...

    myTable.setModel(new javax.swing.table.DefaultTableModel(

         new Object [][] {

        {"Rent/Mortgage", null},

        {"Water", null},

        {"Electricity", null},

        {"Cable/Internet", null},

        {"Car", null},

        {"Groceries", null},

        {"Clothing", null},

        {"Gas", null},

        {null, null},

        {null, null}

    },

    new String [] {

        "Item", "Amount"

...

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        calculateButton.setBackground(Color.lightGray);

        ...}



I'm trying to figure out how to put 0.00 in the cells currently marked as null. (This is a calculator, trying to set the amounts to zero upon clicking my clear button.) I've tried a plethora of for loops with none of them working like I'd like. I suppose I just don't understand JTable just yet. Thanks in advance for any help.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
JTable.getModel().setValueAt(Object value, int row, int col)

That should give you what you need.

Here are the relevant doc pages:
JTable (Java Platform SE 6)
TableModel (Java Platform SE 6)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
@Cody

@Cody

    Newbie

  • Members
  • Pip
  • 9 posts
    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        myTable.editCellAt(-1,-1); //stop editing

        calculateButton.setBackground(Color.lightGray);

        for(int row = 0; row < myTable.getRowCount(); row++)

        {

            for(int col = 0; col < myTable.getColumnCount(); col++)

            {

                myTable.setValueAt(0.00,row,1);

            }

        }

    }  

It's almost doing what I wanted it to now... I wanted to set everything on the right side to 0.00$ and NOT put 0.00$ in the cells that were originally blank... (The extra lines)...

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You would have to use getValueAt() and test the value first to see if it's blank.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users