Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec l'effacement d'une ligne de JTable

    27 janvier 2015 à 10:08:12

    Bonjour, s'il vous plait j'aimerais supprimer ma ligne de tableau mais j'arrive pas voilà mon code, je clic sur un bouton pour enlever un produit de ma liste JTable " tableau"

     if(event.getSource() == Enl){
    	        	JComboBox<Object[]> comboBox = new JComboBox<Object[]>();
    	        	Object[] data = new Object[tableau.getSize().height];
    	        	for (int i = 0; i < tableau.getRowCount();i++) 
    	        	{
    	        	data[i] = tableau.getValueAt(i,0);
    	        	comboBox.addItem(data);
    	        	}
    	        	int name = (int) JOptionPane.showInputDialog(
    	        			null,
    	        			"Veuillez indiquer le produit à enlever !",
    	        			"Stock",
    	        			JOptionPane.QUESTION_MESSAGE,	
    	        			null,data,data[0]);
    	        	
    	        	if(name == JOptionPane. OK_OPTION)
    	        	{
    	        	int rep = JOptionPane.showConfirmDialog(null,
    	        			"Voulez-vous supprimer le produit " + name + " du Stock ?",
    	        			"Suppression Produit",
    	        			JOptionPane. YES_NO_OPTION);
    	        			if ( rep == JOptionPane. YES_OPTION)
    	        			{
    	        				((DefaultTableModel) tableau.getModel()).removeRow(comboBox.getSelectedItem());
    	        				
    	        			}
    	        			if ( rep == JOptionPane. NO_OPTION) 
    	        			{
    	        				//fermer la fênetre de dialogue	
    	        			}
    	        	}
    	        	if(name == JOptionPane. OK_OPTION)
    	        	{
    	        	//	fermer la fênetre de dialogue
    	        	}


    ici entre crocher j'aimerais supprimer la ligne avec le produit sélection dans la fenêtre de dialogue voilà une photo pour mieux comprendre 


    • Partager sur Facebook
    • Partager sur Twitter
      27 janvier 2015 à 11:09:37

      tu utilises une BDD ou ta liste est écrit en dur ?

      • Partager sur Facebook
      • Partager sur Twitter
      Un homme azerty en vaut deux.
        27 janvier 2015 à 11:21:06

        ah non je l'ai écris en dure mais j'ajoute a chaque fois avec un bouton ajouter 

        -
        Edité par btuning23 27 janvier 2015 à 11:46:23

        • Partager sur Facebook
        • Partager sur Twitter
          27 janvier 2015 à 11:51:45

          Donc ta liste est en dure ok

          if(name == JOptionPane.OK_OPTION) {
              int rep = JOptionPane.showConfirmDialog(null,"Voulez-vous supprimer le produit " + name + " du Stock ?",
              "Suppression Produit", JOptionPane. YES_NO_OPTION);
              if ( rep == JOptionPane. YES_OPTION) {
                  DefaultTableModel model = (DefaultTableModel) tableau.getModel();
                  
                  for(i=0; i<=tableau.length; i++) {
                      
                      if(tableau[i]==name){
                        model.removeRow(i);
                        break;
                      }
                  }
              }
              if ( rep == JOptionPane.NO_OPTION) {
              //fermer la fênetre de dialogue 
              }
          }



          • Partager sur Facebook
          • Partager sur Twitter
          Un homme azerty en vaut deux.
            27 janvier 2015 à 13:39:11

            Merci , mais avec tableau.lenght ça marche pas je ne sais pas pourquoi il reconnait pas
            • Partager sur Facebook
            • Partager sur Twitter
              27 janvier 2015 à 14:49:28

              Moui il y a des fois quelque soucis pour le régler il faut faire :

              for(i=0; i<=(tableau.length-1); i++)

              mais comme tu peut le constater

              Á moins que tu as fais un tableau à plusieurs dimension ou là le .length ne marche pas si c'est le cas montrer moi la création de tableau et je te ferais le code

              -
              Edité par GuillaumeBo1 27 janvier 2015 à 15:20:49

              • Partager sur Facebook
              • Partager sur Twitter
              Un homme azerty en vaut deux.
                27 janvier 2015 à 16:19:21

                Merci beaucoup voilà mon code : 

                private String[]  columnName = {"Produit", "Description", "Prix d'achat","Prix de vente"};
                		private Object[][] dataModel = {
                				  {"Stylos", "Noir,Rouge,Bleu", "1.32","2.45"},
                				  {"Cahiers", "Cahiers d'exercices", "2.45","4.89"}};
                		Model model = new Model();
                		public JTable tableau = new JTable(model);
                		private JScrollPane barres_defilement = new JScrollPane(tableau);
                
                		class Model extends AbstractTableModel {
                			private static final long serialVersionUID = 1L;
                			public int getColumnCount() {
                	            return columnName.length;
                	        }
                	        public int getRowCount() {
                	            return dataModel.length;
                	        }
                	        public Object getValueAt(int arg0, int arg1) {
                	            return dataModel[arg0][arg1];
                	        }
                	        public String getColumnName(int column) {
                	            return columnName[column];
                	        }
                	        public void modifLigne(Object[] data) {
                	            int indice = tableau.getSelectedRow();
                	            if (indice >= 0 && indice < dataModel.length) {
                	                dataModel[indice] = data;
                	                this.fireTableDataChanged();
                	            }
                	        }
                	        public void addRow(Object[] data) {
                	            dataModel = Arrays.copyOf(dataModel, dataModel.length + 1);
                	            dataModel[dataModel.length - 1] = data;
                	            this.fireTableDataChanged();
                	        }
                       /*     public void removeRow(int index)
                            {
                              for (int i = 0; i < obj.size(); i++) {
                                objcol = (Vector<Object>)obj.get(i);
                                objcol.remove(index);
                              }
                            }*/
                	        public void removeSelectedRows(JTable table){
                	        	   DefaultTableModel model = (DefaultTableModel) tableau.getModel();
                	        	   int[] rows = table.getSelectedRows();
                	        	   for(int i=0;i<rows.length;i++){
                	        	     model.removeRow(rows[i]-i);
                	        	   }
                	        	}
                	        public void removeProduit(int rowIndex){
                		    	tableau.remove(rowIndex);
                		    	fireTableRowsDeleted(rowIndex, rowIndex);
                		    }
                
                	    }
                
                public void actionPerformed(ActionEvent event) {
                	       if (event.getSource() == Ajt) {
                	            Gestionno2 ajout = new Gestionno2(null,null, null, null, null, this.model);
                	            ajout.setVisible(true);
                	            ajout.setMinimumSize(new Dimension(400, 100));
                	        }
                	        if (event.getSource() == Mod) {
                	            int row = this.tableau.getSelectedRow();
                	            if (row == -1) {
                	                return;
                	            }
                	            JFrame corps = null ;
                	            String nom = (String) tableau.getValueAt(row, 0);
                	            String description = (String) tableau.getValueAt(row, 1);
                	            String PrixAchat = (String) tableau.getValueAt(row, 2);
                	            String PrixVente = (String) tableau.getValueAt(row, 3);	            
                	            Gestionno2 ajout = new Gestionno2(corps,nom, description,PrixAchat,PrixVente, this.model);
                	            ajout.setVisible(true);
                	            ajout.setMinimumSize(new Dimension(400, 100));
                	        }
                	        if(event.getSource() == Enl){
                	        	JComboBox<Object[]> comboBox = new JComboBox<Object[]>();
                	        	Object[] data = new Object[tableau.getSize().height];
                	        	for (int i = 0; i < tableau.getRowCount();i++) 
                	        	{
                	        	data[i] = tableau.getValueAt(i,0);
                	        	comboBox.addItem(data);
                	        	}
                	        	int name = (int) JOptionPane.showInputDialog(
                	        			null,
                	        			"Veuillez indiquer le produit à enlever !",
                	        			"Stock",
                	        			JOptionPane.QUESTION_MESSAGE,	
                	        			null,data,data[0]);
                	        	
                	        	if(name == JOptionPane. OK_OPTION)
                	        	{
                	        	int rep = JOptionPane.showConfirmDialog(null,
                	        			"Voulez-vous supprimer le produit " + name + " du Stock ?",
                	        			"Suppression Produit",
                	        			JOptionPane. YES_NO_OPTION);
                	        			if ( rep == JOptionPane. YES_OPTION)
                	        			{
                	        	//			((DefaultTableModel) tableau.getModel()).removeRow(comboBox.getSelectedItem());
                	        			DefaultTableModel model = (DefaultTableModel) tableau.getModel();
                	        		         
                	        	/*	     for(int i=0; i<=tableau.getRowCount(); i++) {
                	        		             
                	        		  if(tableau[i]==name){
                	        		              model.removeRow(i);
                	        		              break;
                	        		            }
                	        		        }*/
                	        				
                	        			}
                	        			if ( rep == JOptionPane. NO_OPTION) 
                	        			{
                	        				//fermer la fênetre de dialogue	
                	        			}
                	        	}
                	        	else if(name == JOptionPane. CANCEL_OPTION)
                	        	{
                	        	//	fermer la fênetre de dialogue
                	        	}
                		public static void main(String[] args){
                			SwingUtilities.invokeLater(new Runnable(){
                				public void run(){
                					Gestionno tableauDesInvariants = new Gestionno("StokGestion");
                					tableauDesInvariants.setVisible(true);
                				}
                			});
                		}

                et la c'est ma classe pour ajouter un nouveau produit 

                  public Gestionno2(JFrame corps,String nom, String description,String PrixAchat, String PrixVente, Gestionno.Model tableModel) {
                        super(corps, "Ajouter un Produit",true );
                        miseEnPlace();
                        nomLabel.setText(nom);
                        DescriptionLabel.setText(description);
                        PrixAchatLabel.setText(PrixAchat);
                        PrixVenteLabel.setText(PrixVente);
                        this.tableModel = tableModel;
                        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                    }
                	@Override
                	public void actionPerformed(ActionEvent e) {
                		// TODO Auto-generated method stub
                        if (e.getActionCommand() == "OK" ) {
                            Object[] data = { nom.getText(), Description.getText(), PrixAchat.getText(),PrixVente.getText()};
                            if (nomLabel.getText() == null && DescriptionLabel.getText() == null && this.PrixAchatLabel != null && this.PrixVenteLabel != null) {
                                ((Gestionno.Model) tableModel).addRow(data);
                            } else {
                                ((Gestionno.Model) tableModel).modifLigne(data);
                            }
                            dispose();
                        }
                        if (e.getActionCommand() == "Annuler" ) { this.dispose();}
                		
                	}
                
                }
                Pour mieux comprendre ce que je suis entrain de faire 




                -
                Edité par btuning23 27 janvier 2015 à 18:05:52

                • Partager sur Facebook
                • Partager sur Twitter

                Problème avec l'effacement d'une ligne de JTable

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown