Creating a table

I would like to create a table of data in my program with a Swing GUI, that would look something like this:
Though there don't need to be spaces between the cells.

I thought of using a JTable and a JTableHeader, but 1) I don't know how to put an image into a cell of a JTable, and 2) according to the example in this tutorial, a JTable creates a table that allows some user interaction (kind of like a non-editable excel table - it allows row selecting and similar). I don't need any of that, I just want a simple data representation in the form of a table.

Is it possible to do that with a JTable or is there another class that I can use?

Re: Creating a table

Originally Posted by Mate de Vita

I would like to create a table of data in my program with a Swing GUI, that would look something like this:
Though there don't need to be spaces between the cells.

I thought of using a JTable and a JTableHeader, but 1) I don't know how to put an image into a cell of a JTable, and 2) according to the example in this tutorial, a JTable creates a table that allows some user interaction (kind of like a non-editable excel table - it allows row selecting and similar). I don't need any of that, I just want a simple data representation in the form of a table.

Is it possible to do that with a JTable or is there another class that I can use?

In order to ensure that the table becomes able to accept data types like ImageIcon, you are required to do some extensions.
It becomes easy to manipulate a JTable if you had created a DefaultTableModel, then passed that to the table in initialization.
The DefaultTableModel is what you extend to ensure ImageIcon typed data add-ability.

You lament that you want a table with simple data representation, but it is somewhat unavoidable that you will have to dive into non-straightforward
code to generate tables that allow the addition of images to them. But this is easy, so just observe.

You would then pass the table to a JScrollPane, so you may scroll through items that exceed the default table height. I WILL INVEST MY KNOWLEDGE AND TIME in guiding you through this with code below:

Guideline:

IMPORTS

PHP Code:

import javax.swing.ImageIcon; //if you want images in your table, you must import this shit.
import javax.swing.JTable; //your table
import javax.swing.table.DefaultTableModel; //your table model. This makes it very easy to add objects to table
import javax.swing.JScrollPane; //your table container that will allow you to scroll though table items if many items are added

Also note that your renderer implementation is lacking in that it doesn't set the text to null for a header cell that contains an Icon, and doesn't set the Icon to null for a column that should show only text. It doesn't respect the foreground color of the table header, and a plain JLabel doesn't look much like a typical table header cell.

Re: Creating a table

Originally Posted by KevinWorkman

I would be pretty cautious of his code, to be perfectly honest. But yes, you can return a Class from a method. I still think using a renderer is the way to go.

What I'm doing there is extending the table model there. I have written a complex table class whose table system is setup just as the code I typed you above...and it works flawlessly.
It is a very robust way to establish and define your table.

Re: Creating a table

Originally Posted by DarrylBurke

Also note that your renderer implementation is lacking in that it doesn't set the text to null for a header cell that contains an Icon, and doesn't set the Icon to null for a column that should show only text. It doesn't respect the foreground color of the table header, and a plain JLabel doesn't look much like a typical table header cell.

Re: Creating a table

1. I would assign a TableColumnModel variable* the value returned from table.getTableHeader().getColumnModel() and use that variable for the next 3 lines. Less clutter, more readable.

2. The renderer text is already set by the call to the super implementation. You only need to set the icon to null in the 'else' condition. Also, for rendering the String representation of an Object type parameter, it's always more flexible to use toString() rather than casting to String.

Re: Creating a table

OK, thank you both. I'll be back later with more questions.
Right now, I'm going to go through all those tutorials, which I should and would have done in the first place, if I'd known this program would require virtually all the basics of Java.

Re: Creating a table

In the program that I've posted here, definitely not. But this was only a test program for the JTable, since I wasn't able to test it in the real program, as I'm having a few problems with getting it to even show up at all in the real thing. But like I said, that's a subject for another time :)