Comment chercher une ligne dand JTable

chat11 Messages postés 58 Date d'inscription lundi 10 septembre 2007 Statut Membre Dernière intervention 12 février 2011 - 10 sept. 2007 à 23:25
chat11 Messages postés 58 Date d'inscription lundi 10 septembre 2007 Statut Membre Dernière intervention 12 février 2011 - 14 sept. 2007 à 00:02
bonjour tt le monde ,


j'essaie de supprimer une ligne dans JTable ,sachant que mon programmme fais l'établissement de la connexion entre la base de données et le programme (jdbc),au 1er lieu.


merci pour votre aide.

4 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
11 sept. 2007 à 09:29
Salut,

Commence déjà par nous montrer ton code et dis nous ce qui ne marche pas, ca pourra aider pour savoir ce qui cloche.
______________________________________
DarK Sidious
0
chat11 Messages postés 58 Date d'inscription lundi 10 septembre 2007 Statut Membre Dernière intervention 12 février 2011
11 sept. 2007 à 21:09
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.sql.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
public class Articles extends JFrame {
 public Articles() {
  initComponents();
 }


 private void initComponents() {
  // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
  dialogPane = new JPanel();
  contentPanel = new JPanel();
  toolBar1 = new JToolBar();
  afficher = new JButton();
  modifier = new JButton();
  supprimer = new JButton();
  chercher = new JButton();
  actualiser = new JButton();
  label1 = new JLabel();
  tcode = new JTextField();
  valider = new JButton();
  label2 = new JLabel();
  tnom = new JTextField();
  annuler = new JButton();
  fermer = new JButton();
  scrollPane1 = new JScrollPane();
  table1 = new JTable();
  CellConstraints cc = new CellConstraints();


  //this
  setTitle("Articles");
  setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  Container contentPane = getContentPane();
  contentPane.setLayout(new BorderLayout());


  //dialogPane
  {
   dialogPane.setBorder(Borders.DIALOG_BORDER);
   dialogPane.setLayout(new BorderLayout());
      //contentPanel
   {
    contentPanel.setBackground(Color.pink);
    contentPanel.setFocusCycleRoot(true);
    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,
      new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
      new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
      new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
      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
     },
     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,
      FormFactory.LINE_GAP_ROWSPEC,
      FormFactory.DEFAULT_ROWSPEC,
      FormFactory.LINE_GAP_ROWSPEC,
      FormFactory.DEFAULT_ROWSPEC,
      FormFactory.LINE_GAP_ROWSPEC,
      new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
      FormFactory.LINE_GAP_ROWSPEC,
      new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
     }));
        //toolBar1
    {
     toolBar1.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
     
     //---- afficher ----
     afficher.setText("Afficher");
     afficher.setIcon(new ImageIcon("E:\\program java\\Livres\\icones\\edit.png"));
     afficher.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       afficherActionPerformed(e);
      }
     });
     toolBar1.add(afficher);
     
     //---- modifier ----
     modifier.setText("Modifier");
     modifier.setIcon(new ImageIcon("E:\\program java\\Livres\\icones\\mod.png"));
     modifier.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       modifierActionPerformed(e);
      }
     });
     toolBar1.add(modifier);
     
     //---- supprimer ----
     supprimer.setText("Supprimer");
     supprimer.setIcon(new ImageIcon("E:\\program java\\Livres\\icones\\FONT2B.png"));
     supprimer.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       supprimerActionPerformed(e);
      }
     });
     toolBar1.add(supprimer);
     
     //---- chercher ----
     chercher.setText("Chercher");
     chercher.setIcon(new ImageIcon("E:\\program java\\Livres\\icones\\cher.png"));
     chercher.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       chercherActionPerformed(e);
      }
     });
     toolBar1.add(chercher);
     
     //---- actualiser ----
     actualiser.setText("Actualiser");
     actualiser.setIcon(new ImageIcon("E:\\program java\\Livres\\icones\\affi.png"));
     actualiser.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
       actualiserActionPerformed(e);
      }
     });
     toolBar1.add(actualiser);
    }
    contentPanel.add(toolBar1, cc.xywh(3, 3, 18, 1));
    
    //---- label1 ----
    label1.setText("Code");
    contentPanel.add(label1, cc.xy(7, 5));
    
    //---- tcode ----
    tcode.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
    contentPanel.add(tcode, cc.xy(11, 5));
    
    //---- valider ----
    valider.setText("Valider");
    valider.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
      validerActionPerformed(e);
     }
    });
    contentPanel.add(valider, cc.xy(21, 5));
    
    //---- label2 ----
    label2.setText("Nom");
    contentPanel.add(label2, cc.xy(7, 7));
    
    //---- tnom ----
    tnom.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
    contentPanel.add(tnom, cc.xywh(11, 7, 3, 1));
    
    //---- annuler ----
    annuler.setText("Annuler");
    annuler.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
      annulerActionPerformed(e);
     }
    });
    contentPanel.add(annuler, cc.xy(21, 7));
    
    //---- fermer ----
    fermer.setText("Fermer");
    fermer.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
      fermerActionPerformed(e);
     }
    });
    contentPanel.add(fermer, cc.xy(21, 9));
        //scrollPane1
    {
     
     //---- table1 ----
    /* table1.setModel(new DefaultTableModel(
      new Object[][] {
      
      },*/
      model=new DefaultTableModel(
       new Object[][] {
       },
       
      new String[] {
       "codeA", "nomA"
      }
     ) {
      Class[] columnTypes = new Class[] {
       Integer.class, String.class
      };
      public Class getColumnClass(int columnIndex) {
       return columnTypes[columnIndex];
      }
     }
     ;
    table1.setModel(model);
     table1.setBackground(new Color(255, 255, 204));
     table1.setCellSelectionEnabled(true);
     table1.setFocusCycleRoot(true);
     table1.setFocusTraversalPolicyProvider(true);
     table1.setNextFocusableComponent(null);
     table1.setSurrendersFocusOnKeystroke(true);
     table1.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
       table1MouseClicked(e);
      }
     });
     scrollPane1.setViewportView(table1);
    }
    contentPanel.add(scrollPane1, cc.xywh(11, 11, 9, 3));
   }
   dialogPane.add(contentPanel, BorderLayout.CENTER);
  }
  contentPane.add(dialogPane, BorderLayout.CENTER);
  // JFormDesigner - End of component initialization  //GEN-END:initComponents
 }


 private void afficherActionPerformed(ActionEvent e) {
  
  // masqueaffiche();
  Connection con;
   ResultSet rs=null;
    Statement statement=null;
        Integer code;
        String nom ;
             
         //    initialiser_table();
          try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String s = "jdbc:odbc:article";
            String s1 = "";
            String s2 = "";
            con = DriverManager.getConnection(s, s1, s2);
            System.out.println("etablie");
            statement = con.createStatement();
        }
        catch(Exception exception)
        {
            this.dispose();
        }       
        try{
         rs=statement.executeQuery("select* from art  order by code");      
          while(rs.next())  {
           code=Integer.parseInt(rs.getString("code"));
              nom=rs.getString("nom");
            
              model.addRow(new Object[]{code,nom});
 
            
            }
        }
        catch(SQLException sqlexception)
        {
            System.out.println(sqlexception);
        }
  // TODO add your code here
  
 
 }
 private void initialiser_table(){
                 // model=null;
                table1.setModel(new DefaultTableModel(
       new Object[][] {
       },
       new String[] {
        "codeA", "nomA"
       }
      ) {
       Class[] columnTypes = new Class[] {
        Integer.class, String.class
       };
       public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
       }
      });
       table1.setModel(model);    
      }


 private void modifierActionPerformed(ActionEvent e) {
  // TODO add your code here
 }


 private void supprimerActionPerformed(ActionEvent e) {
  // TODO add your code here
 }


 private void chercherActionPerformed(ActionEvent e) {
  // TODO add your code here
 }
 


 private void actualiserActionPerformed(ActionEvent e) {
  // TODO add your code here
 }


 private void validerActionPerformed(ActionEvent e) {
  // TODO add your code here
 }


 private void annulerActionPerformed(ActionEvent e) {
  // TODO add your code here
  tcode.setText("");
  tnom.setText("");
 }


 private void fermerActionPerformed(ActionEvent e) {
 System.exit(0);
 }


 private void table1MouseClicked(MouseEvent e) {
  // TODO add your code here
 }




 private JPanel dialogPane;
 private JPanel contentPanel;
 private JToolBar toolBar1;
 private JButton afficher;
 private JButton modifier;
 private JButton supprimer;
 private JButton chercher;
 private JButton actualiser;
 private JLabel label1;
 private JTextField tcode;
 private JButton valider;
 private JLabel label2;
 private JTextField tnom;
 private JButton annuler;
 private JButton fermer;
 private JScrollPane scrollPane1;
 private JTable table1;
  private DefaultTableModel model ;
 public static void main(String args[]){
  Articles lot=new Articles();
  lot.setVisible(true);
  lot.pack();
 }
}


Voici mon programme, on réalité je veux supprimer, modifier et chercher une ligne, sachant que j'ai constitué la fenêtre à l'aide de JFormDesigner et ma base de données comporte le code et le nom d'article.
0
chat11 Messages postés 58 Date d'inscription lundi 10 septembre 2007 Statut Membre Dernière intervention 12 février 2011
12 sept. 2007 à 21:20
ce programme n'est pas executable sans  le package com ,
0
chat11 Messages postés 58 Date d'inscription lundi 10 septembre 2007 Statut Membre Dernière intervention 12 février 2011
14 sept. 2007 à 00:02
Mes questions sont difficile.si bien que personne ne veut pas me répondre.
0
Rejoignez-nous