I'm having trouble generating a drop down list using an array string that I populate in the Controller class. The list displays in the GUI but contains no values. Am i using the getter/setter method incorrectly? I've been unable to find an example despite looking at the Oracle documention for JComboBoxes. The api made reference to the setModel method that I presume I'am using incorrectly. Can anyone provide a simple example?
package example;
import javax.swing.*;
public class MyApp extends JFrame {
JTabbedPane tabbedPane = new JTabbedPane();
View view = new View();
ColourView colourView = new ColourView();
Controller controller = new Controller(colourView);
public MyApp() {
tabbedPane.add("First Tab", colourView);
getContentPane().add(tabbedPane);
}
package example;
import javax.swing.*;
public class ColourView extends View {
private JLabel colourLabel;
private JComboBox comboBox;
public ColourView() {
colourLabel = new JLabel();
colourLabel.setText("Colours");
colourLabel.setBounds(20, 30, 70, 20);
mainContentLayeredPane.add(colourLabel, JLayeredPane.DEFAULT_LAYER);
comboBox = new JComboBox();
comboBox.setSize(100, 20);
mainContentLayeredPane.add(comboBox, JLayeredPane.DEFAULT_LAYER);
}
public void setComboBox(String[] list) {
comboBox.setModel(new DefaultComboBoxModel());
for (Object item : list) {
comboBox.addItem(item);
}
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
MyApp app = new MyApp();
app.setVisible(true);
app.setSize(600, 600);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
package example;
import java.util.ListIterator;
public class Controller {
private ColourView colourView;
ListIterator<String> litr;
String listData[] = {"Item 1", "Item 2", "Item 3", "Item 4"};
Controller(ColourView colourView) {
this.colourView = colourView;;
}
public void getListData() {
colourView.setComboBox(listData);
}
}