/** You may copy this example and use it for any purpose, commercial or otherwise. */ import com.ricebridge.csvman.CsvManager; import com.ricebridge.csvman.CsvSpec; import com.ricebridge.csvman.CsvManagerException; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.io.*; /** Load a CSV file into a Swing JTable. */ public class LoadJTable { private CsvManager iCsvManager = null; private CsvSpec iCsvSpec = null; private JFrame iMainFrame = null; private JTable iTable = null; private JFileChooser iFileChooser = null; private boolean iHasHeaders = true; /** Program starts here. */ public static void main( String[] pArgs ) throws Exception { LoadJTable rtm = new LoadJTable(); } /** Create JTable and display it. */ public LoadJTable() throws Exception { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); // set CSV Manager JTable options iCsvManager = new CsvManager(); iCsvSpec = iCsvManager.getCsvSpec(); iCsvSpec.setProperty( "TableModel.dataHasHeaders", iHasHeaders ); iCsvSpec.setProperty( "TableModel.editable", true ); // Swing setup code iFileChooser = new JFileChooser(); iMainFrame = new JFrame("LoadJTable"); iMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToolBar toolbar = makeToolBar(); iTable = new JTable(); JScrollPane ts = new JScrollPane( iTable ); iTable.setPreferredScrollableViewportSize(new Dimension(500, 200)); iMainFrame.getContentPane().add( toolbar, BorderLayout.NORTH); iMainFrame.getContentPane().add( ts, BorderLayout.CENTER); iMainFrame.pack(); iMainFrame.setVisible(true); } /** Create toolbar and load/save actions. */ public JToolBar makeToolBar() { JToolBar toolbar = new JToolBar(); JButton loadB = new JButton( "Load CSV File" ); JButton saveB = new JButton( "Save CSV File" ); JCheckBox headerCB = new JCheckBox( "Has Headers", iHasHeaders ); loadB.addActionListener( new AbstractAction() { public void actionPerformed( ActionEvent e ) { try { File csvF = askForCsvFile( true ); if( null != csvF ) { // use CSV Manager to load data for the JTable TableModel tm = iCsvManager.loadTableModel(csvF); iTable.setModel( tm ); } } catch( CsvManagerException ce ) { // getUserMessage returns a user friendly message displayError( ce.getUserMessage() ); } catch( Throwable t ) { t.printStackTrace(); displayError( "Internal Error: "+t.getMessage() ); } } } ); saveB.addActionListener( new AbstractAction() { public void actionPerformed( ActionEvent e ) { try { File csvF = askForCsvFile( false ); if( null != csvF ) { TableModel tm = iTable.getModel(); // use CSV Manager to load data from the JTable iCsvManager.saveTableModel( csvF, tm ); } } catch( CsvManagerException ce ) { // getUserMessage returns a user friendly message displayError( ce.getUserMessage() ); } catch( Throwable t ) { t.printStackTrace(); displayError( "Internal Error: "+t.getMessage() ); } } } ); headerCB.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent e ) { iHasHeaders = !iHasHeaders; // update CsvSpec setting iCsvSpec.setProperty( "TableModel.dataHasHeaders", iHasHeaders ); } } ); toolbar.add( loadB ); toolbar.add( saveB ); toolbar.add( headerCB ); return toolbar; } /** Get the location of the csv file to load or save. */ private File askForCsvFile( boolean pLoad ) { int result = pLoad ? iFileChooser.showOpenDialog( iMainFrame ) : iFileChooser.showSaveDialog( iMainFrame ); if( JFileChooser.APPROVE_OPTION == result ) { File file = iFileChooser.getSelectedFile(); return file; } return null; } /** Display an error message in a popup window. */ private void displayError( String pMsg ) { JOptionPane.showMessageDialog( iMainFrame, pMsg, "Error", JOptionPane.ERROR_MESSAGE ); } }