cobie
cobie

Reputation: 7271

JComboBox not displaying till clicked on

I have a bunch of JComboBox which i have put in a JPanel but the JComboBoxes are not visible till they are clicked on. Has anyone got any idea if what I maybe doing wrong, the initialisation code is shown below:

public class DelegateUI extends JFrame implements Observer, ActionListener{
public static int vertexPoints[][]= {
    {586, 584}, {586, 754}, {83, 754}, {83, 582}, // 1 - 4
    {414, 246}, {340, 514}, {479, 514},  // 5 - 8
    {618, 108}, {700, 203}, {493, 336}, {492, 126}, // 9 - 11
    {935, 123}, {937, 470}, {729, 473}, {730, 125}, // 12 - 15  
    {772, 557}, {656, 672}, {608, 410}, 
    {1042, 120}, {1104, 179}, {1071, 508}, {967, 169}   // 19 - 22
    };
private Model model;
private JPanel topPanel;
private JComboBox searchType;
private JComboBox startVertex;
private JComboBox goalVertex;
Object [] searchTypes = { "Breadth First", "Uniform Cost", "Greedy Best First", "A*"};
Object [] vertexNumbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
        "16", "17", "18", "19", "20", "21", "22"};



public DelegateUI(Model m){

    this.model = m;
    m.addObserver(this);
    setSize(1200, 1000);

    topPanel = new JPanel();
    topPanel.setBackground(Color.BLACK);
    searchType = new JComboBox(searchTypes);
    startVertex = new JComboBox(vertexNumbers);
    goalVertex = new JComboBox(vertexNumbers);

    searchType.setSelectedIndex(0);
    searchType.addActionListener(this);

    startVertex.setSelectedIndex(3);
    startVertex.addActionListener(this);
    startVertex.setActionCommand("start");

    goalVertex.setSelectedIndex(19);
    goalVertex.addActionListener(this);
    goalVertex.setActionCommand("goal");

    topPanel.add(searchType);
    topPanel.add(startVertex);
    topPanel.add(goalVertex);


    this.setLayout(new BorderLayout());
    this.add(topPanel, BorderLayout.NORTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}

Upvotes: 0

Views: 232

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I used your code and it works for me:

public void createGUI(){

        setSize(1200, 1000);

        topPanel = new JPanel();

        JComboBox searchType = new JComboBox(new Object[]{"1", "2"});
        JComboBox startVertex = new JComboBox(new Object[]{"1", "2"});
        JComboBox goalVertex = new JComboBox(new Object[]{"1", "2"});

        searchType.setSelectedIndex(1);         
        startVertex.setSelectedIndex(1);          
        startVertex.setActionCommand("start");

        goalVertex.setSelectedIndex(1);
        goalVertex.setActionCommand("goal");

        topPanel.add(searchType);
        topPanel.add(startVertex);
        topPanel.add(goalVertex);


        this.setLayout(new BorderLayout());
        this.add(topPanel, BorderLayout.NORTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

}

enter image description here

Sounds like you have some issue with ActionListener or new JComboBox(Vector/Object[]).

Upvotes: 1

Related Questions