Java 使jcombobox中的项目可见

Java 使jcombobox中的项目可见,java,sql,arrays,loops,jcombobox,Java,Sql,Arrays,Loops,Jcombobox,我试图制作一个组合框,根据我的数据库填充里面的项目。目前,数据库包含5条记录,分别是test1、test2、test3、test4、test5。我已经做了一个循环,这意味着用每个记录填充它 private void selectschoolActionPerformed(java.awt.event.ActionEvent evt) { ResultSet rs = null; select

我试图制作一个组合框,根据我的数据库填充里面的项目。目前,数据库包含5条记录,分别是test1、test2、test3、test4、test5。我已经做了一个循环,这意味着用每个记录填充它

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);
我已经在for循环中进行了测试,它循环了正确的次数来填充数组,所有记录都不会减少。但是在GUI中,当我放下组合框时,它是空的。但是当我点击并按住鼠标,将它拖到下面的空白区域,它将选择test1,并且只选择test1。我如何才能使其余的值显示出来?我已经对它进行了测试,ob[]保存了需要输入组合框的正确记录。我想可能是selectschool.addItem不正常工作。 非常感谢,谢谢

道·格特尔学校;只需从sql中检索数据库记录,您需要创建JComboBox并向其添加ActionListener。不创建ActionPerformed并在设置JComboBox的内部

试着这样做:

// 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);
  }
});
编辑:或者在一个完整、非常简单但有效的示例中:

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);
    }
}

我仍然不知道你的意思,我试图复制你的代码,但仍然无法使它工作。我还需要添加ActionListener作为新方法,或者将其放在ActionPerformed中。如果我没有在ActionPerformed中设置JcomboBox,那么我会把它放在哪里呢?在公共布尔init中创建JcomboBox,或者在JFrame或JWindow的构造函数中创建JcomboBox。然后,在该init方法中,使用comboBox.AddActionListener添加ActionListener,就像我所做的那样。ActionListener是一个抽象类,因此必须为它实现actionPerformedActionEvent。无论何时现在对JComboBox执行操作,都会调用添加到JComboBox的ActionListener的actionPerformedActionEvent方法。