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 Ekon; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author George */ public class PanelVypisFirem extends javax.swing.JPanel { private VytvoreniFirmy vytvorFirmy; private ArrayList<String> poleNazvu; private String aktualniNazev; public PanelVypisFirem(VytvoreniFirmy vytvorFirmy) { initComponents(); this.vytvorFirmy = vytvorFirmy; this.poleNazvu = new ArrayList<>(); this.aktualniNazev = null; } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); tableFirmy = new javax.swing.JTable(); jPanel3 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); tfNazev = new javax.swing.JTextField(); tfMesto = new javax.swing.JTextField(); tfUlice = new javax.swing.JTextField(); tfPSC = new javax.swing.JTextField(); tfKontakt = new javax.swing.JTextField(); tfEmail = new javax.swing.JTextField(); tfICO = new javax.swing.JTextField(); tfDICO = new javax.swing.JTextField(); cmbKraj = new javax.swing.JComboBox(); btnUloz = new javax.swing.JButton(); btnSmaz = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); btnNahrajSeznam = new javax.swing.JButton(); btnPridej = new javax.swing.JButton(); btnUlozSeznam = new javax.swing.JButton(); btnEditace = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setLayout(new java.awt.BorderLayout()); tableFirmy.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Nazev firmy", "Msto", "Ulice", "Kraj", "PSC", "Kontakt", "Email", "ICO", "DICO" })); jScrollPane1.setViewportView(tableFirmy); add(jScrollPane1, java.awt.BorderLayout.CENTER); jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel1.setText("Nazev Firmy :"); jLabel2.setText("Mesto : "); jLabel3.setText("Ulice : "); jLabel4.setText("Kraj :"); jLabel5.setText("PSC : "); jLabel6.setText("Kontakt :"); jLabel7.setText("Email :"); jLabel8.setText("ICO :"); jLabel9.setText("DICO : "); tfMesto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tfMestoActionPerformed(evt); } }); cmbKraj.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hlavn msto Praha", "Jiho?esk kraj", "Jihomoravsk kraj", "Karlovarsk kraj", "Krlovehradeck kraj", "Libereck kraj", "Moravskoslezsk kraj", "Olomouck kraj", "Pardubick kraj", "Plzesk kraj", "Stedo?esk kraj", "steck kraj", "Vyso?ina", "Zlnsk kraj" })); btnUloz.setText("Uloz"); btnUloz.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUlozActionPerformed(evt); } }); btnSmaz.setText("Smaz"); btnSmaz.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSmazActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel9).addGap(40, 40, 40) .addComponent(tfDICO)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addComponent(jLabel8).addGap(50, 50, 50) .addComponent(tfICO)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addComponent(jLabel7).addGap(45, 45, 45) .addComponent(tfEmail)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup().addComponent(jLabel6).addGap(32, 32, 32) .addComponent(tfKontakt)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup() .addGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5).addComponent(jLabel4)) .addGap(47, 47, 47).addGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfPSC).addComponent(cmbKraj, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(jPanel2Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel3) .addGap(44, 44, 44).addComponent(tfUlice)) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel2) .addGap(37, 37, 37).addComponent(tfMesto)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(btnUloz, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSmaz, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tfNazev, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(tfNazev, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(tfMesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(tfUlice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(cmbKraj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(tfPSC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(tfKontakt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8).addComponent(tfICO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9).addComponent(tfDICO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnUloz).addComponent(btnSmaz)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); btnNahrajSeznam.setText("Nahraj"); btnNahrajSeznam.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNahrajSeznamActionPerformed(evt); } }); jPanel1.add(btnNahrajSeznam); btnPridej.setText("Vypis"); btnPridej.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPridejActionPerformed(evt); } }); jPanel1.add(btnPridej); btnUlozSeznam.setText("Uloz"); btnUlozSeznam.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUlozSeznamActionPerformed(evt); } }); jPanel1.add(btnUlozSeznam); btnEditace.setText("Editace"); btnEditace.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditaceActionPerformed(evt); } }); jPanel1.add(btnEditace); jButton1.setText("Uloz do xls"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jButton2.setText("Nahrej z xls"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel1.add(jButton2); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 34, Short.MAX_VALUE))); add(jPanel3, java.awt.BorderLayout.LINE_END); }// </editor-fold>//GEN-END:initComponents private void btnPridejActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPridejActionPerformed DefaultTableModel model = (DefaultTableModel) tableFirmy.getModel(); while (model.getRowCount() > 0) { model.removeRow(0); } Iterator it = vytvorFirmy.dejIterator(); Iterator itN = poleNazvu.iterator(); Firma f; try { for (Iterator it1 = it; it1.hasNext();) { f = (Firma) it1.next(); model.addRow(new Object[] { f.getNazevFirmy(), f.getMesto(), f.getUlice(), f.getKraj(), f.getPsc(), f.getKontakt(), f.getEmail(), f.getIco(), f.getDico() }); } } catch (NullPointerException e) { JOptionPane.showMessageDialog(this, "seznam firem je prazdny", "Chyba", 1); } }//GEN-LAST:event_btnPridejActionPerformed private void btnUlozSeznamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUlozSeznamActionPerformed ArrayList<Firma> list = new ArrayList<>(); String name = null; FileOutputStream fos; ObjectOutputStream out; JFileChooser fc = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("objekty obj", "obj"); fc.setFileFilter(filter); int resault = fc.showSaveDialog(this); try { if (resault == JFileChooser.APPROVE_OPTION) { File fileSelected = fc.getSelectedFile(); System.out.println(fileSelected.getName()); list = vytvorFirmy.getListFirem(); fos = new FileOutputStream(fileSelected.getName()); out = new ObjectOutputStream(fos); out.writeObject(list); out.close(); JOptionPane.showMessageDialog(this, "Ulozeni firem se podarilo ", "Informace", 1); } } catch (FileNotFoundException ex) { Logger.getLogger(PanelVypisFirem.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PanelVypisFirem.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnUlozSeznamActionPerformed private void tfMestoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfMestoActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tfMestoActionPerformed private void btnEditaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditaceActionPerformed DefaultTableModel model = (DefaultTableModel) tableFirmy.getModel(); if (tableFirmy.getSelectedRow() == -1) { if (tableFirmy.getRowCount() == 0) { JOptionPane.showMessageDialog(this, "tabulka je prazdna", "Informace", 1); } else { JOptionPane.showMessageDialog(this, "Zvolte firmu z tabulky", "Informace", 1); } } else { aktualniNazev = tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 0).toString(); tfNazev.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 0).toString()); tfMesto.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 1).toString()); tfUlice.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 2).toString()); tfPSC.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 4).toString()); tfKontakt.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 5).toString()); tfEmail.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 6).toString()); tfDICO.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 7).toString()); tfICO.setText(tableFirmy.getValueAt(tableFirmy.getSelectedRow(), 8).toString()); } }//GEN-LAST:event_btnEditaceActionPerformed private void btnUlozActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUlozActionPerformed Iterator it = vytvorFirmy.dejIterator(); Firma f = null; for (Iterator it1 = it; it1.hasNext();) { f = (Firma) it.next(); if (aktualniNazev.equals(f.getNazevFirmy())) { f.setNazevFirmy(tfNazev.getText()); f.setMesto(tfMesto.getText()); f.setUlice(tfUlice.getText()); f.setPsc(Integer.valueOf(tfPSC.getText())); f.setKraj(cmbKraj.getSelectedItem().toString()); f.setKontakt(tfKontakt.getText()); f.setEmail(tfEmail.getText()); f.setDico(Integer.valueOf(tfDICO.getText())); f.setIco(Integer.valueOf(tfICO.getText())); tableFirmy.setValueAt(tfNazev.getText(), tableFirmy.getSelectedRow(), 0); tableFirmy.setValueAt(tfMesto.getText(), tableFirmy.getSelectedRow(), 1); tableFirmy.setValueAt(tfUlice.getText(), tableFirmy.getSelectedRow(), 2); tableFirmy.setValueAt(cmbKraj.getSelectedItem().toString(), tableFirmy.getSelectedRow(), 2); tableFirmy.setValueAt(tfPSC.getText(), tableFirmy.getSelectedRow(), 4); tableFirmy.setValueAt(tfKontakt.getText(), tableFirmy.getSelectedRow(), 5); tableFirmy.setValueAt(tfEmail.getText(), tableFirmy.getSelectedRow(), 6); tableFirmy.setValueAt(tfICO.getText(), tableFirmy.getSelectedRow(), 7); tableFirmy.setValueAt(tfDICO.getText(), tableFirmy.getSelectedRow(), 8); } } }//GEN-LAST:event_btnUlozActionPerformed private void btnNahrajSeznamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNahrajSeznamActionPerformed ArrayList<Firma> list = new ArrayList<>(); FileInputStream fis = null; ObjectInputStream in = null; JFileChooser fch = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("objects obj", "obj"); fch.setFileFilter(filter); int returnVal = fch.showOpenDialog(this); try { if (returnVal == JFileChooser.APPROVE_OPTION) { File selectedFile = fch.getSelectedFile(); System.out.println(selectedFile.getName()); fis = new FileInputStream(selectedFile.getName()); in = new ObjectInputStream(fis); list = (ArrayList<Firma>) in.readObject(); vytvorFirmy.setListFirem(list); JOptionPane.showMessageDialog(this, "Nahravani firem se podarilo", "Informace", 1); } else if (returnVal == JFileChooser.CANCEL_OPTION) { JOptionPane.showMessageDialog(this, "Nevybrali jste soubor", "Informace", 1); } } catch (FileNotFoundException ex) { Logger.getLogger(PanelVypisFirem.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PanelVypisFirem.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(PanelVypisFirem.class.getName()).log(Level.SEVERE, null, ex); } for (int i = 0; i < list.size(); i++) { System.out.println(list.toString()); } }//GEN-LAST:event_btnNahrajSeznamActionPerformed private void btnSmazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSmazActionPerformed tfNazev.setText(null); tfMesto.setText(null); tfUlice.setText(null); tfPSC.setText(null); tfKontakt.setText(null); tfEmail.setText(null); tfDICO.setText(null); tfICO.setText(null); }//GEN-LAST:event_btnSmazActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Zamestnanci"); Map<String, Object[]> data = new TreeMap<>(); data.put("1", new Object[] { "Nazev Firmy", "Mesto", "Ulice", "Kontakt", "Email", "ICO", "DICO", "Kraj", "PSC" }); int i = 2; try { for (Iterator it = vytvorFirmy.dejIterator(); it.hasNext();) { Firma f = (Firma) it.next(); data.put(String.valueOf(i++), new Object[] { f.getNazevFirmy(), f.getMesto(), f.getUlice(), f.getKontakt(), f.getEmail(), f.getIco(), f.getDico(), f.getKraj(), f.getPsc() }); } Set<String> keySet = data.keySet(); int rowNum = 0; for (String key : keySet) { Row row = sheet.createRow(rowNum++); Object[] objArr = data.get(key); int cellNum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellNum++); if (obj instanceof String) { cell.setCellValue((String) obj); } else if (obj instanceof Integer) { cell.setCellValue((Integer) obj); } } } JFileChooser fch = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("objects xls xlsx", "xls", "xlsx"); fch.setFileFilter(filter); int returnVal = fch.showSaveDialog(this); File nazevSouboru = fch.getSelectedFile(); FileOutputStream out = new FileOutputStream(nazevSouboru.getAbsolutePath()); workbook.write(out); out.close(); JOptionPane.showMessageDialog(this, "Sobour XLS/XLSX vytvoren", "Informace", 1); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Chyba !", "Error", 1); } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed JFileChooser fch = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("objects xls xlsx", "xls", "xlsx"); fch.setFileFilter(filter); int returnVal = fch.showOpenDialog(this); try { File nazevSouboru = fch.getSelectedFile(); ArrayList<Firma> list = new ArrayList<>(); FileInputStream file = new FileInputStream(nazevSouboru.getAbsoluteFile()); //Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbookV = new XSSFWorkbook(nazevSouboru.getAbsolutePath()); //Get first/desired sheet from the workbook XSSFSheet sheetV = workbookV.getSheetAt(0); Iterator<Row> rowIT = sheetV.iterator(); while (rowIT.hasNext()) { Row row = rowIT.next(); Iterator<Cell> cellIt = row.cellIterator(); ArrayList<String> listO = new ArrayList<>(); while (cellIt.hasNext()) { Cell cell = cellIt.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: // System.out.print(cell.getNumericCellValue() + " "); listO.add(String.valueOf((int) cell.getNumericCellValue())); break; case Cell.CELL_TYPE_STRING: listO.add(cell.getStringCellValue()); //System.out.print(cell.getStringCellValue() + " "); break; } } vytvorFirmy.pridejFirmu(new Firma(String.valueOf(listO.get(0)), String.valueOf(listO.get(1)), String.valueOf(listO.get(2)), String.valueOf(listO.get(3)), String.valueOf(listO.get(4)), Integer.valueOf(listO.get(5)), Integer.valueOf(listO.get(6)), String.valueOf(listO.get(7)), Integer.valueOf(listO.get(8)))); } file.close(); DefaultTableModel model = (DefaultTableModel) tableFirmy.getModel(); while (model.getRowCount() > 0) { model.removeRow(0); } Iterator it = vytvorFirmy.dejIterator(); Iterator itN = poleNazvu.iterator(); Firma f; try { for (Iterator it1 = it; it1.hasNext();) { f = (Firma) it1.next(); model.addRow(new Object[] { f.getNazevFirmy(), f.getMesto(), f.getUlice(), f.getKraj(), f.getPsc(), f.getKontakt(), f.getEmail(), f.getIco(), f.getDico() }); } } catch (NullPointerException e) { JOptionPane.showMessageDialog(this, "seznam firem je prazdny", "Chyba", 1); } } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_jButton2ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnEditace; private javax.swing.JButton btnNahrajSeznam; private javax.swing.JButton btnPridej; private javax.swing.JButton btnSmaz; private javax.swing.JButton btnUloz; private javax.swing.JButton btnUlozSeznam; private javax.swing.JComboBox cmbKraj; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tableFirmy; private javax.swing.JTextField tfDICO; private javax.swing.JTextField tfEmail; private javax.swing.JTextField tfICO; private javax.swing.JTextField tfKontakt; private javax.swing.JTextField tfMesto; private javax.swing.JTextField tfNazev; private javax.swing.JTextField tfPSC; private javax.swing.JTextField tfUlice; // End of variables declaration//GEN-END:variables }