I'm writing a Java GUI. I have a few preset JComboBoxes
and to be able to distinct them from each other, I want to extend the class and add an enum
variable that can help me distinct them from each other.
Here's an MCVE of two standard JComboBoxes
:
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 = { "Yes", "No" };
DefaultComboBoxModel ynModel = new DefaultComboBoxModel(yesNo);
ynComboBox = new JComboBox(ynModel);
components = new JComponent[] { tfComboBox, ynComboBox };
JButton printSelection = new JButton("Print Type");
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("Printing selection: " + temp.getSelectedItem().toString());
// if (temp.getBoxType() == BoxType.Company){
// System.out.println("Companies say: " +
// temp.getSelectedItem().toString());
// } else if(temp.getBoxType() == BoxType.Device){
// System.out.println("Devices are: " +
// 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("MCVE");
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
}
}
As the example above is illustrating, there's no way for me to distinct the two JComboBoxes
from each other when the user clicks the button. An example of the usage of the BoxType
would be that one BoxType
would print one type of message while the other BoxType
would print another message. E.g:
if(temp.getBoxType() == BoxType.Device){
System.out.println("The devices are: " + temp.getSelectedItem().toString());
} else if(temp.getBoxType() == BoxType.Company){
System.out.println("The companies say: " + temp.getSelectedItem().toString());
}
However I have stumbled into a problem with the constructor which I try to use similar to what I did with the JComboBox
and input a DefaultComboBoxModel
, which I haven't implemented yet in the XComboBox
class.
Question
How can I modify the XComboBox
class so that I can give it a DefaultComboBoxModel
when constructing the element?
I want to be able to do this:
ynComboBox = new XComboBox(tfModel);
ynComboBox.setBoxType(BoxType.Device);
Thank you for any help and or guidance!