Can `JTableHeader` span over multiple columns?

半世苍凉 提交于 2019-12-23 09:40:13

问题


I've spent quite some time searching for this and I've only found the GroupableHeader code. I need one header over 2 columns in a 2 column JTable. How can this be done without the use of the infamous GroupableHeader, while keeping the default look and feel of the `JTableHeader?


This is a graphical representations of what I have in mind:

_________________________
|     Table Header      |
|-----------------------|
|           |           |
|-----------|-----------|
|           |           |
|-----------|-----------|

回答1:


This depends on what eaxctly you want to achieve.

I did something similar by creating my own JTableHeader, which was essentially just a JPanel.

On it I placed "group" headers on the top row, over the columns I wanted to "group".

Then I cheated and used the existing tables row header and added it to the second row below the header.

This requires some slight of hand, as you need to know the columns you want to group/span and their widths. You also need to monitor for changes in their widths and update the header appropriately.

Most of the implementations I've seen use their own UI implementation, personally I don't like this as it undoes the work of the look and feel you're using.

You could have a look at this which demonstrates a number of interesting things you can do with JTables as well multi-column spanning, but again, it's using it's own UI to get it to work.

The implementation we've begin using at work works in the same way and the problem I have with it is that you can see the mouse resize cursor within the group header, which, to me, isn't really what you want the user to see



来源:https://stackoverflow.com/questions/13201590/can-jtableheader-span-over-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!