IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

JTable LostFocus sur Cellule editable


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 24
    Par défaut JTable LostFocus sur Cellule editable
    Bonjour,

    Voila j'ai crée mon JTable(JTextField) avec 5 colonnes dont 2 editables.
    Dans la premiere colonne je saisis un numero article est ca rempli sur toute la ligne les differentes infos à la perte de focus: decriptif, quantite, prix unitaire et prix TT. Mais le pb c'est que je veux apres ce remplissage allez sur la cellule quantite qui est editable elle aussi. Je peux saisir la quantite mais je ne sais pas comment faire pour qu'au moment ou je tape entrer une action se passe pour remplir la cellule prix TT.

    Avez vous une solution?
    Merci pour votre aide.

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 24
    Par défaut
    C'est si difficile que ca les JTable!!lol
    C'est le 2ieme sujet que je proste sans reponse!!lol

  3. #3
    Membre expérimenté Avatar de Lethal
    Profil pro
    Développeur Java
    Inscrit en
    Février 2006
    Messages
    194
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Février 2006
    Messages : 194
    Par défaut
    Soit tu redéfinit un CellEditor pour ta table et tu ajoute un ActionListener dessus qui sera notifié quand tu appuies sur ENTER.

    Ou alors tu récupère le CellEditor par défaut et tu lui ajoute un CellEditor Listener.

    Par exemple si ta JFrame implémente CellEditorListener tu lui ajoute les méthodes
    public void editingStopped(ChangeEvent e) et public void editingCanceled(ChangeEvent e)

    donc:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class FrameTableEnter extends javax.swing.JFrame implements CellEditorListener
    {
     
        /** Creates new form FrameTableEnter */
        public FrameTableEnter()
        {
            initComponents();
           jTable1.getDefaultEditor(Object.class).addCellEditorListener(this);
        }
     
        public void editingStopped(ChangeEvent e)
        {
            System.out.println("édition de case finie");
        }
     
        public void editingCanceled(ChangeEvent e)
        {
     
        }
         ......
         ......

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    24
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 24
    Par défaut
    Le pb c'est que ca ne marche pas ca en fait des que je saisi ds ma 1iere colonne apres je ne peux faire aucune action sur la Colonne 2: elle est ediatble mais quand je modifie la quantite ou qu'ensuite je change de cellule il ne se passe rien .
    VOici mon code:
    Ici creation de la table:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class EcranCreationCommande extends JPanel implements ActionListener,MouseListener,CellEditorListener {
     
     
    		tm =new TableModel(lig,col);
    		table = new JTable ();
    		table.getTableHeader().setReorderingAllowed(false);
    		table.setModel(tm);
    		table.setDefaultEditor(Object.class, new MyDefaultCellEditor(new JTextField()));
    		scrollPane.setViewportView(table);
    		table.getDefaultEditor(Object.class).addCellEditorListener(this);
     
    		table.addMouseListener(this);
     
     
    	}
    ici création de ma table model:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    class TableModel extends AbstractTableModel {
    		private String[] columnNames = { "N°Article","Unité", "Descriptif Article",
    				 "Prix Unitaire TTC", "Prix Total" };
     
    		public TableModel(int r, int c) {
    			data = new ArrayList<List<Object>>();
    			for (int i = 0; i < r; i++) {
    				List<Object> l = new ArrayList<Object>();
    				for (int j = 0; j < c; j++)
    					l.add("");
    				data.add(l);
    			}
    		}
     
    		public void addRow(String arg0) {
    			l = new ArrayList<Object>();
    			l.add(arg0);
    			l.add(arg0);
    			l.add(arg0);
    			l.add(arg0);
    			l.add(arg0);
    			data.add(l);
    			fireTableDataChanged();
    		}
     
    		public void removeRow(int i) {
    			String temp = table.getValueAt(i, 0).toString();
    			String prix = table.getValueAt(i, colo + 4).toString();
    			if (table.getValueAt(i, colo + 4).toString() != "") {
    				prixTT -= Float.parseFloat(prix);
    				jlbTotalBord.setText((String.valueOf(prixTT)));
    			}
     
    			int temp2 = Integer.parseInt(temp);
    			for (int x = 0; x < liste.size(); x++) {
    				if (temp2 == liste.get(x)) {
    					liste.remove(x);
    				}
    			}
    			data.remove(i);
    			fireTableRowsDeleted(i,i);
    			fireTableDataChanged();
    		}
    		public void removeRow2() {
    			liste.remove(0);
    			data.remove(0);
    			fireTableRowsDeleted(0,0);
     
    		}
     
    		public String getColumnName(int arg0) {
    			return columnNames[arg0];
    		}
     
    		public int getColumnCount() {
    			return columnNames.length;
    		}
     
    		public int getRowCount() {
    			return data.size();
    		}
     
    		public Object getValueAt(int rowIndex, int columnIndex) {
    			return data.get(rowIndex).get(columnIndex);
    		}
     
    		public Class getColumnClass(int c) {
    			return getValueAt(0, c).getClass();
    		}
     
    		public void setValueAt(Object value, int row, int col) {
    			data.get(row).set(col, value);
    			fireTableCellUpdated(row, col);
    		}
     
    		public boolean isCellEditable(int row, int col) {
    			if ((col == 0 && data.get(row).get(col).toString().length() == 0)|| col == 1)
    				return true;
    			else
    				return false;
    		}
    	}
    Et la traitement de mes focus lost sur les colonnes editables:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    public class MyDefaultCellEditor extends DefaultCellEditor implements FocusListener  {
    	//	actionHandler = new CellEditorActionHandler();
     
    		public MyDefaultCellEditor(JTextField textField) {
    			super(textField);
    			setClickCountToStart(0);
    			editorComponent = textField;
    			editorComponent.setBorder(null);
    			editorComponent.addFocusListener(this);
    		}
     
    		public void focusGained(FocusEvent e) {
    			ligne = table.getSelectedRow();
    			colo = table.getSelectedColumn();
    		}
     
    		public void focusLost(FocusEvent e) {
    			pane= new JOptionPane();
    			String temp = table.getValueAt(ligne, colo).toString();
    			int etat = 0;
    			String quantite, Prix;
     
    			if(table.getSelectedColumn()==0){
    				if (temp.length() != 0) {
    					int temp2 = Integer.parseInt(temp);
    					for (int i = 0; i < liste.size(); i++) {
    						if (temp2 == liste.get(i)) {
    							pane.showMessageDialog(null, "Cet article a déjà été saisi","Avertissement",pane.WARNING_MESSAGE);
    							table.setValueAt("", ligne, 0);
    							etat = 1;
    						}
    					}
    					if (etat == 0) {
    						liste.add(temp2);
    						for (int i = 0; i < arts.getArticle().size(); i++) {
    							if (temp2 == arts.getArticle().get(i).getNumArt()&& arts.getArticle().get(i).getQuantiteArt()!=0) {
    								table.setValueAt(1, ligne, colo+1);
    								quantite= table.getValueAt(ligne, colo+1).toString();
     
    								table.setValueAt(arts.getArticle().get(i)
    										.getDescriptifArt(), ligne, colo + 2);
     
    								table.setValueAt(String.valueOf(arts.getArticle()
    										.get(i).getPrixCliArt()), ligne, colo + 3);
    								Prix=table.getValueAt(ligne, colo+3).toString();
     
    								/*prixTT = prixTT
    										+ (int) arts.getArticle().get(i).getPrixCliArt();
    								*/
    								prixHT=Float.parseFloat(quantite)*Float.parseFloat(Prix) ; 
    								prixTT= prixTT + prixHT;
    								table.setValueAt(String.valueOf(prixHT), ligne, colo + 4);
    								jlbTotalBord.setText((String.valueOf(prixTT)));
    								prixTTC=(float) (prixTT+(prixTT*0.195));
    								System.out.println("le prix est "+prixTTC);
    								jlbTotalTtcR.setText((String.valueOf(prixTTC)));
    								//table.removeEditor();
    							}
    						}
     
    						if (table.getValueAt(ligne, 2).toString().length()!=0){
    								tm.addRow("");	
    						}
    						else{ 
    							pane.showMessageDialog(null, "Cet article n'existe pas!",
    								      "Avertissement",pane.WARNING_MESSAGE);
    							table.setValueAt("", ligne, 0);	
    						}
    					}		
    				}
    			}
    		}
    	}
    Donc si une pers a une solution car la ca fait deux semaines que je suis decu et que je suis bloqué.
    Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [JTable] rendre une cellule non editable après edition
    Par Phil29 dans le forum Composants
    Réponses: 3
    Dernier message: 31/08/2010, 19h00
  2. Cellule non editable sur colonne editable
    Par FranT dans le forum Composants
    Réponses: 1
    Dernier message: 02/02/2010, 17h22
  3. faire des teste sur une cellule editable
    Par minanoun dans le forum Composants
    Réponses: 0
    Dernier message: 07/06/2009, 11h27
  4. cellule editable d'une JTable
    Par jayjay.f dans le forum Composants
    Réponses: 1
    Dernier message: 24/03/2007, 01h11
  5. [JTable] Cellule editable qui efface mon contenu
    Par Zanton dans le forum Composants
    Réponses: 4
    Dernier message: 01/06/2006, 12h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo