简体   繁体   中英

Making items in jcombobox visible

I am trying to make a combo box which populates the item inside according to my database. at the moment the database contains 5 records which are test1,test2,test3,test4,test5. I've made a loop which is meant to populate it with each record.

private void selectschoolActionPerformed(java.awt.event.ActionEvent evt) {                                             
    ResultSet rs = null;
    selectschool.setVisible(true);
    try{
      rs = DAO.getAllSchool();
      ArrayList allSchoolName = new ArrayList();
      int count = 0;
      while(rs.next()){
          allSchoolName.add(rs.getString("SchoolName"));
      count++;

      }
      Object ob[] = new Object[count];
      for (int i = 0; i < count; i++){
            ob[i] = allSchoolName.get(i);
            selectschool.addItem(ob[i]);
      }
  }catch (Exception e){
      System.out.println("Exception: " + e);
  }
CreateTimeTable ctt = new CreateTimeTable();
    ctt.setVisible(true);
    String sch = selectschool.getSelectedItem().toString();
    ctt.jTextArea1.setText(sch);

I have tested in the for loop and it loops the right amount of times to fill the array will all the records no more no less. but in the GUI when I drop down the combo box, it is empty. But when I click and hold the mouse down, drag it below to the empty field below, it will select test1 and only test 1. How could I make the rest of the values show up? I've tested it and the ob[] holds the right records which need to be entered into the combo box. I think it might be the selectschool.addItem() which isn't working right. Much appreciated, Thanks.

(DAO.getAllSchool(); just retrieves the database records from sql)

You need to create the JComboBox and add an ActionListener to it. Not create the ActionPerformed and inside that set the JComboBox .

Try it like this:

// fill with values from ResultSet rs (however you obtain it)
ArrayList<String> schools = new ArrayList<String>();
while(rs.next())
  schools.add(rs.getString("SchoolName"));

// create the JComboBox
final JComboBox<String> selectSchool = new JComboBox<String>(schools.toArray(new String[]{}));
// add an actionlistener
selectSchool.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // do something with e.g. printing the selection
    String school = (String) selectSchool.getSelectedItem();
    System.out.println("You selected " + school);
  }
});

edit: Or in a complete, very simple but working example:

package sto;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        // create school list (in your example here would be the ResultSet)
        ArrayList<String> schools = new ArrayList<String>();
        schools.add("School 1");
        schools.add("School 2");
        schools.add("School 3");
        schools.add("School 4");

        // create the JComboBox
        final JComboBox<String> selectSchool = new JComboBox<String>(schools.toArray(new String[]{}));
        // add an actionlistener
        selectSchool.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // do something with e.g. printing the selection
                String school = (String) selectSchool.getSelectedItem();
                System.out.println("You selected " + school);
            } 
        });

        // create a JFrame and add the JComboBox
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(selectSchool);
        frame.pack();
        frame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM