首页 > Display JComboBox items 遇到问题

Display JComboBox items 遇到问题

我有两个 JcomboBox(如图),items都是从MySQL拿的,分别是在 title 和 date 桌子。
我目前的code就是当 Select 的 comboBox item 选后,它才会 display date (Date JComboBox)。有什么方法可以让它一开始就自动display date 而不是要我选了Angry Birds 之后才 display date 吗 ? 谢谢。

public class BuyTicket {
    
    static JFrame frame;
    JLabel title,lblMainDate,selectMovie,dateOfShow;
    JComboBox Select,Date;
    
    public JPanel createContentPane() throws IOException
    {
        Select = new JComboBox();
        Select.setLocation(115,90);
        Select.setSize(175, 20);
        try {    
            DatabaseConnection db=new DatabaseConnection();
            Connection connect=db.getConnection();
            String sql="Select title FROM movie";
            PreparedStatement ps=connect.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
            String name = rs.getString("title");
            Select.addItem(name);         
        }
                    
    } catch (Exception e) {
        e.printStackTrace();
    }
         
        
        Select.addActionListener(new ActionListener()
         {
     public void actionPerformed(ActionEvent event)
         {
          JComboBox comboBox=(JComboBox) event.getSource();
         Object selected = Select.getSelectedItem();
         displayDate(selected);
         }
         });
    
    }
    
    private void displayDate(Object selected) {
        // TODO Auto-generated method stub
          try {    
                Date.removeAllItems();
                DatabaseConnection db=new DatabaseConnection();
                Connection connect=db.getConnection();
                String sql="Select date FROM movie WHERE title = ?";
                PreparedStatement ps=connect.prepareStatement(sql);
                ps.setObject(1, selected);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                String date1 = rs.getString("date");
                DefaultComboBoxModel model = (DefaultComboBoxModel)Date.getModel();
                if (model.getIndexOf(date1) == -1)
                {
                    Date.addItem(date1);
                }
                 
            }
                        
        } catch (Exception e) {
            System.out.println("null");
        }
    
        
    }
    
}

Edited

public class BuyTicket {

    static JFrame frame;
    JLabel title,lblMainDate,selectMovie,dateOfShow;
    JComboBox Select,Date;

    public JPanel createContentPane() throws IOException
    {
        Select = new JComboBox();
        Select.setLocation(115,90);
        Select.setSize(175, 20);
        try {   
            DatabaseConnection db=new DatabaseConnection();
            Connection connect=db.getConnection();
            String sql="Select title FROM movie";
            PreparedStatement ps=connect.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
            String name = rs.getString("title");
            Select.addItem(name);         
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

        Select.addActionListener(new ActionListener()
        {
    public void actionPerformed(ActionEvent event)
        {
         JComboBox comboBox=(JComboBox) event.getSource();
         Object selected = Select.getSelectedItem();
         displayDate(selected);
        }
        });

        String getMovie = (String)Select.getSelectedItem(); // New code added, directly display the Date JComboBox items 
        System.out.println(getMovie);
        displayDate(getMovie);
        Date = new JComboBox();
        Date.setLocation(115,140);
        Date.setSize(175, 20);
    }

    private void displayDate(Object selected) {
        // TODO Auto-generated method stub
          try { 
                Date.removeAllItems();
                DatabaseConnection db=new DatabaseConnection();
                Connection connect=db.getConnection();
                String sql="Select date FROM movie WHERE title = ?";
                PreparedStatement ps=connect.prepareStatement(sql);
                ps.setObject(1, selected);
                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                String date1 = rs.getString("date");
                DefaultComboBoxModel model = (DefaultComboBoxModel)Date.getModel();
                if (model.getIndexOf(date1) == -1)
                {
                    Date.addItem(date1);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

Latest Output

Angry Bird
java.lang.NullPointerException
    at gui.BuyTicket.displayDate(BuyTicket.java:131)
    at gui.BuyTicket.createContentPane(BuyTicket.java:87)
    at gui.BuyTicket.createAndShowGUI(BuyTicket.java:115)
    at gui.HomePage$2.mouseClicked(HomePage.java:151)
    at java.awt.Component.processMouseEvent(Unknown Source)

在初始化Listener后直接设置列表的第一项以触发一次监听器。

没有看具体的API如何操作,但大体是这个意思。

【热门文章】
【热门文章】