繁体   English   中英

使 jcombobox 中的项目可见

[英]Making items in jcombobox visible

我正在尝试制作一个组合框,根据我的数据库填充里面的项目。 目前数据库包含 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 并且只选择 test 1。我怎样才能显示其余的值? 我已经对其进行了测试,并且 ob[] 保存了需要输入到组合框中的正确记录。 我认为这可能是 selectschool.addItem() 无法正常工作。 非常感谢,谢谢。

(DAO.getAllSchool(); 只是从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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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