How to extend the JComboBox class?
<p>I'm writing a Java GUI. I have a few preset <code>JComboBoxes</code> and to be able to distinct them from each other, I want to extend the class and add an <code>enum</code> variable that can help me distinct them from each other.</p>
<p>Here's an MCVE of two standard <code>JComboBoxes</code>:</p>
<pre><code>import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class XComboBoxMCVE extends JPanel {

    private JComboBox tfComboBox;
    private JComboBox ynComboBox;
    private JComponent[] components;

    public XComboBoxMCVE() {
        setLayout(new BorderLayout());
    
        JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5));

        Boolean[] trueFalse = { true, false };
        DefaultComboBoxModel tfModel = new DefaultComboBoxModel(trueFalse);
        tfComboBox = new JComboBox(tfModel);

        String[] yesNo = { &quot;Yes&quot;, &quot;No&quot; };
        DefaultComboBoxModel ynModel = new DefaultComboBoxModel(yesNo);
        ynComboBox = new JComboBox(ynModel);

        components = new JComponent[] { tfComboBox, ynComboBox };
    
        JButton printSelection = new JButton(&quot;Print Type&quot;);
        printSelection.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (JComponent component : components) {
                    // I also have other components in component in program,
                    // therefore this usage..
                    if (component instanceof JComboBox) {
                        JComboBox temp = (JComboBox) component;
                        System.out.println(&quot;Printing selection: &quot; + temp.getSelectedItem().toString());
                        // if (temp.getBoxType() == BoxType.Company){
                        // System.out.println(&quot;Companies say: &quot; +
                        // temp.getSelectedItem().toString());
                        // } else if(temp.getBoxType() == BoxType.Device){
                        // System.out.println(&quot;Devices are: &quot; +
                        // temp.getSelectedItem().toString());
                        // }
                    }
                }
            }
        });

        JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5));
        buttonPane.add(printSelection);

        comboPanel.add(tfComboBox);
        comboPanel.add(ynComboBox);

        add(comboPanel, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.PAGE_END);
    }

    public static void createAndShowGUI(){
        JFrame frame = new JFrame(&quot;MCVE&quot;);
        frame.setLayout(new BorderLayout());
    
        XComboBoxMCVE pane = new XComboBoxMCVE();
        
        frame.add(pane, BorderLayout.CENTER);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

class XComboBox extends JComboBox {
    BoxType type;

    public XComboBox(BoxType type) {
        this.type = type;
    }

    public void setBoxType(BoxType type) {
        this.type = type;
    }

    public BoxType getBoxType() {
        return this.type;
    }

    public enum BoxType {
        Model, Company, Device
    }
}
</code></pre>
<p>As the example above is illustrating, there's no way for me to distinct the two <code>JComboBoxes</code> from each other when the user clicks the button. An example of the usage of the <code>BoxType</code> would be that one <code>BoxType</code> would print one type of message while the other <code>BoxType</code> would print another message. E.g:</p>
<pre><code>if(temp.getBoxType() == BoxType.Device){
    System.out.println(&quot;The devices are: &quot; + temp.getSelectedItem().toString());
} else if(temp.getBoxType() == BoxType.Company){
    System.out.println(&quot;The companies say: &quot; + temp.getSelectedItem().toString());
}
</code></pre>
<p>However I have stumbled into a problem with the constructor which I try to use similar to what I did with the <code>JComboBox</code> and input a <code>DefaultComboBoxModel</code>, which I haven't implemented yet in the <code>XComboBox</code> class.</p>
<h1>Question</h1>
<p>How can I modify the <code>XComboBox</code> class so that I can give it a <code>DefaultComboBoxModel</code> when constructing the element?</p>
<p>I want to be able to do this:</p>
<pre><code>ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);
</code></pre>
<p>Thank you for any help and or guidance!</p>