Advertisement
Guest User

Java JTable setCellRenderer

a guest
Mar 5th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1.     import java.awt.*;
  2.     import java.text.DecimalFormat;
  3.     import java.text.DecimalFormatSymbols;
  4.     import javax.swing.*;
  5.     import javax.swing.table.DefaultTableCellRenderer;
  6.    
  7.     public class SimpleTableExample extends JFrame {
  8.         private final JPanel topPanel;
  9.         private final JTable table;
  10.         private final JScrollPane scrollPane;
  11.        
  12.         public SimpleTableExample() {
  13.             setTitle( "Simple Table Application" );
  14.             setSize( 300, 200 );
  15.             setBackground( Color.gray );
  16.             setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
  17.            
  18.             topPanel = new JPanel();
  19.             topPanel.setLayout( new BorderLayout() );
  20.             getContentPane().add( topPanel );
  21.            
  22.            
  23.             table = new JTable();
  24.             table.setModel(new javax.swing.table.DefaultTableModel(
  25.                 new Object [][] {{"IT01", "Shoes",  new Double(3233233.87)}, {"IT02", "Hammer",  new Double(321233.87)}},
  26.                 new String [] { "ID", "ITEM", "PRICE" }
  27.             ) {
  28.             Class[] types = new Class [] {
  29.                 java.lang.String.class, java.lang.String.class, java.lang.Double.class
  30.             };
  31.             public Class getColumnClass(int columnIndex) {
  32.                 return types [columnIndex];
  33.             }
  34.         });
  35.        
  36.         table.getColumnModel().getColumn(2).setCellRenderer(new DecimalFormatRenderer() );
  37.        
  38.         scrollPane = new JScrollPane(table);
  39.         topPanel.add(scrollPane, BorderLayout.CENTER);
  40.     }
  41.    
  42.     public static void main( String args[] ) {
  43.         SimpleTableExample mainFrame = new SimpleTableExample();
  44.         mainFrame.setVisible( true );
  45.     }
  46.    
  47.     static class DecimalFormatRenderer extends DefaultTableCellRenderer {
  48.         private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols() ;
  49.         private static final DecimalFormat dfCurrency = new DecimalFormat("#,###.##");
  50.        
  51.         public DecimalFormatRenderer() {
  52.             super();
  53.            
  54.             dfs.setDecimalSeparator(',');
  55.             dfs.setGroupingSeparator('.');
  56.             dfCurrency.setDecimalFormatSymbols(dfs);
  57.         }
  58.        
  59.         public Component getTableCellRendererComponent(
  60.             JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  61.                 // First format the cell value as required
  62.                
  63.                 value = dfCurrency.format((Number)value);
  64.                
  65.                 // And pass it on to parent class
  66.            
  67.                 return super.getTableCellRendererComponent(
  68.                     table, value, isSelected, hasFocus, row, column );
  69.             }
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement