Search code examples
javastringcell

Shorten a string in JTabel when cell is too small


When a cell is too small to display the whole string, it shortens the end by adding '...'. For instance: "This is a string" becomes "This is..." Is there a way to shorten it at the beginning? "...a string".

Thx!


Solution

  • JTable by default uses a JLabel as its rendering component, so basically you need to tell that label how to shorten your string. You can do that by implementing a custom TableCellRenderer. Perhaps you can use the Left Dot Renderer as a starting point and extend it, if it does not exactly suits your needs.