{"id":300,"date":"2015-09-10T17:17:36","date_gmt":"2015-09-10T07:17:36","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=300"},"modified":"2015-11-20T18:34:54","modified_gmt":"2015-11-20T07:34:54","slug":"control-decimal-places-displayed-jtable-column","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/control-decimal-places-displayed-jtable-column\/","title":{"rendered":"How to control decimal places displayed in JTable column?"},"content":{"rendered":"

Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells.
\nTo control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the parent (DefaultTableCellRenderer) class.
\nTo get the table to use that class we tell the JTable to use it for a given column.<\/p>\n

Following example illustrates what is required.<\/p>\n

\r\npackage com.learnjava.swing.table;\r\n\r\nimport java.awt.*;\r\nimport java.text.DecimalFormat;\r\nimport javax.swing.*;\r\nimport javax.swing.table.*;\r\n\r\npublic class DecimalPlacesInTable extends JFrame {\r\n\tpublic static void main( String[] args ) {\r\n\t\tDecimalPlacesInTable frame = new DecimalPlacesInTable();\r\n\t\tframe.setDefaultCloseOperation( EXIT_ON_CLOSE );\r\n\t\tframe.pack();\r\n\t\tframe.setVisible( true );\r\n\t}\r\n\r\n\tpublic DecimalPlacesInTable() {\r\n\t\tObject[] columnNames = { "A", "B", "C" };\r\n\t\tObject[][] data = {\r\n\t\t\t\t{ "abc", new Double( 850.503 ), 5 },\r\n\t\t\t\t{ "def", new Double( 36.23254 ), 6 },\r\n\t\t\t\t{ "ghi", new Double( 8.3 ), 7 },\r\n\t\t\t\t{ "jkl", new Double( 246.0943 ), 23 }};\r\n\r\n\t\tJTable table = new JTable(data, columnNames);\r\n\r\n        \/\/ Tell the table what to use to render our column of doubles\r\n\r\n\t\ttable.getColumnModel().getColumn(1).setCellRenderer(\r\n\t\t\tnew DecimalFormatRenderer() );\r\n\t\tgetContentPane().add(new JScrollPane(table));\r\n\t}\r\n\r\n        \/**\r\n         Here is our class to handle the formatting of the double values\r\n         *\/\r\n\r\n\tstatic class DecimalFormatRenderer extends DefaultTableCellRenderer {\r\n\t\tprivate static final DecimalFormat formatter = new DecimalFormat( "#.00" );\r\n\r\n\t\tpublic Component getTableCellRendererComponent(\r\n\t\t\tJTable table, Object value, boolean isSelected,\r\n\t\t\tboolean hasFocus, int row, int column) {\r\n\r\n\t\t\t\/\/ First format the cell value as required\r\n\r\n\t\t\tvalue = formatter.format((Number)value);\r\n\r\n            \/\/ And pass it on to parent class\r\n\r\n\t\t\treturn super.getTableCellRendererComponent(\r\n\t\t\t\ttable, value, isSelected, hasFocus, row, column );\r\n\t\t}\r\n\t}\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells. To control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the parent (DefaultTableCellRenderer) class. To get…<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4,15],"tags":[87,88,85,86],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-4Q","jetpack-related-posts":[{"id":251,"url":"https:\/\/learn-java-by-example.com\/java\/add-checkbox-items-jlist\/","url_meta":{"origin":300,"position":0},"title":"How do add a checkbox to items in a JList?","date":"June 30, 2015","format":false,"excerpt":"We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox's seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean's as\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":300,"position":1},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":300,"position":2},"title":"Convert Inches to Centimetres","date":"October 14, 2011","format":false,"excerpt":"The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":166,"url":"https:\/\/learn-java-by-example.com\/java\/month-calendar\/","url_meta":{"origin":300,"position":3},"title":"Month Calendar","date":"May 1, 2011","format":false,"excerpt":"The following application shows how to format displaying a month in a way that would be suitable for including in a calendar. This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column. The day that\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/learn-java-by-example.com\/wp-content\/uploads\/2011\/05\/calendar.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":300,"position":4},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":370,"url":"https:\/\/learn-java-by-example.com\/java\/iterate-dates-range\/","url_meta":{"origin":300,"position":5},"title":"How can I iterate through all dates in a range?","date":"November 23, 2013","format":false,"excerpt":"Ever wanted to iterate through a range of Date's? We can use Iterator's for looping through the elements of a Collection, but for Date's we need to implement the Date arithmetic required. The following class shows how we can create our own Iterator implementation to easily iterate over a range\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/300"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":3,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/300\/revisions\/316"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}