com.components.wizard.layout2media.java Source code

Java tutorial

Introduction

Here is the source code for com.components.wizard.layout2media.java

Source

/*
 * 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 com.components.wizard;

import com.components.mysqlconnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author lenovo
 */
public class layout2media extends javax.swing.JFrame {

    Connection connection = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    public layout2media() {
        initComponents();
        connection = mysqlconnect.ConnectDb();
        showtabel();
        fill();
    }

    /**
     * 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();
        media1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        pathfield = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        media2 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        media3 = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        media4 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        media1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null }, { null, null },
                        { null, null }, { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Title 1", "Title 2" }));
        jScrollPane1.setViewportView(media1);
        if (media1.getColumnModel().getColumnCount() > 0) {
            media1.getColumnModel().getColumn(0).setPreferredWidth(2);
        }

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 280, 230, 220));

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jList1);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 300, 200));

        jButton3.setText("ADD to Topleft");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 120, -1, -1));

        jButton4.setText("ADD to TopRight");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 120, -1, -1));

        pathfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pathfieldActionPerformed(evt);
            }
        });
        getContentPane().add(pathfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 70, 260, -1));

        jButton5.setText("ADD to BtmLeft");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 210, -1, -1));

        jButton6.setText("ADD to btmRight");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 210, -1, -1));

        jLabel1.setBackground(new java.awt.Color(0, 153, 153));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Screen Format");
        jLabel1.setOpaque(true);
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 100, 260, 150));

        media2.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null }, { null, null },
                        { null, null }, { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Title 1", "Title 2" }));
        jScrollPane2.setViewportView(media2);
        if (media2.getColumnModel().getColumnCount() > 0) {
            media2.getColumnModel().getColumn(0).setPreferredWidth(2);
        }

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 280, 230, 220));

        media3.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null }, { null, null },
                        { null, null }, { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Title 1", "Title 2" }));
        jScrollPane4.setViewportView(media3);
        if (media3.getColumnModel().getColumnCount() > 0) {
            media3.getColumnModel().getColumn(0).setPreferredWidth(2);
        }

        getContentPane().add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 280, 230, 220));

        media4.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null }, { null, null },
                        { null, null }, { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Title 1", "Title 2" }));
        jScrollPane5.setViewportView(media4);
        if (media4.getColumnModel().getColumnCount() > 0) {
            media4.getColumnModel().getColumn(0).setPreferredWidth(2);
        }

        getContentPane().add(jScrollPane5, new org.netbeans.lib.awtextra.AbsoluteConstraints(820, 280, 230, 220));

        jLabel2.setFont(new java.awt.Font("Trajan Pro", 3, 18)); // NOI18N
        jLabel2.setText("Whole Media List ");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 10, -1, -1));

        jLabel3.setFont(new java.awt.Font("Trajan Pro", 3, 18)); // NOI18N
        jLabel3.setText("Media Path");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 40, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void showtabel() {
        try {

            String runSQL = "select * from layout2media1";
            pst = connection.prepareStatement(runSQL);
            rs = pst.executeQuery(runSQL);
            media1.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (SQLException | NumberFormatException e) {

        }
        try {

            String runSQL = "select * from layout2media2";
            pst = connection.prepareStatement(runSQL);
            rs = pst.executeQuery(runSQL);
            media2.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (SQLException | NumberFormatException e) {

        }
        try {

            String runSQL = "select * from layout2media3";
            pst = connection.prepareStatement(runSQL);
            rs = pst.executeQuery(runSQL);
            media3.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (SQLException | NumberFormatException e) {

        }
        try {

            String runSQL = "select * from layout2media4";
            pst = connection.prepareStatement(runSQL);
            rs = pst.executeQuery(runSQL);
            media4.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (SQLException | NumberFormatException e) {

        }

    }

    private void fill() {
        DefaultListModel m = new DefaultListModel();
        try {
            String sql = "select * from media";
            pst = connection.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                String path = rs.getString("name");
                m.addElement(path);
            }
            jList1.setModel(m);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        } finally {
            try {
                rs.close();
                pst.close();
            } catch (Exception e) {
            }
        }

    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String mediaID = "SELECT COUNT(*) FROM layout2media1;";
        int rowCount = 0;
        try {
            pst = connection.prepareStatement(mediaID);
            rs = pst.executeQuery();
            if (rs.next()) {
                rowCount = rs.getInt(1);
            }
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        ++rowCount;
        try {

            String runSQL = "insert into layout2media1 (id1,media1) values (?,?)";
            pst = connection.prepareStatement(runSQL);
            pst.setString(1, Integer.toString(rowCount));
            pst.setString(2, pathfield.getText());
            pst.execute();
            JOptionPane.showMessageDialog(null, "media saved");
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }

        showtabel();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        String tmp = (String) jList1.getSelectedValue();
        String sql = "select * from media where name=?";
        try {
            pst = connection.prepareStatement(sql);
            pst.setString(1, tmp);
            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("path");
                pathfield.setText(add1);

            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jList1MouseClicked

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String mediaID = "SELECT COUNT(*) FROM layout2media2;";
        int rowCount = 0;
        try {
            pst = connection.prepareStatement(mediaID);
            rs = pst.executeQuery();
            if (rs.next()) {
                rowCount = rs.getInt(1);
            }
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        ++rowCount;
        try {

            String runSQL = "insert into layout2media2 (id1,media2) values (?,?)";
            pst = connection.prepareStatement(runSQL);
            pst.setString(1, Integer.toString(rowCount));
            pst.setString(2, pathfield.getText());
            pst.execute();
            JOptionPane.showMessageDialog(null, "media saved");
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }

        showtabel();

    }//GEN-LAST:event_jButton4ActionPerformed

    private void pathfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathfieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pathfieldActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        String mediaID = "SELECT COUNT(*) FROM layout2media3;";
        int rowCount = 0;
        try {
            pst = connection.prepareStatement(mediaID);
            rs = pst.executeQuery();
            if (rs.next()) {
                rowCount = rs.getInt(1);
            }
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        ++rowCount;
        try {

            String runSQL = "insert into layout2media3 (id1,media3) values (?,?)";
            pst = connection.prepareStatement(runSQL);
            pst.setString(1, Integer.toString(rowCount));
            pst.setString(2, pathfield.getText());
            pst.execute();
            JOptionPane.showMessageDialog(null, "media saved");
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }

        showtabel();

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        String mediaID = "SELECT COUNT(*) FROM layout2media4;";
        int rowCount = 0;
        try {
            pst = connection.prepareStatement(mediaID);
            rs = pst.executeQuery();
            if (rs.next()) {
                rowCount = rs.getInt(1);
            }
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        ++rowCount;
        try {

            String runSQL = "insert into layout2media4 (id1,media4) values (?,?)";
            pst = connection.prepareStatement(runSQL);
            pst.setString(1, Integer.toString(rowCount));
            pst.setString(2, pathfield.getText());
            pst.execute();
            JOptionPane.showMessageDialog(null, "media saved");
        } catch (SQLException | NumberFormatException e) {
            JOptionPane.showMessageDialog(null, e);
        }

        showtabel();

    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(layout2media.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(layout2media.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(layout2media.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(layout2media.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new layout2media().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable media1;
    private javax.swing.JTable media2;
    private javax.swing.JTable media3;
    private javax.swing.JTable media4;
    private javax.swing.JTextField pathfield;
    // End of variables declaration//GEN-END:variables
}