我正在使用 MySQL 服务器 5.6 来托管我用来管理零件编号记录的数据库。我有一个 Java 客户端前端。我的客户端和数据库之间的连接都很好。在创建部件的面板中,我有一个 JComboBox-typeComboBox,它通过查询数据库中所有可能的部件类型并在下拉菜单中显示它们的类型编号来填充。根据从该 JComboBox 中的选择,tdescripTextField 和 seqTextField 填充了适当的信息,但我无法让 JComboBox-matComboBox 由使用所选 PartType 编号查询的适当结果填充。
我使用了一些 System.out.println(); 调试发生问题的代码部分,据我所知,String[] mats 确实包含正确的信息,但它只是不会将其分配给 matComboBox。如果我不初始化 matComboBox = new JComboBox(); 在 typeComboBox 之后,我收到一个编译错误,指出 matComboBox 为空。
提前感谢您的帮助。
ps 我所有的查询方法都从数据库返回 JSONArrays。
class CreatePanel extends JPanel{
//JButtons
private JButton saveButton;
private JButton backButton;
//JComboBox
private JComboBox<?> typeComboBox;
private JComboBox<?> matComboBox;
//JTextField
private JTextField tdescripTextField;
private JTextField mdescripTextField;
private JTextField descripTextField;
private JTextField seqTextField;
private JTextField bpartTextField;
private JTextField cpartTextField;
private JTextField spartTextField;
//JLabel
private JLabel lblSeq;
private JLabel lblDescription;
private JLabel lblMatterialDescription;
private JLabel lblTypeDescription;
private JLabel lblType;
private JLabel lblMatterial;
private JLabel lblBosalPartNumber;
private JLabel lblCustomerPartNumber;
private JLabel lblSupplierPartNumber;
private JLabel lblCreateAPart;
private JLabel Bosal;
JPanel contentPane;
//StringPanel
public CreatePanel(final JPanel create)
{
//TextFields
tdescripTextField = new JTextField();
tdescripTextField.setEditable(false);
mdescripTextField = new JTextField();
mdescripTextField.setEditable(false);
descripTextField = new JTextField();
seqTextField = new JTextField();
seqTextField.setEditable(false);
bpartTextField = new JTextField();
bpartTextField.setEditable(false);
cpartTextField = new JTextField();
spartTextField = new JTextField();
//ComboBoxes
typeComboBox = new JComboBox<Object>();
matComboBox = new JComboBox<Object>();
JSONArray temp1 = new JSONArray();
String[] types = null;
try{
temp1 = con.queryReturnAllTypes();
types = new String[temp1.length()];
for(int i = 0; i < temp1.length(); i++){
types[i] = temp1.getJSONObject(i).get("PartType").toString();
}
}catch(Exception ex){/*ignore*/}
typeComboBox = new JComboBox<Object>(types);
typeComboBox.setEditable(true);
typeComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int partType = Integer.valueOf((String) typeComboBox.getSelectedItem());
/*Debug*/System.out.println(partType);
JSONArray temp1 = new JSONArray();
JSONArray temp2 = new JSONArray();
String typeDescrip = null;
String seqNum = null;
String[] mats = null;
try{
temp1 = con.queryPartType(partType);
/*Debug*/System.out.println(temp1);
temp2 = con.queryMaterialPartType(partType);
/*Debug*/System.out.println(temp2);
mats = new String[temp2.length()];
/*Debug*/System.out.println(temp2.length());
typeDescrip = temp1.getJSONObject(0).get("TypeDescription").toString();
tdescripTextField.setText(typeDescrip);
seqNum = temp1.getJSONObject(0).get("SeqNumber").toString();
seqTextField.setText(seqNum);
for(int i = 0; i < temp2.length(); i++){
/*Debug*/System.out.println(i);
/*Debug*/System.out.println(temp2.getJSONObject(i).get("Material").toString());
mats[i] = temp2.getJSONObject(i).get("Material").toString();
/*Debug*/System.out.println(mats[i]);
}
for(int i = 0; i<mats.length; i++){
/*Debug*/System.out.println(mats[i]);
}
}catch(Exception ex){/*ignore*/}
for(int i = 0; i<mats.length; i++){
/*Debug*/System.out.println(mats[i]);
}
matComboBox = new JComboBox<Object>(mats);
matComboBox.setEditable(true);
}
});
//Labels
lblType = new JLabel("Type");
lblMatterial = new JLabel("Material");
lblTypeDescription = new JLabel("Type Description");
lblMatterialDescription = new JLabel("Material Description");
lblSeq = new JLabel("Seq");
lblDescription = new JLabel("Description");
lblBosalPartNumber = new JLabel("Bosal Part Number");
lblCustomerPartNumber = new JLabel("Customer Part Number");
lblSupplierPartNumber = new JLabel("Supplier Part Number");
lblCreateAPart = new JLabel("Create a Part Number");
ImageIcon bosal = new ImageIcon(getClass().getResource("/Images/bosal.jpg"));
Bosal = new JLabel(bosal);
setBackground(Color.DARK_GRAY);
//Buttons
ImageIcon save = new ImageIcon(getClass().getResource("/images/save1.jpg"));
saveButton = new JButton(save);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton){
int n = JOptionPane.showConfirmDialog(
frame,
"Are you sure you want to save part data?",
"Save:",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
}}});
add(saveButton);
ImageIcon back = new ImageIcon(getClass().getResource("/images/back1.jpg"));
backButton = new JButton(back);
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == backButton)
{
setVisible(false);
frame.setLocation(550,220);
frame.setSize(700, 580);
main.setVisible(true);
}}});
add(backButton);
setupPanel();
};
private void setupPanel()
{
//Label Fonts
lblType.setFont(new Font("Tahoma", Font.BOLD, 14));
lblType.setForeground(Color.BLACK);
lblMatterial.setFont(new Font("Tahoma", Font.BOLD, 14));
lblMatterial.setForeground(Color.BLACK);
lblTypeDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblTypeDescription.setForeground(Color.BLACK);
lblMatterialDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblMatterialDescription.setForeground(Color.BLACK);
lblDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
lblDescription.setForeground(Color.BLACK);
lblSeq.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSeq.setForeground(Color.BLACK);
lblSupplierPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSupplierPartNumber.setForeground(Color.BLACK);
lblCreateAPart.setFont(new Font("EucrosiaUPC", Font.BOLD, 64));
lblCreateAPart.setForeground(Color.BLACK);
lblCustomerPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblCustomerPartNumber.setForeground(Color.BLACK);
lblBosalPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
lblBosalPartNumber.setForeground(Color.BLACK);
//Group Layout
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(24)
.addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
.addGap(6)
.addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 434, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblType)
.addGap(121)
.addComponent(lblTypeDescription)
.addGap(163)
.addComponent(lblBosalPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addGap(76)
.addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblMatterial)
.addGap(101)
.addComponent(lblMatterialDescription)
.addGap(143)
.addComponent(lblCustomerPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addGap(76)
.addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(lblSeq)
.addGap(129)
.addComponent(lblDescription)
.addGap(201)
.addComponent(lblSupplierPartNumber))
.addGroup(groupLayout.createSequentialGroup()
.addGap(331)
.addComponent(backButton, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
.addGap(41)
.addComponent(saveButton, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(50)
.addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
.addGap(97)
.addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(67)
.addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(36, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(32)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
.addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblType)
.addComponent(lblTypeDescription)
.addComponent(lblBosalPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGap(3)
.addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(3)
.addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblMatterial)
.addComponent(lblMatterialDescription)
.addComponent(lblCustomerPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(9)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblSeq)
.addComponent(lblDescription)
.addComponent(lblSupplierPartNumber))
.addGap(6)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(saveButton, 0, 0, Short.MAX_VALUE)
.addComponent(backButton, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
.addContainerGap(135, Short.MAX_VALUE))
);
setLayout(groupLayout);
}}