Membuka Data Tabel dalam File TXT dengan Java
Suatu hari, penulis diberikan data tabel dalam bentuk file *.txt, dengan pemisah antara data satu dengan selanjutnya, menggunakan tanda koma per baris.
Berikut ini adalah contoh isi file dari data tabel tersebut:
Tanggal scan,Tanggal,Jam,PIN,Mesin 01-11-2019 06:51:12,01-11-2019,6:51:12,1010,MESIN 4 DIREKTORAT 01-11-2019 15:04:15,01-11-2019,15:04:15,1010,MESIN 4 DIREKTORAT 04-11-2019 06:48:10,04-11-2019,6:48:10,1010,MESIN 5 DIREKTORAT 04-11-2019 14:14:52,04-11-2019,14:14:52,1010,MESIN 5 DIREKTORAT 05-11-2019 06:40:17,05-11-2019,6:40:17,1010,MESIN 5 DIREKTORAT
Penulis ingin menampilkannya dalam bentuk software berbasis Java, menggunakan AlbertTable yang telah penulis buat sebelumnya.
Dengan menggunakan JFileChooser dan membacanya dengan BufferedReader, penulis dapat menampilkannya tabel sesuai gambar di atas.
Kode di bawah ini adalah kode main project OpenDataFile.
Kode project lengkapnya dapat di download pada link Google Drive penulis dibawah ini:
package opendatafile; import AlbertTable.ClipboardKeyAdapter; import AlbertTable.TModel; import AlbertTable.TableMouseAdapter; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.WindowConstants; /** * * @author Edugameapp */ public class OpenDataFile extends JFrame { private JLabel status=new JLabel("Ready"); private LoopProgressBar progressbar; private JTable tablelist; private boolean ONCALL=false; private String[] columnNames; public void setup() { setTitle("Open TXT Table"); JMenuBar menubar=new JMenuBar(); JMenu menu=new JMenu("File"); JMenuItem jm=new JMenuItem("Open"); jm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev){ openFiles(); } }); menu.add(jm); menubar.add(menu); setJMenuBar(menubar); setLayout(new GridBagLayout()); GridBagConstraints con = new GridBagConstraints(); con.fill = GridBagConstraints.BOTH; con.weightx=1; con.weighty=1; con.gridx=0; con.gridy=0; JPanel panel = new JPanel(); panel.setLayout(new GridLayout()); columnNames=new String[]{"Data"}; Object[][] Variables = new Object[1][columnNames.length]; TModel tmodel = new TModel(columnNames, Variables); tablelist = new JTable(tmodel); tablelist.setRowHeight(20); tablelist.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane cpane=new JScrollPane(tablelist,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); ClipboardKeyAdapter cbcb=new ClipboardKeyAdapter(tablelist){ public void sendLaporan(String a) { laporan(a); } }; tablelist.addKeyListener(cbcb); tablelist.addMouseListener(new TableMouseAdapter(tablelist,cbcb){ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int row = tablelist.getSelectedRow(); } } }); cpane.setPreferredSize(new Dimension(800,400)); panel.add(cpane); add(panel,con); Font f = status.getFont(); status.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); con.weighty=0; con.gridy++; panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT)); progressbar = new LoopProgressBar(); progressbar.setup(); progressbar.setPreferredSize(new Dimension(16,16)); progressbar.hidden(); panel.add(progressbar); panel.add(status); add(panel,con); setSize(new Dimension(800,400)); pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((int)(0.5*(screenSize.getWidth()-getWidth())),(int)(0.5*(screenSize.getHeight()-getHeight()))); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().requestFocusInWindow(); setVisible(true); } private void openFiles() { JFileChooser chooser = new JFileChooser(); String fileBuf=System.getProperty("user.dir"); if(fileBuf!=null){ File home=new File(fileBuf); if(home.isDirectory()){ chooser.setCurrentDirectory(home); }else{ chooser.setCurrentDirectory(home.getParentFile()); } } chooser.setDialogTitle("Select Files"); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { final File files = chooser.getSelectedFile(); if (files != null) { new Thread(new Runnable(){ public void run(){ scanfiles(files); } }).start(); }else JOptionPane.showMessageDialog(this,"Template empty"); } } private void scanfiles(File files) { progressbar.showup(); try { laporan("Opening file "+files.getName()); FileReader fw = new FileReader(files.getAbsoluteFile()); BufferedReader dos = new BufferedReader(fw); List<String> baris=new ArrayList<String>(); String line=dos.readLine(); int io=0; while(line!=null){ if(!line.isEmpty()){ if(io==0){ String[] kata=line.split(","); columnNames=new String[kata.length]; for(int i=0;i<columnNames.length;i++){ columnNames[i]=kata[i]; } }else{ baris.add(line); } io++; } line=dos.readLine(); } dos.close(); laporan("Stored to table"); Object[][] data=new Object[baris.size()][1]; for(int i=0;i<baris.size();i++){ String[] kata=baris.get(i).split(","); data[i]=new Object[columnNames.length]; for(int j=0;j<data[i].length;j++){ data[i][j]=kata[j]; } } ((TModel)tablelist.getModel()).setData(columnNames,data); ((TModel)tablelist.getModel()).updateTable(); laporan(baris.size()+" data opened!"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Could open file! : " + ex.toString()); laporan("Failed!"); } progressbar.hidden(); } private void laporan(String status) { this.status.setText(status); } public static void main(String[] args) { OpenDataFile main=new OpenDataFile(); main.setup(); } }
Kode project lengkapnya dapat di download pada link Google Drive penulis dibawah ini:
Penulis menggunakan NetBeans 8.2 dengan Java Development Kit (JDK) versi 8.
Untuk library AlbertTable dapat didownload di sini.
Untuk library AlbertTable dapat didownload di sini.
Video berikut adalah contoh tampilan dari kompilasi project di atas.
Komentar
Posting Komentar