Extending JTable through codes, adding objects into the cell

Posted on 2006-03-27
Last Modified: 2012-06-27

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?

Question by:amai
    LVL 8

    Expert Comment

    LVL 14

    Accepted 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now