Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package regression; import java.awt.Font; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.DecimalFormat; import java.util.Iterator; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.jfree.data.xy.XYDataset; import style.FButton; import style.FTable; /** * * @author Fikri-Falndi */ public class home extends javax.swing.JFrame { /** * Creates new form home */ public home() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButton1 = new FButton("Open Data"); jButton2 = new FButton("New Data"); jButton3 = new FButton("Exit"); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("IHSG Predictor"); setPreferredSize(new java.awt.Dimension(510, 400)); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.setLayout(null); jButton1.setText("Open Data"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton1.setBounds(15, 70, 200, 40); jButton2.setText("New Data"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2); jButton2.setBounds(15, 120, 200, 40); jButton3.setText("Exit"); jPanel1.add(jButton3); jButton3.setBounds(15, 170, 200, 40); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jTextArea1.setEditable(false); jTextArea1.setBackground(new java.awt.Color(42, 42, 42)); jTextArea1.setColumns(20); jTextArea1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N jTextArea1.setForeground(new java.awt.Color(255, 255, 255)); jTextArea1.setRows(5); jTextArea1.setText( "Cara Penggunaan :\n\nOpen Data ->\nJika anda telah memiliki data\nyang akan dianalisis. data harus\nberekstensi .xls\n\nNew Data ->\nJika anda ingin memasukkan data\nbaru untuk dianalisis\n\nExit ->\nUntuk keluar dari Aplikasi"); jTextArea1.setCaretColor(new java.awt.Color(255, 255, 255)); jScrollPane1.setViewportView(jTextArea1); jPanel1.add(jScrollPane1); jScrollPane1.setBounds(230, 70, 250, 270); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("IHSG Predictor"); jPanel1.add(jLabel1); jLabel1.setBounds(180, 10, 140, 40); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: new _dataImport().show(); dispose(); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: di.setVisible(false); openData(); }//GEN-LAST:event_jButton1ActionPerformed public void openData() { JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try { FileInputStream file = new FileInputStream(selectedFile.getPath()); HSSFWorkbook workbook = new HSSFWorkbook(file); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIteratorCount = sheet.iterator(); Iterator<Row> rowIteratorValue = sheet.iterator(); int rowCount = 0, colCount = 0, rowValue = 0, colValue = 0; while (rowIteratorCount.hasNext()) { Row row = rowIteratorCount.next(); Iterator<Cell> cellIteratorCount = row.cellIterator(); colCount = 0; while (cellIteratorCount.hasNext()) { Cell cell = cellIteratorCount.next(); colCount++; } rowCount++; } di.header = new String[colCount]; di.dataValue = new String[rowCount - 1][colCount]; while (rowIteratorValue.hasNext()) { Row row = rowIteratorValue.next(); Iterator<Cell> cellIteratorValue = row.cellIterator(); colValue = 0; while (cellIteratorValue.hasNext()) { Cell cell = cellIteratorValue.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: if (rowValue == 0) { di.header[colValue] = String.valueOf(cell.getBooleanCellValue()); } else { di.dataValue[rowValue - 1][colValue] = String.valueOf(cell.getBooleanCellValue()); } break; case Cell.CELL_TYPE_NUMERIC: if (rowValue == 0) { di.header[colValue] = String.valueOf(cell.getNumericCellValue()); } else { double cellVal = cell.getNumericCellValue(); String dt = null; if (cellVal % 1 != 0) { dt = new DecimalFormat("#0.00##").format(cell.getNumericCellValue()); di.dataValue[rowValue - 1][colValue] = dt; } else { dt = new DecimalFormat("#0").format(cell.getNumericCellValue()); di.dataValue[rowValue - 1][colValue] = dt; } } break; case Cell.CELL_TYPE_STRING: if (rowValue == 0) { di.header[colValue] = String.valueOf(cell.getStringCellValue()); } else { di.dataValue[rowValue - 1][colValue] = String.valueOf(cell.getStringCellValue()); } break; } colValue++; } rowValue++; } DefaultTableModel model = new DefaultTableModel(di.dataValue, di.header); di.jTable1.setModel(model); di.jTable1.getTableHeader().setFont(new Font("Tahoma", Font.PLAIN, 16)); for (int c = 0; c < di.header.length; c++) { di.jTable1.getColumnModel().getColumn(c).setCellRenderer(new FTable()); } file.close(); di.setVisible(true); dispose(); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(fileChooser, "File Not Found"); } catch (IOException ex) { JOptionPane.showMessageDialog(fileChooser, "Ekstensi File yang Anda masukkan salah"); } } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new home().setVisible(true); } }); } _dataImport di = new _dataImport(); // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables }