0

My question is how to link two comboboxes together. The reason being that I want the user to be able to choose a manufacturer brand and for the next combobox to display the cars linked to that manufacturer. Then I want the data to be put into a jtextarea to be displayed to the user. This also goes for my jlists in my program.

package rlc.building.program;
/*
 *
 * @author Jacob
 */
public class GUI extends javax.swing.JFrame {

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

    jScrollPane2 = new javax.swing.JScrollPane();
    jList2 = new javax.swing.JList();
    jScrollPane3 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    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();
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    jButton1 = new javax.swing.JButton();
    jComboBox3 = new javax.swing.JComboBox();
    jComboBox4 = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jList2.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { "LED Light Bar £250", "Rally Light Hood Mount £150", "Rally Lights x2 £100", "Limo Tinted Windows £1500", "Custom Exo-Skelton Roll Cage £3000", "Custom Roll-Cage £1000", "Thick White Wall Tires £1450", "Front and Rear Strut Bars £400", " ", " " };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane2.setViewportView(jList2);

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

    jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
    jLabel1.setText("Real Life Car Building Program");

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel2.setText("Modifications");
    jLabel2.setToolTipText("This area below is a list of all the modifications you can choose from for any vehicle.");

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel3.setText("Manufacturers");
    jLabel3.setToolTipText("The car companys that you can choose from.");

    jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel4.setText("Vehicles");
    jLabel4.setToolTipText("The vehicles you can choose from.");

    jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel5.setText("Parts");
    jLabel5.setToolTipText("The parts that you can install on the vehicle straight from the manufacturer.");

    jList1.setModel(new javax.swing.AbstractListModel() {
        String[] strings = { "Leather Seats £1000", "New Stainless Steel Exhaust £500", "Alloy Wheels £1200", "Checker Plated Steel Panels £400", " " };
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane1.setViewportView(jList1);

    jButton1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
    jButton1.setText("Reset");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chevrolet", "Dodge", "Ford", "Honda", "Infiniti", "Land Rover", "Mazda", "Mitsubishi", "Nissan", "Pontiac", "Scion", "Subaru", "Toyota", "Volkswagen" }));
    jComboBox3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox3ActionPerformed(evt);
        }
    });

    jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

    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(208, 208, 208)
            .addComponent(jLabel1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(layout.createSequentialGroup()
            .addGap(59, 59, 59)
            .addComponent(jLabel3)
            .addGap(163, 163, 163)
            .addComponent(jLabel5)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addGap(93, 93, 93))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane3)
            .addGap(18, 18, 18)
            .addComponent(jButton1)
            .addGap(26, 26, 26))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(71, 71, 71)
                    .addComponent(jLabel4)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(14, 14, 14)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)))
            .addGap(11, 11, 11)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(3, 3, 3)
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(47, 47, 47)
                            .addComponent(jButton1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                            .addContainerGap())))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))))
    );

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

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextArea1.setText("");
}                                        

private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {                                           

}                                          

/**
 * @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(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.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 GUI().setVisible(true);
        }
    });



}



// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JComboBox jComboBox4;
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.JList jList1;
private javax.swing.JList jList2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration                   
}

The code is for my GUI that I have already built. I just need to fill in the bones to make the program work.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71

1 Answers1

4

The reason being that I want the user to be able to choose a manufacturer brand and for the next combobox to display the cars linked to that manufacturer.

Some hints:

  • Create a Manufacturer POJO class that holds the manufacturer's data and a list with its associated cars.
  • Add a list of Manufacturer objects to the first combo box model.
  • Based on the Manufacturer selected in the first combo get the list of its associated cars and populate the second combo box model.
  • Based on the Car selected in the second combo box set the text area's text to display its data.

You will have to:

  • Provide either an ItemListener or ActionListener to both combo boxes.
  • Provide custom ListCellRenderer at least for the first combo box.

Suggested readings

dic19
  • 17,821
  • 6
  • 40
  • 69
  • Question: what is a POJO class in Java? Can I use this in the comboboxes action performed area or do I have to do this outside that. – Jacob Fellows May 21 '15 at 14:07
  • POJO is a [Plain Old Java Object](http://en.wikipedia.org/wiki/Plain_Old_Java_Object). @JacobFellows – dic19 May 21 '15 at 14:10
  • Take a look to [this example](http://stackoverflow.com/a/19096485/1795530) for a better understanding. This is about adding and displaying `Person` objects (simple POJO's) to a combo box. I think you'll be able to figure out how to implement the same with `Manufacturer` and `Car`. @JacobFellows – dic19 May 21 '15 at 14:25
  • Ok so looking at the example I need to create a class for my jComboBoxes that link together and that should work. Also thanks for the help. @dic19 – Jacob Fellows May 21 '15 at 17:29
  • You are welcome :) That's right. The example covers the POJO and custom renderer points. To selection changes look at the linked resources. @JacobFellows – dic19 May 21 '15 at 17:45
  • If I import the java swing functions will this make the program work with the private variable declarations or do I only need the ones that matter to me. @dic19 – Jacob Fellows May 22 '15 at 14:51