Swing JTable custom rendering

2019-05-29 02:31发布

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?

4条回答
Ridiculous、
2楼-- · 2019-05-29 02:53

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.

查看更多
做自己的国王
3楼-- · 2019-05-29 03:07

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

查看更多
戒情不戒烟
4楼-- · 2019-05-29 03:10

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-05-29 03:13

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.

查看更多
登录 后发表回答