0

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • No, a `TableCellRenderer` cannot span cells. What other options have you considered? – trashgod Aug 15 '13 at 00:16
  • @Tim Herold came up with a solid link, but it wasn't exactly what I was looking for. I will most likely wind up just sticking the data in the last column, applying a wrap, and let the user accept it – CChestnut Aug 15 '13 at 12:26
  • Use a JDataGrid - https://www.java.net//forum/topic/javadesktop/javadesktop-announcements/javadesktop-product-announcements/jcomponentpack-v34-released – Gilbert Le Blanc Aug 15 '13 at 16:07

2 Answers2

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.

fonZ
  • 2,428
  • 4
  • 21
  • 40
  • Thanks for the help. For this project, however, it will be easier to just stick the data in the last column, apply a wrap, and let the user deal with the ugly formatting – CChestnut Aug 15 '13 at 12:14
  • You're welcome. And you're right, that is just some fancy feature you can do without anyway. – fonZ Aug 15 '13 at 18:22
1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045