简体   繁体   English

如何使用 JComboBox object 作为 java 方法的参数?

[英]how to use a JComboBox object as a parameter of a java method?

I get an error message due to the JComboBox parameter I am using in my code.由于我在代码中使用的 JComboBox 参数,我收到一条错误消息。 The method is: public void miseAJourComboBox(JComboBox<String> jcb)方法是: public void miseAJourComboBox(JComboBox<String> jcb)

The message is:消息是:

error: cannot find symbol
public void miseAJourComboBox(JComboBox<String> jcb){
symbol: class JComboBox
location: class FrameAchat

and the code (simplified) is:代码(简化)是:

public class FrameAchat extends javax.swing.JFrame {
 
    public ConnectionSQL connectionSQL;
 
    /** Creates new form FrameAchat */
    public FrameAchat() {
        connectionSQL = new ConnectionSQL();
        System.out.println("passe par constructeur de achat");
        initComponents();
    }
 
 
    /** 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">                          
    private void initComponents() {
 
        jLabelFicheAchat = new javax.swing.JLabel();
        jComboIdPdt = new javax.swing.JComboBox<>();
        jLabelMarque = new javax.swing.JLabel();
        jLabelModele = new javax.swing.JLabel();
        jLabelAnnee = new javax.swing.JLabel();
        jLabelPrix = new javax.swing.JLabel();
        jComboIdFrs = new javax.swing.JComboBox<>();
        jLabelNom = new javax.swing.JLabel();
        jComboQte = new javax.swing.JComboBox<>();
        jLabelQte = new javax.swing.JLabel();
        Achat = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabelFicheAchat.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabelFicheAchat.setText("Creation Achat");
 
        jComboIdPdt.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
 
        jLabelMarque.setText("jLabel1");
 
        jLabelModele.setText("jLabel1");
 
        jLabelAnnee.setText("jLabel1");
 
        jLabelPrix.setText("jLabel1");
 
        jComboIdFrs.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
 
        jLabelNom.setText("jLabel1");
 
        jComboQte.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
 
        jLabelQte.setText("Quantité:");
 
        Achat.setText("ACHETER");
 
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabelFicheAchat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 177, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(91, 91, 91))
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(53, 53, 53)
                        .add(jLabelMarque)
                        .add(18, 18, 18)
                        .add(jLabelModele)
                        .add(28, 28, 28)
                        .add(jLabelAnnee)
                        .add(18, 18, 18)
                        .add(jLabelPrix))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabelQte))
                        .add(53, 53, 53)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabelNom)
                            .add(Achat))))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(21, 21, 21)
                .add(jLabelFicheAchat)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboIdPdt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabelMarque)
                    .add(jLabelModele)
                    .add(jLabelAnnee)
                    .add(jLabelPrix))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboIdFrs, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabelNom))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboQte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabelQte))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 76, Short.MAX_VALUE)
                .add(Achat)
                .add(51, 51, 51))
        );
 
        pack();
    }// </editor-fold>                        
 
    /**
     * @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(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameAchat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameAchat.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 FrameAchat().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton Achat;
    private javax.swing.JComboBox<String> jComboIdFrs;
    private javax.swing.JComboBox<String> jComboIdPdt;
    private javax.swing.JComboBox<String> jComboQte;
    private javax.swing.JLabel jLabelAnnee;
    private javax.swing.JLabel jLabelFicheAchat;
    private javax.swing.JLabel jLabelMarque;
    private javax.swing.JLabel jLabelModele;
    private javax.swing.JLabel jLabelNom;
    private javax.swing.JLabel jLabelPrix;
    private javax.swing.JLabel jLabelQte;
    // End of variables declaration                   
 
    public void miseAJourComboBox(JComboBox<String> jcb){
        System.out.println("hi");
    }
 
 
}

I have been helped on that.我在这方面得到了帮助。 FYI, I missed to import the JComboBox or to add in my parameter the path of JComboBox:仅供参考,我错过了导入 JComboBox 或在我的参数中添加 JComboBox 的路径:

public void miseAJourComboBox(javax.swing.JComboBox<String> jcb ,String tableAParcourir, String champRecherche){

instead of:代替:

public void miseAJourComboBox(JComboBox<String> jcb ,String tableAParcourir, String champRecherche){

and now it works现在它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM