2010-06-29 23 views
0
package org.shop.ui; 

import demo.Customer; 
import java.util.List; 
import java.util.logging.Logger; 
import javax.persistence.EntityManager; 
import javax.persistence.Persistence; 
import javax.persistence.Query; 
import org.openide.util.NbBundle; 
import org.openide.windows.TopComponent; 
import org.openide.windows.WindowManager; 
import org.openide.util.ImageUtilities; 
import org.netbeans.api.settings.ConvertAsProperties; 

/** 
* Top component which displays something. 
*/ 
@ConvertAsProperties(dtd = "-//org.shop.ui//Customer//EN", 
autostore = false) 
public final class CustomerTopComponent extends TopComponent { 

    private static CustomerTopComponent instance; 
    /** path to the icon used by the component and its open action */ 
    static final String ICON_PATH = "org/shop/ui/einstein_clerk.gif"; 
    private static final String PREFERRED_ID = "CustomerTopComponent"; 

    EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager(); 
Query query = entityManager.createQuery("SELECT c FROM Customer c"); 
List<Customer> resultList = query.getResultList(); 
for (Customer c : resultList) { 
    jTextArea1.append(c.getName() + " (" + c.getCity() + ")" + "\n"); 
} 

    public CustomerTopComponent() { 
     initComponents(); 
     setName(NbBundle.getMessage(CustomerTopComponent.class, "CTL_CustomerTopComponent")); 
     setToolTipText(NbBundle.getMessage(CustomerTopComponent.class, "HINT_CustomerTopComponent")); 
     setIcon(ImageUtilities.loadImage(ICON_PATH, true)); 


    } 

    /** 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. 
    */ 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
    }// </editor-fold>       

    // Variables declaration - do not modify      
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    // End of variables declaration     
    /** 
    * Gets default instance. Do not use directly: reserved for *.settings files only, 
    * i.e. deserialization routines; otherwise you could get a non-deserialized instance. 
    * To obtain the singleton instance, use {@link #findInstance}. 
    */ 
    public static synchronized CustomerTopComponent getDefault() { 
     if (instance == null) { 
      instance = new CustomerTopComponent(); 
     } 
     return instance; 
    } 

    /** 
    * Obtain the CustomerTopComponent instance. Never call {@link #getDefault} directly! 
    */ 
    public static synchronized CustomerTopComponent findInstance() { 
     TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID); 
     if (win == null) { 
      Logger.getLogger(CustomerTopComponent.class.getName()).warning(
        "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system."); 
      return getDefault(); 
     } 
     if (win instanceof CustomerTopComponent) { 
      return (CustomerTopComponent) win; 
     } 
     Logger.getLogger(CustomerTopComponent.class.getName()).warning(
       "There seem to be multiple components with the '" + PREFERRED_ID 
       + "' ID. That is a potential source of errors and unexpected behavior."); 
     return getDefault(); 
    } 

    @Override 
    public int getPersistenceType() { 
     return TopComponent.PERSISTENCE_ALWAYS; 
    } 

    @Override 
    public void componentOpened() { 
     // TODO add custom code on component opening 
    } 

    @Override 
    public void componentClosed() { 
     // TODO add custom code on component closing 
    } 

    void writeProperties(java.util.Properties p) { 
     // better to version settings since initial version as advocated at 
     // http://wiki.apidesign.org/wiki/PropertyFiles 
     p.setProperty("version", "1.0"); 
     // TODO store your settings 
    } 

    Object readProperties(java.util.Properties p) { 
     if (instance == null) { 
      instance = this; 
     } 
     instance.readPropertiesImpl(p); 
     return instance; 
    } 

    private void readPropertiesImpl(java.util.Properties p) { 
     String version = p.getProperty("version"); 
     // TODO read your settings according to their version 
    } 

    @Override 
    protected String preferredID() { 
     return PREFERRED_ID; 
    } 

} 

以上是netbeans的應用CRUD應用 參考網站 - http://platform.netbeans.org/tutorials/nbm-crud.html --point沒有-4。 在編譯上面的代碼後,即時通訊非法獲取類型錯誤的行開始 - 「爲(客戶c:resultList){」 和另一個錯誤在下一行作爲非法前向參考 PLZ指南如何解決上述2錯誤誤差在NetBeans污物應用

回答

1

此代碼:

EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager(); 
Query query = entityManager.createQuery("SELECT c FROM Customer c"); 
List<Customer> resultList = query.getResultList(); 
for (Customer c : resultList) { 
    jTextArea1.append(c.getName() + " (" + c.getCity() + ")" + "\n"); 
} 

需要在一個方法或構造,如本教程中明確指出:

一下添加到TopComponent的構造函數的末尾:(...)

+0

謝謝你的工作.. 但在運行應用程序之前的crud應用程序,教程說,啓動數據庫服務器。教程網站 - http://platform.netbeans.org/tutorials/nbm-crud.html - 運行原型.. 是netbeans6.8中已存在的服務器,或者我必須安裝任何oracle sql server,並且需要連接到NetBeans的? plz指南.. – 2010-06-29 15:02:40

0

請檢查您的模塊的源代碼級別。

查看源代碼下的模塊屬性。 也許它的級別低於1.5,因此泛型和增強的「for(x:y)」語法不可用。

我剛剛嘗試在NetBeans 6.9中創建一個新模塊,但它將初始源級別設置爲1.6。