简体   繁体   English

基于JCombobox Java上的selectedItem的JFrame

[英]JFrame based on selectedItem on JCombobox Java

I just started to develop in Java, so I have a newbie question, I have a Jcombobx that's getting filled from my database using this function: 我刚刚开始用Java开发,所以我有一个新手问题,我有一个Jcombobx,正在使用此函数从数据库中填充它:

private void fill(){
   try{
       con = DriverManager.getConnection("jdbc:....");
       Statement pst = con.createStatement();
       String sql = "select * from student";
       ResultSet rs = pst.executeQuery(sql);
       while(rs.next()){
            jCombo.addItem(rs.getString("strudent_name");
       }
   } catch (Exception e){
      System.out.println(e.getMessage);
 }

The code is working and the combo is filled from my database, the next step is to fill another JFrame from my database based on the selectedItem in the combobox, when I was developping in PHP I know that was easier because I only had to put the id in the select value and the name in the displayed. 代码正在运行并且从数据库中填充了组合,下一步是基于组合框中的selectedItem从数据库中填充另一个JFrame,当我在PHP中进行开发时,我知道这会更容易,因为我只需要将选择值中的id和显示的名称。 In android I could send in via Intent but I don't know how to do in jaa, because the next JFrame that must be opened has to get the details about the selected student, using informations from the other tables. 在android中,我可以通过Intent发送,但我不知道如何在jaa中进行操作,因为必须打开的下一个JFrame必须使用其他表中的信息来获取有关所选学生的详细信息。 Thanks for helping. 感谢您的帮助。

First create a wrapper class for student 首先为学生创建一个包装器类

public class Student
{

    private int id;
    private String name;
    //more fields here like grades, subjects etc

    public Student(int id, String name)
    {
        super();
        this.id = id;
        this.name = name;
    }



    public int getId()
    {
        return this.id;
    }



    public void setId(int id)
    {
        this.id = id;
    }



    public String getName()
    {
        return this.name;
    }



    public void setName(String name)
    {
        this.name = name;
    }



    @Override
    public String toString()
    {
        return name;
    }


}

Then you declare JComboBox like this 然后像这样声明JComboBox

JComboBox<Student> jCombo = new JComboBox<>();

And then 接着

private void fill(){
   try{
       con = DriverManager.getConnection("jdbc:....");
       Statement pst = con.createStatement();
       String sql = "select * from student";
       ResultSet rs = pst.executeQuery(sql);
       while(rs.next()){
        Student student = new Student(rs.getInt("student_id") , rs.getString("student_name"));     
         jCombo.addItem(student);
       } // while end

   } catch (Exception e){
      System.out.println(e.getMessage);
 }

When some button is clicked you can get id of selected student by 单击某些按钮后,您可以通过以下方式获取所选学生的ID

showDetailsButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            Student selectedStudent = (Student) jCombo.getSelectedItem();
            new StudentDetails(selectedStudent.getId());

        }
    });

Where StudentDetails is a another JFrame 其中StudentDetails是另一个JFrame

class StudentDetails extends JFrame 
{
    public StudentDetails(int studentID)
    {
        // "Select * from student where student_id =" + studentID;
        super();
        add(new JLabel("Detail of student with id " + studentID + " goes here"));
        setVisible(true);
        setSize(100, 100);

    }
}

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

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