2

I have to do a 5 minute presentation on a project that I have to implement.

Stupid ol' me chose a project that is way too much for a 5 min presentation.

Also, I procrastinated. The teacher says it's ok but im kind "cheating" by using the Netbeans GUI builder.

I have the layout all done but no functionality.

I have a tabbed view, each with a table.

Lets use jTable1 as my example. This table has 3 columns, one for a name(string), one for a number(object??), and one that has a checkbox in it(boolean).

My question is: I want to check the checkbox in the 3rd column and then update the number column based on how many times I have checked that checkbox.

How can I do this either using the GUI builder or hand coding with events/bindings/connection/or whatever I need?

Now, let me get some things straight:

I am NOT asking anyone to do my assignment for me. I am stuck on this one part and I just need help on this single thing. I know that if answered, this thread will be helpful for somebody searching the net for the answer.

Thanks.

Cool Joe
  • 315
  • 1
  • 3
  • 11

3 Answers3

1

See How to use Tables on how to implement your CellEditor. You also have to implement an AbstractTableModel for this to save the state. This is not very easy the first time you do it. See my question How to make a JButton in a JTable cell click-able? for some hints.

But you are using checkboxes wrong. If you are going to count the clicks, a JButton is a better choice.

Community
  • 1
  • 1
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • It's a grade book of sorts. I have an attendance tab where I count the number of absents. Clicking the check for absent increases it once, clicking it again decreases it. The checkboxes are just the default for the bool type input in the DefaultTableModel. – Cool Joe Dec 13 '11 at 01:21
1

The JTable shoud use a custom table model, extending AbstractTableModel. Its setValueAt method should be implemented so that each time a new value is set in the boolean column of a given row, the value of the number column of the same row is incremented.

See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data for how to implement a table model.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1
  1. Right-click on your Checkbox.
  2. From the pop-up menu, select Events --> Action --> actionPerformed
  3. In your newly-created method, you can add the code that you want to execute. Use http://docs.oracle.com/javase/tutorial/uiswing/components/table.html to help you edit your table.

For example, if you have created your table by passing an Object[][] called data to the constructor, you can update your table with:

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

I hope this helped!

eboix
  • 5,113
  • 1
  • 27
  • 38