Displaying a jTable inside another jTable // JTable cellRenderer 11


Java is one of the greatest object oriented languages. This can easily be seen when using swing components. JTables are a great example. Briefly, jTables are just a base to display a matrix of JComponents. So inside a JTable you can embed any object which extends the JComponent class.
Today I’ll show you an easy way to display a JTable inside another jTable as a cellRenderer. If you’ve ever worked with multivalued data models this can be of great help. In this simple example I’ll create a small contact manager where the name, phones, and e-mail addresses of the contact can be stored. If the contact has more than one e-mail address or phone number, then they’ll be displayed using an embedded jTable.

JTable inside JTable cell

In the above image you can see the result. The code will only allow you to display the data, but you won’t be able to edit. If you want to edit the data you must also create a table cell editor. I’ll analyze this in further posts.

Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+Pin on PinterestEmail this to someone

Leave a comment

Your email address will not be published. Required fields are marked *

11 thoughts on “Displaying a jTable inside another jTable // JTable cellRenderer

  • Chris

    Hello — any chance you’ll be updating this any time soon? I’m using the code, and it works, but I’d like to be able to find out which row was clicked on the inner (embedded) table.

    Also, I sent you email, and it bounced with a “Message could not be delivered to the domain – marcnuri.com.” message.

  • Marc Nuri

    The problem with this is that what you click when clicking the embedded table is just the visible representation of your data. To be able to manage this data you’ll have to create your own TableCellEditor just as we created the TableCellRenderer. When I get some spare time maybe I’ll do a tutorial.

    For the time being you can try adapting this code Creating a Custom Table Cell Editor in a JTable Component

    About the E-Mail bouncing, maybe the server was down when you tried.

  • Andrea

    Hello,

    thanks for your code, it was really useful to me but I have some problems when I display the table, if I go to other window then when I return, the table and also the menu and other components that I have in the same window where is the table disappear, you know why its happening this??

    Thanks a lot.

  • Kohlerfc

    Hi Thanks for the post, it helped me go in the right direction. Just a note could you please format your code as it is very difficult to read.

  • Tarun

    Nice description.

    I have question.Can we add Combox in Jtable header e.g. if we select any action in combo box then it affects whole table (e.g. table bacground color) ?

  • Sakret

    Hi, I have a problem, I copy this code and paste it in my java class (main class) but when I run it it only says “BUILD SUCCESSFUL (total time: 1 second)” maybe because of this

    public static void main(String[] args) {

    }

    what should I do? Thanks!

  • Amit

    Hi Marc,

    I am trying to use it to make nested tables n levels deep but unable to select the cell present in inner table. Could you please help by suggesting the way forward?

  • Rubens Pereira

    Olá como faco pra mudar a cor de uma célula do jTable quando a coluna de status de um cliente for pago ou a receber

    Uso o jtable do Netbeans

  • Karoline

    There was an issue on rhel4 where if a tracing preoscs didn’t call ptrace detach that the thread group leader would stay in traced state. If someone sent it a SIGCONT the thread group leader would continue and all the child threads would go into a traced state (T in ps). A second SIGCONT would bring it back to normal. Between the two signals a ps ax would show only the thread group leader status. Be sure to check the threads in ps to make sure none of them are in a traced state.