问题
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:
- When text is entered check for the amount of characters
- Check for the width of the cell and look if the text fits
- If the text doesnt fit check if the cell next to this one has text
- 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 aJScrollPane
.
来源:https://stackoverflow.com/questions/18241691/can-you-set-the-cells-of-jtable-to-spill-over