• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Extending JTable through codes, adding objects into the cell


I need to write a piece of code that add row of information into a table whenever the user click  button "Add person" after entering some information. E.g. User enters name, id of a person, and when click, name, id of the person gets entered into Column A and Column B in a new row.

In Column C, I then need to programatically add a checkbox for the user. When the user click on the checkbox, the whole cell will turn green, and if the checkbox is unclicked, the whole cell will turn red.

If user click on "Add Device", then the columns in the table will added by one, and all the rows in the table will get a new cell, with a checkbox added into it.

I'm not familiar with Swing, AWT. Can someone help me with how to write this code?

1 Solution
the table structure (rows and columns) is defined in two interfaces one is TableModel and the other is TableColumnModel. for your job its better to use new JTable(DefaultTableModel). DefaultTableModel is derived from TableModel which supports Vectors of cell values so you can add new rows to it later. getColumnModel() gives you the table headers enables you to add new columns to table too.
and for the checkbox. there are to interfaces called TableCellRenderer and TableCellEditor which enables you to have any thing you want to be shown in a cell and any way you like the user edit a cell interactivly.
please refer to sun's java tutorial and read the samples to learn how customize your table

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now