Can you set the cells of JTable to spill over?

大憨熊 提交于 2019-12-12 01:56:58

问题


I'm trying to imitate some Excel spreadsheet functionality on my JTable, specifically what I've been calling cell spillover or cell overflow. This occurs when you fill a cell with text with a wider width than the cell is set to. If there is no text in the cell to its right, the words simply keep going into the next cell and beyond.

I feel like this would be accomplished somehow with TableCellRenderer, but I do not know how. Any help would be much appreciated


回答1:


I dont know because you did not give alot of detail but a simple use case would be:

  1. When text is entered check for the amount of characters
  2. Check for the width of the cell and look if the text fits
  3. If the text doesnt fit check if the cell next to this one has text
  4. If it has no text, resize the cell to fit the text and make the one next to it smaller.

Or something like that. But i dont know if JTable allows resizing of individual cells, you should check the JTable API. http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html

And i dont know if there is not a better way, so you should consult API documentation and examples. For example: http://users.csc.calpoly.edu/~jdalbey/305/Lectures/TableCellRendererExamples.html

I hope it helps you.




回答2:


No, a TableCellRenderer cannot span cells. As alternatives, consider these:

  • TablePopupEditor, a custom TableCellEditor, can be adapted to serve as a renderer.

  • A ListSelectionListener, shown here, can be added to an adjacent component in order to display details of the selected row, perhaps using a JScrollPane.



来源:https://stackoverflow.com/questions/18241691/can-you-set-the-cells-of-jtable-to-spill-over

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