2016-04-09 9 views
0

私はMVCパターンを使って最初のJAVA Swingアプリケーションを構築しようとしています。しかし、私はそれをかなり把握していません。特にそれをコード化する方法ではありません。私はこのサイトやMVCに関する他のサイトで多くを読んでいます。 これは私がMVCについて読んものの一つです: The MVC pattern and SWINGMVCパターンに従おうとしています

http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm

私が開発にかなり新しいですし、ベストプラクティスを使用して、最初から始めたいです。私はMVCパターンについて理解考える何

あなたはコントローラモデルビューを持っています。 コントローラは、ビューから入力を取得します。次にコントローラモデルに状態を変更するように依頼します。 のモデルから状態を取得し、データベースに格納するか、のビューをデータベースから取得したものに変更するなど、入力と関連付ける必要があります。

たとえば、「アドレスの追加」ボタンをクリックすると、ビューのテキストフィールドから入力が取得されます。 コントローラモデルを更新し、得られたデータをビューからデータベースに保存します。 「検索Addres」ボタンをクリックすると、のテキストフィールドから入力が得られます。表示はデータベースを検索し、データが戻ったらモデルを更新し、ビューに出力します。

私が知りたいと思うものがMVCの場合は、どうすればよいでしょうか?

これまでのコードを使って説明することはできますか?

どのように私はコントローラビューからデータを取得することができますし、モデルの上?

これは、これまでの私のコードです:

ビューJFrameの

