JTable: Store different data than displaying
i have a question about the JTable in Java. Im reading Time-String from a database in the international standard-format (year:month:day Hours:Minutes). Thats cool beacause of sorting my table by time.
BUT the client wants to display the time in the format day:month:year Hours:Minutes). Its no problem to display it this way, but easy sorting is not possible anymore.
Im searching for a solution to store the original data for sorting and display the new format. I know in Flex there is such a thing like data und label for a cell.
Is there anything similar in Java?
Thank you 开发者_如何学Pythonvery much!!
You can use a custom cell renderer to change how the value is displayed while keeping it in store with a sortable format:
- http://download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer
Generally date information should not be stored as a String for the reason you just gave. Different application want to format the data differently. So the data should be stored as a Date object in the model. Then you use a renderer to format the date anyway you wish.
Table Format Renderers shows you an easy way to do the formatting.
精彩评论