2

I have this kind of progamming task without JavaFx, instead it's Java Swing. I realized my knowledge is still limited.

I have one single JTable. But, within this JTable I need a custome Cell Renderer. The goal is to make this kind of JTable: Example image

My current solutions are: Example Image

  1. Create a Single JTable:
    • get each Column and set its CellRenderer with a custom Renderer (below).
  2. Create a new Class implements TableCellRenderer:
    • return different JPanel inside getTableCellRendererComponent method using switch case (as column counted).

After hours, and hours, I think my current solutions is quite daunting tasks. Thus, My question is:

What are the simplest method of creating this Custom JTable to achieve the main goal as mentioned above?

Charles
  • 48,924
  • 13
  • 96
  • 136
gumuruh
  • 2,428
  • 4
  • 30
  • 50
  • 1
    If the question is: is it possible without custom renderers? The answer is no, AFAIK. How is this a daunting task? – JB Nizet Jan 19 '12 at 08:39
  • @JBNizet, the daunting task is that I should create each different JPanel that containing different component inside of it without getting short code... Ya, I realized that I should custom the JTable of course. – gumuruh Jan 19 '12 at 09:27
  • row/column spanning is not supported generally - so yes, it is daunting :-) Increasing the granularity of the underlying table and then split (by by appropriate renderers/editor) is the hack to go in fully controlled contexts. With the additional price of having more complex data per (tableModel) cell which must be split by the renderer to per-renderer-area data. – kleopatra Jan 19 '12 at 09:59

4 Answers4

3

you have two options

1) JPanel nested another JComponents and solve that by using standard LayoutManagers note scrolling isn't natural nor nice

2) JTable with JPanel can solve that, notice about scrolling inner JScrollPane inside another JScrollPane

Community
  • 1
  • 1
mKorbel
  • 108,320
  • 17
  • 126
  • 296
  • +1 for nested panels; a related example may be seen [here](http://stackoverflow.com/a/5630271/230513). – trashgod Jan 19 '12 at 10:35
2

Once you create a nested panel for one row, as suggested by @mKorbel, you can add any number of them to a GridLayout(0, 1) in a JScrollPane. If rendering many rows becomes an issue, you can adopt the same approach used by JTable, illustrated here.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918
2

I've been facing this problem for a while, and I decided to do it myself. Extending the existing implementation of a table, adding some concepts for what I expect from a table, and writting some editors/listeners for that. All the same, but with a treetable.

I'm working on this project called SUMI.

It contains a java package (ar.com.tellapic.sumi.treetable) that is an extension of a JXTreeTable from SwingLabs.

The project is being developed and I didn't provide any documentation yet. You can do what you want by creating a renderer and if needed, an editor, for lastly attaching actions to each object.

If you decide to use it and you need help, email me, I'll help you without any problem.

Or, you could read the source by your own.

Regards,

EDITED (again):

To clear a little bit this answer, I've just created a wiki page in the project wiki and put the relevant code there. If someone feels that the code should be inserted here, please let me know.

Basically, I try to explain how to find a straight solution to the renderer/editor problems you may find using JTable with your specifics needs by using part of my project, in order to get something like this:

Note that the screenshot was taken after clicking on the respective tick-button.

Sebastian
  • 3,103
  • 2
  • 34
  • 37
  • Hey, thanks for the reply, @Sebastian.... i think you also found the similar concept from the JTable that we all used 2get troubled here... Anyway, Have u tried to put the button on that Tick-image ? I've put it there the button, but the interaction seems failed. :( – gumuruh Feb 28 '12 at 06:55
  • @gumuruh, I need to document the whole thing. Basically, for that "tick button" you mentioned, you need to create the "model" (a custom `TellapicNodeAction`) and do some rewrite of the view (the `DualImageRenderer` class). `TellapicNodeAction` can have a reference to an `AbstractAction`. You can extend `DefaultAbstractTellapicNodeAction` class to create your custom action passing an `AbstractAction` to the constructor (the super class will handle the `perfomAction()` to execute `Action.actionPerformed()` for you). Give me some time, and this afternoon I'll try to document a bit more. Regards. – Sebastian Feb 28 '12 at 14:02
  • @gumuruh, It took me too much characters to mentioned that also you need to handle/use/create the correct editor. When creating your custom `TellapicNodeAction` extending `DefaultAbstractTellapicNodeAction` you pass to the super constructor the editor and rendering keys. As I mentioned before, give time for document that. – Sebastian Feb 28 '12 at 14:07
  • @gumuruh, I wrote some lines trying to explain the underlying concept of the project and help for your question [here](https://code.google.com/p/sumi/wiki/TellapicNodeAction). Don't hesitate in asking question about it. Hope this helps. Regards – Sebastian Mar 02 '12 at 01:59
0

Even though, JTable can be customized to whatever you desire through cell renderer and cell editors, it is never preferred because you have to do a lot of messy codings for that. Instead, for your problem, I suggest to use JScrollPane and add your component (view panel as your sample jTable ) to its viewPort.

For this implementation, represent each rows with your custom class that extends JPanel. And add the required row components (that may be any components like jlabel, jtextfields or even jpanel too) in it. For the simplicity, you can use null layout for the row panel and add the components at any location you want.

I hope this will help you workout with your problem. If you got any problem in this implementation, feel free you ask again.

Manoj Shrestha
  • 3,064
  • 3
  • 36
  • 53
  • 1
    I agree with your first paragraph, but I can't endorse using a `null` layout for this. A [nested layout](http://stackoverflow.com/a/7290858/230513) seems reasonable. – trashgod Jan 19 '12 at 11:11
  • 1
    -1 for null layout (there's nothing "simple" in manually doing a LayoutManager's job) – kleopatra Jan 19 '12 at 11:24