【发布时间】:2016-06-26 14:51:55
【问题描述】:
在 GUI 中,我有两个 JComboBox,其中所有的组合框项目都是从两个 tables 检索的,它们是 title 和 time。 In my current code, when the title is selected, displayDate(selected); will get called since I have implemented addActionListener in Select comboBox.
但这不是我想要的。运行此文件时,我希望它立即根据 Select JcomboBox 中的第一项显示日期。 When the Select comboBox item changed, only the date changed.正确的写作方式是什么?
public class BuyTicket {
static JFrame frame;
JLabel title,lblMainDate,selectMovie,dateOfShow;
JComboBox Select,Date;
public JPanel createContentPane() throws IOException
{
title = new JLabel("CYBER CINEMA");
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) {
System.out.println("null");
}
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");
}
}
});
}
已编辑
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();
}
}
}
我添加了一些代码,但日期仍然无法直接显示,并且出现nullpointer 错误。
最新输出
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)
【问题讨论】:
标签: java mysql swing nullpointerexception actionlistener