voici mon code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import java.sql.*;
import java.util.Vector;
public class lote extends JFrame {
public lote() {
initComponents();
}
private void initComponents() {
dialogPane = new JPanel();
contentPanel = new JPanel();
texttitre = new JComboBox(vect);
scrollPane1 = new JScrollPane();
listModel = new DefaultListModel();
list1 = new JList(listModel);
buttonBar = new JPanel();
okButton = new JButton();
cancelButton = new JButton();
CellConstraints cc = new CellConstraints();
//======== this ========
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(Borders.DIALOG_BORDER);
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setLayout(new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC
},
new RowSpec[] {
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC
}));
//---- texttitre ----
texttitre.setEditable(true);
texttitre.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
comboBox7ItemStateChanged(e);
}
});
contentPanel.add(texttitre, cc.xywh(3, 1, 19, 1));
//======== scrollPane1 ========
{
scrollPane1.setViewportView(list1);
}
contentPanel.add(scrollPane1, cc.xywh(3, 5, 19, 1));
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
buttonBar.setLayout(new FormLayout(
new ColumnSpec[] {
FormFactory.GLUE_COLSPEC,
FormFactory.BUTTON_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.BUTTON_COLSPEC
},
RowSpec.decodeSpecs("pref")));
//---- okButton ----
okButton.setText("OK");
buttonBar.add(okButton, cc.xy(2, 1));
//---- cancelButton ----
cancelButton.setText("Cancel");
buttonBar.add(cancelButton, cc.xy(4, 1));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
}
private void comboBox7ItemStateChanged(ItemEvent e) {
vect.clear();
troverliste();
texttitre.showPopup();
if(e.getStateChange() == ItemEvent.SELECTED){
listModel.addElement(texttitre.getSelectedItem());
}
}
private void troverliste(){
Connection con;
ResultSet rs=null;
java.sql.Statement statement=null;
String titre ;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String s = "jdbc:odbc:bd1";
String s1 = "";
String s2 = "";
con = DriverManager.getConnection(s, s1, s2);
statement = con.createStatement();
}
catch(Exception exception)
{
}
try{
//ma requête
rs=statement.executeQuery("select distinct Titredulivre from ANNONCES where Titredulivre LIKE '"+texttitre.getSelectedItem().toString() + "%' order by Titredulivre" ); // *where titre like ' "+(String)texttitre.getSelectedItem()+"*'" */
while(rs.next()) {
titre=rs.getString("Titredulivre");
vect.addElement(titre);
}
}
catch(SQLException sqlexception)
{
System.out.println(sqlexception);
}
}
private JPanel dialogPane;
private JPanel contentPanel;
private JComboBox texttitre;
private JScrollPane scrollPane1;
DefaultListModel listModel;
private JList list1;
private JPanel buttonBar;
private JButton okButton;
private JButton cancelButton;
//main
public static void main(String args[]){
lote fen = new lote();
fen.setVisible(true);
fen.pack();}
}
il est long car j'ai construis ma fenêtre à l'aide de JFormDeseign .j'ai bien établies la connexion avec la BD et aussi lorsque je tape une lettre dans le combobox j'obtiens les livres qui commence par cette lettre ,mais listModel.addElement(texttitre.getSelectedItem()); ajoute la lettre dans la liste.
merci.