public class WolffAddressManagerMVCJFrameVIEW extends javax.swing.JFrame { 
    /** 
    * Creates new form StoringAdresses1JFrame 
    */ 
    public WolffAddressManagerMVCJFrameVIEW() { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 

    private void initComponents() { 

     jPanel1 = 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(); 
     firstName = new javax.swing.JTextField(); 
     lastName = new javax.swing.JTextField(); 
     emailAdress = new javax.swing.JTextField(); 
     searchEmailAdress = new javax.swing.JTextField(); 
     removeEmailAddress = new javax.swing.JTextField(); 
     street = new javax.swing.JTextField(); 
     number = new javax.swing.JTextField(); 
     additive = new javax.swing.JTextField(); 
     zipCode = new javax.swing.JTextField(); 
     city = new javax.swing.JTextField(); 
     addAdress = new javax.swing.JButton(); 
     removeAdress = new javax.swing.JButton(); 
     searchAdress = new javax.swing.JButton(); 
     jPanel2 = new javax.swing.JPanel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     showInputOutput = new javax.swing.JTextArea(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Address Input Fields", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14))); // NOI18N 

     jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel1.setText("First Name:"); 

     jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel2.setText("Last Name:"); 

     jLabel3.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel3.setText("Email Adress:"); 

     jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel4.setText("Street/Number/Add"); 

     jLabel5.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel5.setText("Zipcode/City"); 

     jLabel6.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel6.setText("Email Address:"); 

     jLabel7.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 
     jLabel7.setText("Email Address:"); 

     firstName.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     lastName.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     emailAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     street.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     number.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     additive.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     zipCode.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     city.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N 

     addAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N 
     addAdress.setText("Add Address"); 
     ActionListener actionListener = new MainWindowActionListenerMVCVIEW(firstName, lastName, emailAdress, street, number, additive, zipCode, city, searchEmailAdress, removeEmailAddress); 
     addAdress.addActionListener(actionListener); 

     removeAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N 
     removeAdress.setText("Remove Address"); 
     removeAdress.setBackground(Color.RED); 
     removeAdress.addActionListener(actionListener); 

     searchAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N 
     searchAdress.setText("Search Address"); 
     searchAdress.addActionListener(actionListener); 


     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(firstName) 
          .addComponent(lastName) 
          .addComponent(emailAdress) 
          .addComponent(searchEmailAdress) 
          .addComponent(removeEmailAddress) 
          .addGroup(jPanel1Layout.createSequentialGroup() 
           .addComponent(street, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addComponent(number, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE) 
           .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
           .addComponent(additive, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))) 
         .addGap(8, 8, 8)) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(city) 
         .addContainerGap()))) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(addAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(removeAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(searchAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addContainerGap()) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addGap(2, 2, 2) 
         .addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(emailAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(street, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(additive, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(41, 41, 41) 
       .addComponent(addAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 

       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(removeEmailAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addComponent(removeAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(searchEmailAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(18, 18, 18) 
       .addComponent(searchAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(203, Short.MAX_VALUE)) 
     ); 

     jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Show Input/Show Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14))); // NOI18N 

     showInputOutput.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N 
     jScrollPane1.setViewportView(showInputOutput); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

     pack(); 
    }     
    private javax.swing.JButton addAdress; 
    private javax.swing.JButton removeAdress; 
    private javax.swing.JButton searchAdress; 
    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; 
    public javax.swing.JTextArea showInputOutput; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextField firstName; 
    private javax.swing.JTextField lastName; 
    private javax.swing.JTextField emailAdress; 
    private javax.swing.JTextField searchEmailAdress; 
    private javax.swing.JTextField removeEmailAddress; 
    private javax.swing.JTextField street; 
    private javax.swing.JTextField number; 
    private javax.swing.JTextField additive; 
    private javax.swing.JTextField zipCode; 
    private javax.swing.JTextField city;     
} 

ビューのActionListener

public class MainWindowActionListenerMVCVIEW implements ActionListener { 
    public JTextField firstName, lastName, emailAddress, street, number, additive, zipCode, city, searchEmailAdress, removeEmailAddress; 


    public MainWindowActionListenerMVCVIEW(JTextField fName, JTextField lName, JTextField eAddress, JTextField streetName, JTextField houseNumber, JTextField add, JTextField zCode, JTextField cityName, JTextField searchEAdress, JTextField removeEAddress) { 

     this.firstName = fName; 
     this.lastName = lName; 
     this.emailAddress = eAddress; 
     this.street = streetName; 
     this.number = houseNumber; 
     this.additive = add; 
     this.zipCode = zCode; 
     this.city = cityName; 
     this.searchEmailAdress = searchEAdress; 
     this.removeEmailAddress = removeEAddress; 
    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 
     firstName.getText(); 
     lastName.getText(); 
     emailAddress.getText(); 
     street.getText(); 
     number.getText(); 
     additive.getText(); 
     zipCode.getText(); 
     city.getText(); 
     searchEmailAdress.getText(); 
     removeEmailAddress.getText(); 
     System.out.println("test" + " " + firstName.getText()); // I put this in just te see if the *ActionListener* works 

    } 
} 

モデル

public class WolffAddressManagerMVCMODEL { 
    public String firstName; 
    public String lastName; 
    public String emailAddress; 
    public String street; 
    public String number; 
    public String additive; 
    public String zipCode; 
    public String city; 
} 

コントローラ

public class WolffAddressManagerMVCController { 
    public WolffAddressManagerMVCMODEL controllerModel; 
    public MainWindowActionListenerMVCVIEW controllerActionListener; 
    public WolffAddressManagerMVCJFrameVIEW controllerJFrame; 
    private Connection connect = null; 
    private final Statement statement = null; 
    private PreparedStatement preparedStatement = null; 
    private ResultSet resultSet = null; 
    public ArrayList<String> queryResultSet = new ArrayList(); 

    public void saveAddressToDataBase(MainWindowActionListenerMVCVIEW controller) throws Exception { 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=password"); 
      preparedStatement = connect.prepareStatement("SELECT email FROM dev.addresses WHERE email= '" + controller.emailAddress.getText() + "'"); 
      resultSet = preparedStatement.executeQuery(); 

      if(resultSet.next()) { 
       JOptionPane.showMessageDialog(null, "Email already exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE); 
      } else { 
       preparedStatement = connect.prepareStatement("INSERT INTO dev.addresses (firstname, lastname, email, street, housenumber, additve, zipcode, city) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); 
       preparedStatement.setString(1, controller.firstName.getText()); 
       preparedStatement.setString(2, controller.lastName.getText()); 
       preparedStatement.setString(3, controller.emailAddress.getText()); 
       preparedStatement.setString(4, controller.street.getText()); 
       preparedStatement.setString(5, controller.number.getText()); 
       preparedStatement.setString(6, controller.additive.getText()); 
       preparedStatement.setString(7, controller.zipCode.getText()); 
       preparedStatement.setString(8, controller.city.getText()); 
       preparedStatement.executeUpdate(); 
      } 

     } catch (ClassNotFoundException | SQLException | HeadlessException e) { 
      throw e; 
     } finally { 
      close(); 
     } 
    } 

    public void searchAddress(String emailAddress) throws Exception { 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
       // Setup the connection with the DB 
      connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=fiat128slc"); 
      preparedStatement = connect.prepareStatement("SELECT * FROM dev.addresses WHERE email= '" + emailAddress + "'"); 
      resultSet = preparedStatement.executeQuery(); 
      if(resultSet.isBeforeFirst()) { 
       while(resultSet.next()) { 
        queryResultSet.add(resultSet.getString("firstname")); 
        queryResultSet.add(resultSet.getString("lastname")); 
        queryResultSet.add(resultSet.getString("email")); 
        queryResultSet.add(resultSet.getString("street")); 
        queryResultSet.add(resultSet.getString("housenumber")); 
        queryResultSet.add(resultSet.getString("additve")); 
        queryResultSet.add(resultSet.getString("zipcode")); 
        queryResultSet.add(resultSet.getString("city")); 
       } 
      } else { 
       JOptionPane.showMessageDialog(null, "Email doesn't exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE); 
      } 
     } catch (ClassNotFoundException | SQLException | HeadlessException e) { 
      throw e; 
     } finally { 
      close(); 
     } 
    } 

答えて

0

私は私の最後の答えを編集しました。私は正しい道にいると思う。私のMVCプロジェクトでゲッターとセッターを使用する。私は今、クエリを処理するクラスを持っています。

コントローラ

public class WolffAddressManagerMVCController { 
    private final WolffAddressManagerMVCMODEL model; 
    private final WolffAddressManagerQueriesMVCSERVICES query; 

    public WolffAddressManagerMVCController(WolffAddressManagerMVCMODEL model) { 
     this.query = new WolffAddressManagerQueriesMVCSERVICES(); 
     this.model = model; 
    } 


    public void buildView() throws Exception { 
     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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(WolffAddressManagerMVCJFrameVIEW.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     java.awt.EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        new WolffAddressManagerMVCJFrameVIEW().setVisible(true); 
       } 
      }); 
     } 

    public void buildView2() throws Exception { 
     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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(WolffAddressManagerMVCUpdateJFrameVIEW.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     java.awt.EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        new WolffAddressManagerMVCUpdateJFrameVIEW().setVisible(true); 
       } 
      }); 
     } 

    public void addCredentials(WolffAddressManagerMVCJFrameVIEW view , WolffAddressManagerMVCMODEL model) throws Exception { 
     if(model.getEmailAddress().equals(model.getReTypeEmailAddress())) { 
      if(query.saveAddressToDataBase(view, model) == true) { 
       showCredentials(view); 
      } 
      else { 
       view.setTextArea(view.getTextArea() + "EMAIL ALREADY EXISTS\n\n"); 
      } 
     } else { 
      JOptionPane.showMessageDialog(null, "Emailaddresses are not the same", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 

    private void showCredentials(WolffAddressManagerMVCJFrameVIEW view) throws Exception { 
     DefaultListModel<String> jList = new DefaultListModel(); 
     JList<String> list = new JList(jList); 
     model.setSearchResultArray(searchCredentials(model)); 
     for(int i = 0; i < model.getSearchResultArray().size(); i++) { 
      jList.addElement(model.getSearchResultArray().get(i)); 
     } 
     list.setModel(jList); 

     int size = list.getModel().getSize(); 
     StringBuilder credentials = new StringBuilder(); 

     for(int i = 0; i < size; i++) { 
      credentials.append(list.getModel().getElementAt(i)).append("\n"); 
     } 
     view.setTextArea(view.getTextArea() + credentials.toString()+ "CREDENTIALS ADDED\n\n"); 
    } 

    public void removeCredentials() throws Exception { 
     query.removeAddress(model.getEmailAddress()); 
    } 

    public ArrayList<String> searchCredentials(WolffAddressManagerMVCMODEL model) throws Exception { 
     return query.searchAddress(model.getEmailAddress()); 
    } 

    public void showSearchResult(WolffAddressManagerMVCJFrameVIEW view, WolffAddressManagerMVCMODEL model) { 
     DefaultListModel<String> jList = new DefaultListModel(); 
     JList<String> list = new JList(jList); 

     try { 
      model.setSearchResultArray(searchCredentials(model)); 
     } catch (Exception e) { 
      Logger.getLogger(MainWindowActionListenerSearchMVCVIEW.class.getName()).log(Level.SEVERE, null, e); 
     } 
     for(int i = 0; i < model.getSearchResultArray().size(); i++) { 
      jList.addElement(model.getSearchResultArray().get(i)); 
     } 
     list.setModel(jList); 

     int size = list.getModel().getSize(); 
     StringBuilder credentials = new StringBuilder(); 
     for(int i = 0; i < size; i++) { 
      credentials.append(list.getModel().getElementAt(i)).append("\n"); 
     } 
     view.setTextArea(view.getTextArea() + credentials.toString()); 
    } 

    private void showUpdateResult(WolffAddressManagerMVCUpdateJFrameVIEW view, WolffAddressManagerMVCMODEL model) { 
     DefaultListModel<String> jList = new DefaultListModel(); 
     JList<String> list = new JList(jList); 

     try { 
      model.getUpdateResultArray(); 
     } catch (Exception e) { 
      Logger.getLogger(MainWindowActionListenerSearchMVCVIEW.class.getName()).log(Level.SEVERE, null, e); 
     } 
     for(int i = 0; i < model.getUpdateResultArray().size(); i++) { 
      jList.addElement(model.getUpdateResultArray().get(i)); 
     } 
     list.setModel(jList); 

     int size = list.getModel().getSize(); 
     StringBuilder credentials = new StringBuilder(); 
     for(int i = 0; i < size; i++) { 
      credentials.append(list.getModel().getElementAt(i)).append("\n"); 
     } 
     view.setTextArea(credentials.toString()); 
    } 

    public void updateCredentials(WolffAddressManagerMVCUpdateJFrameVIEW view,WolffAddressManagerMVCMODEL model) throws Exception { 
     if(query.updateAddress(view, model) == true) { 
      showUpdateResult(view,model); 
     } 
    } 
} 

モデル

 public class WolffAddressManagerMVCMODEL { 
    private String updateEmailAddress; 
    private String companyName; 
    private String firstName; 
    private String lastName; 
    private String emailAddress; 
    private String reTypeEmailAddress; 
    private String street; 
    private String number; 
    private String additive; 
    private String zipCode; 
    private String city; 
    private JTextField jTextFieldUpdateEmailAddress; 
    private JTextField jTextFieldCompanyName; 
    private JTextField jTextFieldFirstName; 
    private JTextField jTextFieldLastName; 
    private JTextField jTextFieldEmailAddress; 
    private JTextField jTextFieldReTypeEmailAddress; 
    private JTextField jTextFieldStreetName; 
    private JTextField jTextFieldHouseNumber; 
    private JTextField jTextFieldAdditive; 
    private JTextField jTextFieldZipCode; 
    private JTextField jTextFieldCityName; 
    private JTextField jTextFieldSearchEmailAddress; 
    private JTextField jTextFieldRemoveEmailAddress; 
    private ArrayList<String> searchResultArray = null; 
    private ArrayList<String> updateResultArray = null; 

    public String getUpdateEmailAddress() { 
     return updateEmailAddress; 
    } 

    public void setUpdateEmailAddress(String updateEmailAddress) { 
     this.updateEmailAddress = updateEmailAddress; 
    } 

    public String getCompanyName() { 
     return companyName; 
    } 

    public void setCompanyName(String companyName) { 
     this.companyName = companyName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getEmailAddress() { 
     return emailAddress; 
    } 

    public void setEmailAddress(String emailAddress) { 
     this.emailAddress = emailAddress; 
    } 

    public String getReTypeEmailAddress() { 
     return reTypeEmailAddress; 
    } 

    public void setReTypeEmailAddress(String reTypeEmailAddress) { 
     this.reTypeEmailAddress = reTypeEmailAddress; 
    } 

    public String getStreet() { 
     return street; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 

    public String getNumber() { 
     return number; 
    } 

    public void setNumber(String number) { 
     this.number = number; 
    } 

    public String getAdditive() { 
     return additive; 
    } 

    public void setAdditive(String additive) { 
     this.additive = additive; 
    } 

    public String getZipCode() { 
     return zipCode; 
    } 

    public void setZipCode(String zipCode) { 
     this.zipCode = zipCode; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public JTextField getJTextFieldUpdateEmailAddress() { 
     return jTextFieldUpdateEmailAddress; 
    } 

    public void setJTextFieldUpdateEmailAddress(JTextField updateEmailAddress) { 
     this.jTextFieldUpdateEmailAddress = updateEmailAddress; 
    } 

    public JTextField getJTextFieldCompanyName() { 
     return jTextFieldCompanyName; 
    } 

    public void setJTextFieldCompanyName(JTextField companyName) { 
     this.jTextFieldCompanyName = companyName; 
    } 

    public JTextField getJTextFieldFirstName() { 
     return jTextFieldFirstName; 
    } 

    public void setJTextFieldFirstName(JTextField fName) { 
     this.jTextFieldFirstName = fName; 
    } 

    public JTextField getJTextFieldLastName() { 
     return jTextFieldLastName; 
    } 

    public void setJTextFieldLastName(JTextField lastName) { 
     this.jTextFieldLastName = lastName; 
    } 

    public JTextField getJTextFieldEmailAddress() { 
     return jTextFieldEmailAddress; 
    } 

    public void setJTextFieldEmailAddress(JTextField emailAddress) { 
     this.jTextFieldEmailAddress = emailAddress; 
    } 

    public JTextField getJTextFieldReTypeEmailAddress() { 
     return jTextFieldReTypeEmailAddress; 
    } 

    public void setJTextFieldReTypeEmailAddress(JTextField reTypeEmailAddress) { 
     this.jTextFieldReTypeEmailAddress = reTypeEmailAddress; 
    } 

    public JTextField getJTextFieldStreet() { 
     return jTextFieldStreetName; 
    } 

    public void setJTextFieldStreet(JTextField streetName) { 
     this.jTextFieldStreetName = streetName; 
    } 

    public JTextField getJTextFieldHouseNumber() { 
     return jTextFieldHouseNumber; 
    } 

    public void setJTextFieldHouseNumber(JTextField houseNumber) { 
     this.jTextFieldHouseNumber = houseNumber; 
    } 

    public JTextField getJTextfieldAdditive() { 
     return jTextFieldAdditive; 
    } 

    public void setJTextFieldAdditive(JTextField additive) { 
     this.jTextFieldAdditive = additive; 
    } 

    public JTextField getJTextFieldZipCode() { 
     return jTextFieldZipCode; 
    } 

    public void setJTexFieldZipCode(JTextField zipCode) { 
     this.jTextFieldZipCode = zipCode; 
    } 

    public JTextField getJTextFieldCity() { 
     return jTextFieldCityName; 
    } 

    public void setJTextFieldCity(JTextField cityName){ 
     this.jTextFieldCityName = cityName; 
    } 

    public JTextField getJTextFieldSearchEmailAddress(){ 
     return jTextFieldSearchEmailAddress; 
    } 

    public void setJTextFieldSearchEmailAddress(JTextField searchEmailAddress) { 
     this.jTextFieldSearchEmailAddress = searchEmailAddress; 
    } 

    public JTextField getJTextFieldRemoveEmailAddress(){ 
     return jTextFieldRemoveEmailAddress; 
    } 

    public void setJTextFieldRemoveEmailAddress(JTextField removeEAddress){ 
     this.jTextFieldRemoveEmailAddress = removeEAddress; 
    } 

    public ArrayList<String> getSearchResultArray() { 
     return searchResultArray; 
    } 

    public void setSearchResultArray(ArrayList searchResultArray) { 
     this.searchResultArray = searchResultArray; 
    } 

    public ArrayList<String> getUpdateResultArray() { 
     return updateResultArray; 
    } 

    public void setUpdateResultArray(ArrayList searchResultArray) { 
     this.updateResultArray = searchResultArray; 
    } 
} 

そして、ここでは、合計で私のActionListenerクラスの一つである私は、読み取り、更新を作成し、上の削除を実行するには、これらのクラスの4持っています私のデータベース。

public class MainWindowActionListenerAddMVCVIEW implements ActionListener { 

    WolffAddressManagerMVCMODEL model = new WolffAddressManagerMVCMODEL(); 
    WolffAddressManagerMVCController controller = new WolffAddressManagerMVCController(model); 
    public MainWindowActionListenerAddMVCVIEW(JTextField fName, JTextField lName, JTextField eAddress, JTextField streetName, JTextField houseNumber, JTextField add, JTextField zCode, JTextField cityName) { 

     this.model.setJTextfiedFirstName(fName); 
     this.model.setJTextFieldLastName(lName); 
     this.model.setJTextFieldEmailAddress(eAddress); 
     this.model.setJTextFieldStreet(streetName); 
     this.model.setJTextFieldHouseNumber(houseNumber); 
     this.model.setJTextFieldAdditive(add); 
     this.model.setJTexFieldZipCode(zCode); 
     this.model.setJTextFieldCity(cityName); 

    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 
     model.setFirstName(model.getJTextFieldFirstName().getText()); 
     model.setLastName(model.getJTextFieldLastName().getText()); 
     model.setEmailAddress(model.getJTextFieldEmailAddress().getText()); 
     model.setStreet(model.getJTextFieldStreet().getText()); 
     model.setNumber(model.getJTextFieldHouseNumber().getText()); 
     model.setAdditive(model.getJTextfieldAdditive().getText()); 
     model.setZipCode(model.getJTextFieldZipCode().getText()); 
     model.setCity(model.getJTextFieldCity().getText()); 
     try { 
      controller.saveAddressToDataBase(this); 
     } catch (Exception ex) { 
      Logger.getLogger(MainWindowActionListenerAddMVCVIEW.class.getName()).log(Level.SEVERE, null, ex); 
     } 
+0

はい、java bean class –

関連する問題