我的下拉列表显示了表单中的几个不正确的元素... image 我有一条错误消息。 “at presentation.JDAlbuInsert.fillComponents(JDAlbuInsert.java:146)” 这是一行:
private void fillComponents()
{
ArrayList <Chanteur> cha = daoCha.selectChanteurs();
for (int i=0;i<cha.size();i++)
jComboCha.addItem(cha.get(i));
}
所以,我的问题是在我的文件JifChanteur我hava这... 也许那个问题就是DaoCha ????你有什么想法吗?
public class JIFChanteurs extends javax.swing.JInternalFrame {
/** Creates new form FenetreAppareils2 */
public JIFChanteurs() {
((BasicInternalFrameUI) getUI()).setNorthPane(null);
initComponents();
fillComponents();
}
private void fillComponents()
{
/* charger toutes les cat�gories */
/*
ArrayList<Categorie> listeCats = daoCat.selectCategories();
listeCats.add(0, new Categorie(0,"*")); // ajoute une cat bidon : 0 signifie toutes les cat
for (Categorie cat : listeCats) {
jComboBoxCat.addItem(cat); // on ins�re des objets dans les combo, mais ils sont affich�s gr�ce � leurs m�thodes toString() !
}*/
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTableCha = new javax.swing.JTable();
setBorder(null);
setTitle("Instruments");
setVisible(true);
jTableCha.setModel(myChaModel);
jTableCha.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
/* initialisation des largeurs de colonnes */
javax.swing.table.TableColumn column = null;
column = jTableCha.getColumnModel().getColumn(0);
column.setPreferredWidth(50);
column = jTableCha.getColumnModel().getColumn(1);
column.setPreferredWidth(150);
column = jTableCha.getColumnModel().getColumn(2);
column.setPreferredWidth(150);
column = jTableCha.getColumnModel().getColumn(3);
column.setPreferredWidth(150);
column = jTableCha.getColumnModel().getColumn(4);
column.setPreferredWidth(150);
column.setPreferredWidth(50);
/* jTableCha.setRowSorter(new TableRowSorter(myModel)); */
jScrollPane1.setViewportView(jTableCha);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
.add(28, 28, 28))
);
pack();
}// </editor-fold>
private void refreshModel()
{
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTableCha;
// End of variables declaration
private DAOChanteurs daoCha = Factory.getDAOChanteurs();;
private TableModelChanteurs myChaModel = new TableModelChanteurs(daoCha.selectChanteurs());;
}
我的班级JDAAlbIns
public class JDAlbuInsert extends javax.swing.JDialog {
/** Creates new form JDApparInsert */
public JDAlbuInsert(java.awt.Frame parent, String titre) {
super(parent, titre, true);
initComponents();
fillComponents();
getRootPane().setDefaultButton(jButtonOK);
setLocation(parent.getX()+parent.getWidth()/3, parent.getY()+parent.getHeight()/3);
setVisible(true);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextIdent = new javax.swing.JTextField();
jTextTitre = new javax.swing.JTextField();
jComboCha = new javax.swing.JComboBox();
jDateChooserDate = new com.toedter.calendar.JDateChooser();
jPanel2 = new javax.swing.JPanel();
jButtonOK = new javax.swing.JButton();
jButtonAnnul = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText("Ident");
jLabel2.setText("Description");
jLabel3.setText("Date");
jLabel4.setText("Nom");
jDateChooserDate.setDateFormatString("dd-MM-yyyy");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1)
.add(jLabel3)
.add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(11, 11, 11)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jComboCha, 0, 200, Short.MAX_VALUE)
.add(jTextIdent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextTitre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
.add(jDateChooserDate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(126, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jTextIdent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(jTextTitre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jDateChooserDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel4)
.add(jComboCha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(185, 185, 185))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jButtonOK.setText("OK");
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOKActionPerformed(evt);
}
});
jPanel2.add(jButtonOK);
jButtonAnnul.setText("Annuler");
jButtonAnnul.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonAnnulActionPerformed(evt);
}
});
jPanel2.add(jButtonAnnul);
getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
private void jButtonAnnulActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Album alb = new Album();
alb.setCodeA(jTextIdent.getText().toUpperCase()); //setIdentApp
alb.setTitreA(jTextTitre.getText());
alb.setSortieA(jDateChooserDate.getDate());
alb.setCatApp((Chanteur)jComboCha.getSelectedItem());
boolean ok = daoAlb.insertAlbum(alb);
if (!ok)
JOptionPane.showMessageDialog(null,"Insertion impossible !","Avertissement",JOptionPane.ERROR_MESSAGE);
this.dispose();
}
private void fillComponents()
{
/* charger toutes les chanteurs */
ArrayList <Chanteur> cha = daoCha.selectChanteurs();
for (int i=0;i<cha.size();i++)
jComboCha.addItem(cha.get(i));
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonAnnul;
private javax.swing.JButton jButtonOK;
private javax.swing.JComboBox jComboCha;
private com.toedter.calendar.JDateChooser jDateChooserDate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextIdent;
private javax.swing.JTextField jTextTitre;
// End of variables declaration
private DAOAlbums daoAlb = Factory.getDAOAlbums();
private DAOChanteurs daoCha = Factory.getDAOChanteurs();
}
答案 0 :(得分:0)
鉴于提供澄清的图像,我可以看到comboBox试图为您提供对象Chanteur
的字符串表示。
这是因为你有
for (int i=0;i<cha.size();i++)
jComboCha.addItem(cha.get(i));
.get()
只返回对象,而不是字符串值,如果要返回对象的toString()
值,请将代码修改为:
for (int i=0;i<cha.size();i++)
jComboCha.addItem(cha.get(i).toString());
它应该有用。
注意:如果迭代像这样的ArrayList,那么使用for:each循环可能更好也许更简单。