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 mybatis.client; import java.io.Reader; import java.util.List; import javax.swing.AbstractListModel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.ListModel; import mybatis.vo.DeptVO; import mybatis.vo.EmpVO; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * * @author USER */ public class MyFrame extends javax.swing.JFrame { private Reader r; private SqlSessionFactory factory; String[] dept_ar; List<DeptVO> d_lists; String[] emp_ar; public MyFrame() { initComponents(); init(); } private void init() { try { r = Resources.getResourceAsReader("mybatis/config/config.xml"); factory = new SqlSessionFactoryBuilder().build(r); r.close(); this.setTitle("config ~~"); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); jButton1 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jList2 = new javax.swing.JList<>(); jSeparator1 = new javax.swing.JSeparator(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { jList1ValueChanged(evt); } }); jScrollPane1.setViewportView(jList1); jButton1.setText(""); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jScrollPane2.setViewportView(jList2); jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL); jLabel1.setText("?"); jLabel2.setText("??"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jButton1)) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(40, 40, 40))); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(79, 79, 79) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE) .addComponent(jScrollPane1).addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(223, Short.MAX_VALUE))); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { jScrollPane1, jScrollPane2 }); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed SqlSession ss = factory.openSession(true); d_lists = ss.selectList("dept.all"); dept_ar = new String[d_lists.size()]; //d_lists.toArray(dept_ar); int i = 0, j = 0; for (DeptVO dvo : d_lists) { List<EmpVO> e_list = dvo.getE_list(); dept_ar[i++] = dvo.getDepartment_name() + "-(" + e_list.size() + ")"; } System.out.println("lists :" + d_lists.size()); ss.close(); jList1.setModel(new AbstractListModel<String>() { @Override public int getSize() { return dept_ar.length; } @Override public String getElementAt(int index) { return dept_ar[index]; } }); }//GEN-LAST:event_jButton1ActionPerformed private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged int idx = jList1.getSelectedIndex(); //JOptionPane.showMessageDialog(this, String.valueOf(idx)); emp_ar = new String[d_lists.get(idx).getE_list().size()]; int i = 0; for (EmpVO e : d_lists.get(idx).getE_list()) { emp_ar[i++] = "(" + i + ") " + e.getFirst_name(); } jList2.setModel(new AbstractListModel<String>() { @Override public int getSize() { return emp_ar.length; } @Override public String getElementAt(int index) { return emp_ar[index]; } }); }//GEN-LAST:event_jList1ValueChanged 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(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MyFrame.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 MyFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JList<String> jList1; private javax.swing.JList<String> jList2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSeparator jSeparator1; // End of variables declaration//GEN-END:variables }