Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JComboBox doesn't work properly. HELP.

Status
Not open for further replies.

lck092580

Programmer
Jun 19, 2002
440
CA
I'm out of ideas. I currently have a JComboBox set up but it doesn't seem to want to list more than one item (even when there's more than one).

What I did was create an applet that recieves input. Since I seem to have errors initializing a JComboBox with an empty array I've used the default constructor and whenever a new record is entered I simply use the .addItem("BLAH") to update the combobox. However, no matter how many items I put in.. it only displays one item. Does anyone know why?

Thanks.

T
 
U'd probably want the code.. it's too long.. so i've snipped parts out of it:

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ApplicationCentre extends Applet
{ private JButton inputButton, admitButton, displayButton, submitButton;
private JPanel buttonsPanel, defaultPanel, inputPanel, admitPanel, displayPanel, rightPanel;
private JPanel textFieldPanel;
private JList universitiesList;
private JTextArea inputTextArea;
private JLabel nameLabel, averageLabel, storageAmount;
private JTextField studentNameTextfield, studentAverageTextfield;
private JComboBox studentsComboBox;
private int count, capacity;
private Student[] studentArray;
public static final String[] UNIVERSITIES = {"Toronto", "York", "Western", "Brock", "Guelph", "Waterloo", "McGill", "Concordia", "Laval", "Macmaster"};

public void init()
{ int size = Integer.parseInt(JOptionPane.showInputDialog("Enter size of database"));
capacity = size;
studentArray = new Student[capacity];
count = 0;
setLayout(new BorderLayout());

// JPanel for action listener buttons (input, admit, display)
buttonsPanel = new JPanel(new GridLayout(3,1));
inputButton = new JButton("Input");
inputButton.addActionListener(new inputButtonListener());
admitButton = new JButton("Admit");
admitButton.addActionListener(new admitButtonListener());
displayButton = new JButton("Display");
displayButton.addActionListener(new displayButtonListener());
buttonsPanel.add(inputButton);
buttonsPanel.add(admitButton);
buttonsPanel.add(displayButton);

// constructs the defaultPanel, inputPanel, admitPanel, & displayPanel
defaultPanel();
inputPanel();
admitPanel();

rightPanel = new JPanel(new CardLayout());
rightPanel.add(defaultPanel, "DEFAULT");
rightPanel.add(inputPanel, "INPUT");
rightPanel.add(admitPanel, "ADMIT");

add(buttonsPanel, BorderLayout.WEST);
add(rightPanel, BorderLayout.CENTER);

}

private void defaultPanel()
{ // JPanel for default panel

}

private void inputPanel()
{ // JPanel for inputPanel
submitButton.addActionListener(new submitButtonListener());
}

private void admitPanel()
{ // JPanel for admitPanel
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraint = new GridBagConstraints();
admitPanel = new JPanel(gridbag);
studentsComboBox = new JComboBox();
admitPanel.add(studentsComboBox);
}


private class inputButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ CardLayout cl = (CardLayout)(rightPanel.getLayout());
cl.show(rightPanel, "INPUT");
}
}

private class submitButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ studentsComboBox.addItem(name);

}
}

private class admitButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ CardLayout cl = (CardLayout)(rightPanel.getLayout());
cl.show(rightPanel, "ADMIT");
System.out.println(studentsComboBox.getItemCount());
System.out.println(studentsComboBox.isVisible());
}
}

private class displayButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{
}
}
}
 
Not sure if thisis what you mean but......

Code:
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ApplicationCentre extends Applet
{    private JButton inputButton, admitButton, displayButton, submitButton;
    private JPanel buttonsPanel, defaultPanel, inputPanel, admitPanel, displayPanel, rightPanel;
    private JPanel textFieldPanel;
    private JList universitiesList;
    private JTextArea inputTextArea;
    private JLabel nameLabel, averageLabel, storageAmount;
    private JTextField studentNameTextfield, studentAverageTextfield;
    private JComboBox studentsComboBox;
    private int count, capacity;
    //private Student[] studentArray;
    public static final String[] UNIVERSITIES = {"Toronto", "York", "Western", "Brock", "Guelph", "Waterloo", "McGill", "Concordia", "Laval", "Macmaster"};


  public ApplicationCentre()
  {
  }
    
    public void init()
    {   // int size = Integer.parseInt(JOptionPane.showInputDialog("Enter size of database"));
        //capacity = size;
        //studentArray = new Student[capacity];
        //count = 0;
        setLayout(new BorderLayout());
        
        // JPanel for action listener buttons (input, admit, display)
        buttonsPanel = new JPanel(new GridLayout(3,1));
        inputButton = new JButton("Input");
        inputButton.addActionListener(new inputButtonListener());
        admitButton = new JButton("Admit");
        admitButton.addActionListener(new admitButtonListener());
        displayButton = new JButton("Display");
        displayButton.addActionListener(new displayButtonListener());
        buttonsPanel.add(inputButton);
        buttonsPanel.add(admitButton);
        buttonsPanel.add(displayButton);
        
        // constructs the defaultPanel, inputPanel, admitPanel, & displayPanel
        setdefaultPanel(); 
        setinputPanel();
        setadmitPanel();
        
        rightPanel = new JPanel(new CardLayout());
        rightPanel.add(defaultPanel, "DEFAULT");
        rightPanel.add(inputPanel, "INPUT");
        rightPanel.add(admitPanel, "ADMIT");
        
        add(buttonsPanel, BorderLayout.WEST);
        add(rightPanel, BorderLayout.CENTER);
                
    }


  public static void main(String[] args)
  {
    ApplicationCentre applet = new ApplicationCentre();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    frame.setTitle("Applet Frame");
    applet.init();
    applet.start();
    frame.setSize(300, 300);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    frame.setLocation((d.width - frameSize.width) / 2, (d.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

    
    private void setdefaultPanel()
    {    // JPanel for default panel
        defaultPanel = new JPanel();
        
    }

    private void setinputPanel()
    {    // JPanel for inputPanel
        inputPanel = new JPanel();
    }
    
    private void setadmitPanel()
    {    // JPanel for admitPanel
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints constraint = new GridBagConstraints();
        admitPanel = new JPanel(gridbag);
        studentsComboBox = new JComboBox(UNIVERSITIES);
        admitPanel.add(studentsComboBox);
    }

    
    private class inputButtonListener implements ActionListener
    {    public void actionPerformed(ActionEvent event)
        {    CardLayout cl = (CardLayout)(rightPanel.getLayout());
            cl.show(rightPanel, "INPUT");
            studentsComboBox.addItem((JOptionPane.showInputDialog("Enter name")));
        }
    }
    
    private class submitButtonListener implements ActionListener
    {    public void actionPerformed(ActionEvent event)
        {                        studentsComboBox.addItem((JOptionPane.showInputDialog("Enter name")));

        }
    }    
    
    private class admitButtonListener implements ActionListener
    {    public void actionPerformed(ActionEvent event)
        {    CardLayout cl = (CardLayout)(rightPanel.getLayout());
            cl.show(rightPanel, "ADMIT");
            System.out.println(studentsComboBox.getItemCount());
            System.out.println(studentsComboBox.isVisible());
        }
    }
    
    private class displayButtonListener implements ActionListener
    {    public void actionPerformed(ActionEvent event)
        {    
        }
    }
}

I added a main class so i didn't have to run it as an applet, probably doesn't do what you want but it demonstrates adding to a JComboBox [upsidedown]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top