Hello Daniweb Members,

I am a beginner programmer and I have a problem,

I am doing a PAT (Practical Assessment Task) for my school and I have come accross a huge problem,

I'm trying to get a value from a selected combobox in Netbeans, (I am using the GUI builder), but I just don't know what is going on fullstop.

I would like to use the variable from the combobox to decide which of the following frames it shall go to.

This is my code, Please Help <3

______________________________________________________________________________________

private void CargoTypeActionPerformed(java.awt.event.ActionEvent evt) {
            int value = 0;

    JComboBox typeCargo = (JComboBox)evt.getSource();
    String type = (String)typeCargo.getSelectedItem();

    if("Children".equals(type))
    {
    value = 1;
    } else if("Goods".equals(type))
    {
    value = 2;
    }

    }



    private void NextActionPerformed(java.awt.event.ActionEvent evt) {
            int
    load == 1;

    if(Children == load)
    {
       dispose();
       new HowMany().setVisible(true);;
    } else if(Goods != load)
    {
       dispose();
       new HowMuch().setVisible(true);;
    }



    }

Try printing "type" at line number 6 , that might give you some clue. Also, paste on your entire code so that we get a better idea

OK well, typing "type" didn't work, so here is my whole code...
Thank you so much for the feedback btw :)<3

import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class WhatCargo extends javax.swing.JFrame {

    /** Creates new form WhatCargo */
    public WhatCargo() {
        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() {

        jLabel1 = new javax.swing.JLabel();
        Next = new javax.swing.JButton();
        CargoType = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("What Type of Cargo Do You Want Us To Transport?");

        Next.setText("Next");
        Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }
        });

        CargoType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Children", "Goods", " " }));
        CargoType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CargoTypeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Next)
                            .addComponent(CargoType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(CargoType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Next, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>                        

    private void CargoTypeActionPerformed(java.awt.event.ActionEvent evt) {                                          
            int value = 0;

    JComboBox typeCargo = (JComboBox)evt.getSource();
    String type = (String)typeCargo.getSelectedItem();

    type
    if("Children".equals(type))
    {
    value = 1;
    } else if("Goods".equals(type))
    {
    value = 2;
    }

    }                                         

    private void NextActionPerformed(java.awt.event.ActionEvent evt) {                                     
            int
    load == 1;

    if(Children == load)
    {
       dispose();
       new HowMany().setVisible(true);;
    } else if(Goods != load)
    {
       dispose();
       new HowMuch().setVisible(true);;
    }

    }                                    

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WhatCargo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JComboBox CargoType;
    public javax.swing.JButton Next;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   

}
  • don't to create a new Top-Level Container, nor on runtime, this is road to the troubles

  • see CardLayout

  • there are issues with variables (I can see that in ActionPerformed)

  • why to re_create a new JComboBox typeCargo = (JComboBox)evt.getSource();, this JComboBox isn't placed and layed in Swing GUI, then you lost reference betweens different Objects

4th bullet point - that's not a new JComboBox, it's just a new reference to an existing JComboBox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.