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 :

Action sur un JTable


Sujet :

Composants Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut Action sur un JTable
    Bonjour ,

    je cherche à effectuer une action différente selon la ligne (ou la case) cliqué sur mon Jtable. Pour l'instant je ne peux que'effectuer une action pour un clic sur l'ensemble du tableau.
    Merci

  2. #2
    Membre confirmé Avatar de schniouf
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    382
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Décembre 2003
    Messages : 382
    Points : 474
    Points
    474
    Par défaut
    Utilise l'interface ListSelectionListener :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    	JTable t = new JTable() ;
    	t.getSelectionModel().addListSelectionListener( new MonListener() ) ;
     
    	public class MonListener implements ListSelectionListener
    	{
    		public void valueChanged(ListSelectionEvent e) {
    			int ligneSelectionne = t.getSelectedRow() ;
    			System.out.println("Ligne sélectionnée : " + ligneSelectionne );
    		}
    	}
    Seul soucis, quand on fait un clic sur une ligne, la méthode valueChanged est appelée 2 fois, une fois à l'enfoncement du bouton de la souris et une fois à son relâchement... quelqu'un sait pourquoi ? Sinon le changement de ligne avec les flèches du clavier marche très bien.
    En espérant que ça t'aidera

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Mille merci , je reviendrai te dire ce qu'il en ai.

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    il me renvoi -1 sur la première ligne , rofl
    et vrai l'action se fait 2 fois , quelqu'un à une idée ?

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Voilà mon code
    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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    package Carte;
    import java.awt.GridLayout;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.TableCellRenderer;
     
    import Utilitaires.DataBase;
     
    public class Serveurs extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	String NomServeur;
    	Vector <String> LettreDisque;
    	Vector <Double> tailleMax;
    	Vector <Boolean> unite;
    	int pourcentage;
    	int x = 0;
    	int y = 20;
            JScrollPane pan;
            JTable tabler;
    	DataBase base;
            ResultSet resultat;
            CompTableCellRenderer composant = new CompTableCellRenderer();
            Thread tache;
            JPanel serv = this;
            boolean test = true;
            double taille;
     
     
     
            public void run(){
                while(test){
                    try{
                        this.lancer();
                        this.updateUI();
                        Thread.sleep(60000);
                    }
                    catch(Exception ecx){
                    System.out.println("Erreur de sleep de SErveur"+ecx.fillInStackTrace());}
                }
            }
     
     
            public void stopper(){
                test = false;
            }
     
     
     
     
            public void lancer(){
                try{
                	String req="select id from appareils where nom = '"+NomServeur+"'";
                	System.out.println(req);
    		base = new DataBase(req);
    		base.readBDD();
    		resultat = base.retourner();
    		if(resultat.next() && resultat.next()){
    			String id = resultat.getString("id");
                String[] nom = {"Disque","Espace Libre","Espace Libre","Etat"}; 
     
                LettreDisque = new Vector<String>();
                tailleMax = new Vector<Double>();
                unite = new Vector<Boolean>();
     
                String req1="select lettre_disque,taille from el_disques where superviser = 1 and nom = '"+NomServeur+"' order by lettre_disque";
                System.out.println(req1);
                base.request=req1;
                base.readBDD();
                resultat = base.retourner();
                while(resultat.next()){
                    LettreDisque.addElement(resultat.getString("lettre_disque"));
                    tailleMax.addElement(resultat.getDouble("taille"));
                }
                base.fermer();
                Object[][] data = new Object[LettreDisque.size()][nom.length];
     
     
                JTable table = new JTable(new Modele(data,nom));
                table.setRowHeight(25);
                table.setShowGrid(false);
                new ModeleListener(table);
                pan = new JScrollPane(table);
                for(int i = 0;i < LettreDisque.size();i++){
                	String req2="select probleme,id from erreurs where nom = '"+NomServeur+"' and probleme = '"+LettreDisque.get(i)+"' and critique = 1 and id = '"+id+"'";
                	System.out.println(req2);
                        base = new DataBase(req2);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            table.setValueAt(new ImageIcon("images/iconefalse.GIF"),i,3);
                        }
                        else{
                            table.setValueAt(new ImageIcon("images/iconeOK.GIF"),i,3);
                        }
                        base.fermer();
                        taille = Double.parseDouble(tailleMax.get(i).toString());
                        JProgressBar bar = new JProgressBar(0,(int)taille);
                        System.out.println("taille barre"+taille);
                        String baf="select disques.utiliser from appareils,disques where appareils.id = disques.id and appareils.id = (select max(id) from appareils where nom = '"+NomServeur+"') and description = '"+LettreDisque.get(i).toString()+"' and disques.nom = '"+NomServeur+"' and appareils.id > '"+id+"' order by description";
                        System.out.println(baf);
                        base = new DataBase(baf);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            double utiliser = resultat.getDouble("utiliser");
                           System.out.println(utiliser+","+taille);
                            bar.setValue((int)(taille - utiliser));
                            bar.setString(Integer.toString(100*((int)(taille - utiliser))/(int)taille)+ " % ");
                            bar.setStringPainted(true);
                            table.setValueAt(" "+LettreDisque.get(i).toString(),i,0);
                            table.getColumnModel().getColumn(0).setCellRenderer(composant);
                            table.setValueAt(bar,i,1);
                            table.getColumnModel().getColumn(1).setCellRenderer(composant);
                            table.getColumnModel().getColumn(2).setCellRenderer(composant);
                            if(taille-utiliser < 1){
                                table.setValueAt(" " + Double.toString((taille-utiliser)*1024).substring(0,5)+" Mo/"+String.valueOf(taille*1024).substring(0, 5)+" Mo",i,2);
                                unite.addElement(false);
                            }
                            else{
                                table.setValueAt(" " + Double.toString((taille-utiliser)).substring(0,5)+" Go/"+String.valueOf(taille).substring(0, 5)+" Go",i,2);
                                unite.addElement(true);
                            }
     
    //                        for(int h=0;h<unite.size();h++)
    //                        	System.out.println(unite.elementAt(h));
                        }
                        base.fermer(); 
                    }
    		tabler = table;
                }
    		}
                catch(SQLException sqle){
                	System.out.println("Erreur SQL de la methode lancer");
                sqle.printStackTrace();}
            }
     
     
     
          public Serveurs(String Nom){
                this.NomServeur = Nom;
                this.lancer();
                add(pan);
                setLayout(new GridLayout(1,1));
     
                SelectionListener listener = new SelectionListener(tabler);
                tabler.getSelectionModel().addListSelectionListener(listener);
                tabler.getColumnModel().getSelectionModel().addListSelectionListener(listener);
     
                tabler.setRowSelectionAllowed(true);
                tabler.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            }
     
          class CompTableCellRenderer extends JPanel implements TableCellRenderer{ 
        	  private static final long serialVersionUID = 1L;
                private JProgressBar barre;
    	    public CompTableCellRenderer(){}  
     
     
    	    public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column){
                    if(column == 1){
                        barre = (JProgressBar)value;
                        return barre;
                    }
                    else{
                        return new JLabel(value.toString());
                    } 
                }
          }
     
     
    public class SelectionListener implements ListSelectionListener {
              JTable table;
     
              // It is necessary to keep the table since it is not possible
              // to determine the table from the event's source
              SelectionListener(JTable table) {
                  this.table = table;
              }
              public void valueChanged(ListSelectionEvent e) {
            	  int sel=-1;
                  // If cell selection is enabled, both row and column change events are fired
            	  if (e.getSource() == table.getSelectionModel()
                          && table.getRowSelectionAllowed()) {
                        // Column selection changed
                       sel = e.getFirstIndex();
            	  } else if (e.getSource() == table.getColumnModel().getSelectionModel()
                          && table.getColumnSelectionAllowed() ){
                       // Row selection changed
                       sel = e.getFirstIndex();
                   }
     
     
                  System.out.println("SELECTION         "+sel);
                      try{                      
                          JProgressBar barre=null;
                          String lab=null;
     
                          //on créer une nouvelle fenetre pour le disques
                                 barre = (JProgressBar)tabler.getValueAt(sel,1);
                                 lab = tabler.getValueAt(sel,0).toString();
                                 lab = lab.substring(1,3);                          
                                 new DessinEspaceLibre(NomServeur,lab,tailleMax.elementAt(sel),sel,unite.elementAt(sel));   
                          }
                          catch(Exception exsc){
                          	System.out.println("Erreur dans nouveau DessinEspaceLibre");
                          exsc.printStackTrace();}
     
                  if (e.getValueIsAdjusting()) {
                      System.out.println("TEST Adjusting...");
                  }
              }
          }
     
    }

  6. #6
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Bonjour,

    Je crois que j'avais déjà eut le coup, à la deuxième action, il te renvoie la bonne valeur de la ligne non?

    Si c'est çà, tu met juste un :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
     
    JTable t = new JTable() ;
    	t.getSelectionModel().addListSelectionListener( new MonListener() ) ;
     
    	public class MonListener implements ListSelectionListener
    	{
    		public void valueChanged(ListSelectionEvent e) {
    			int ligneSelectionne = t.getSelectedRow() ;
    			if(ligneSelectionne>=0)
                                 System.out.println("Ligne sélectionnée : " + ligneSelectionne );
    		}
    	}
    Pourquoi ca marche jamais?Vive le café!

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    ligneSelectionne n'est jamais >= 0

  8. #8
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Je sais pas si ca va t'aider mais voici comment je me souviens avoir procédé dans un soft:

    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
     
     
    tontab.addKeyListener(new KeyListener(){
    			public void  keyPressed(KeyEvent e){
     
     
    			     System.out.println(tontab.getSelectedRow()):
     
    			}
    			public void keyReleased(KeyEvent evt){	
     
    				if (evt.getKeyCode() == KeyEvent.VK_DOWN | evt.getKeyCode() == KeyEvent.VK_UP | evt.getKeyChar()== KeyEvent.VK_ENTER){
     
    					if(tab.getSelectedRow()>=0){
     
     
     
                                                System.out.println(tontab.getSelectedRow()):
     
    					}
     
     
    				}
    			}  
     
      			public void keyTyped(KeyEvent ev){}
     
    		});
     
     
    		tab.addMouseListener(new MouseListener(){
     
    			public void mouseExited(MouseEvent e1) {
     
    			}
     
    			public void mouseEntered(MouseEvent e2) {
     
    			}
     
    			public void mouseClicked(MouseEvent e3) {
     
     
     
    					if(tab.getSelectedRow()>=0){
     
     
     
    					}
    					if (e3.getClickCount() == 2){
     
     
    					}
     
     
     
     
     
     
    			}
     
    			public void mousePressed(MouseEvent e4) {
     
    			}
     
      			public void mouseReleased(MouseEvent e5) {
     
      			}
    		});
    Pourquoi ca marche jamais?Vive le café!

  9. #9
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    En fait en gros le tableau écoute le clique,le double clique,la touche haut et la touche bas.
    Ps : désolé pour l'indentation
    Pourquoi ca marche jamais?Vive le café!

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Ok je reviens tester après manger

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    et bien j'ai toujours -1 . C'est peut etre dans la definition de ma JTable que ça cloche...

  12. #12
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Ca affiche -1 quelque soit la ligne selectionnée?
    Pourquoi ca marche jamais?Vive le café!

  13. #13
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Oui

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Voilà où j'en suis , toujours -1 quelque soit la méthode ou la ligne
    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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    package Carte;
    import java.awt.GridLayout;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Vector;
     
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
     
    import Utilitaires.DataBase;
     
    public class Serveurs extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	String NomServeur;
    	Vector <String> LettreDisque;
    	Vector <Double> tailleMax;
    	Vector <Boolean> unite;
    	int pourcentage;
    	int x = 0;
    	int y = 20;
            JScrollPane pan;
            JTable tabler;
    	DataBase base;
            ResultSet resultat;
            CompTableCellRenderer composant = new CompTableCellRenderer();
            Thread tache;
            JPanel serv = this;
            boolean test = true;
            double taille;
     
     
     
            public void run(){
                while(test){
                    try{
                        this.lancer();
                        this.updateUI();
                        Thread.sleep(60000);
                    }
                    catch(Exception ecx){
                    System.out.println("Erreur de sleep de SErveur"+ecx.fillInStackTrace());}
                }
            }
     
     
            public void stopper(){
                test = false;
            }   
     
            public void lancer(){
                try{
                	String req="select id from appareils where nom = '"+NomServeur+"'";
                	System.out.println(req);
    		base = new DataBase(req);
    		base.readBDD();
    		resultat = base.retourner();
    		if(resultat.next() && resultat.next()){
    			String id = resultat.getString("id");
                String[] nom = {"Disque","Espace Libre","Espace Libre","Etat"}; 
     
                LettreDisque = new Vector<String>();
                tailleMax = new Vector<Double>();
                unite = new Vector<Boolean>();
     
                String req1="select lettre_disque,taille from el_disques where superviser = 1 and nom = '"+NomServeur+"' order by lettre_disque";
                System.out.println(req1);
                base.request=req1;
                base.readBDD();
                resultat = base.retourner();
                while(resultat.next()){
                    LettreDisque.addElement(resultat.getString("lettre_disque"));
                    tailleMax.addElement(resultat.getDouble("taille"));
                }
                base.fermer();
                Object[][] data = new Object[LettreDisque.size()][nom.length];
     
     
                JTable table = new JTable(new Modele(data,nom));
                table.setRowHeight(25);
                table.setShowGrid(false);
     
    //            new ModeleListener(table);
                pan = new JScrollPane(table);
                for(int i = 0;i < LettreDisque.size();i++){
                	String req2="select probleme,id from erreurs where nom = '"+NomServeur+"' and probleme = '"+LettreDisque.get(i)+"' and critique = 1 and id = '"+id+"'";
                	System.out.println(req2);
                        base = new DataBase(req2);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            table.setValueAt(new ImageIcon("images/iconefalse.GIF"),i,3);
                        }
                        else{
                            table.setValueAt(new ImageIcon("images/iconeOK.GIF"),i,3);
                        }
                        base.fermer();
                        taille = Double.parseDouble(tailleMax.get(i).toString());
                        JProgressBar bar = new JProgressBar(0,(int)taille);
                        System.out.println("taille barre"+taille);
                        String baf="select disques.utiliser from appareils,disques where appareils.id = disques.id and appareils.id = (select max(id) from appareils where nom = '"+NomServeur+"') and description = '"+LettreDisque.get(i).toString()+"' and disques.nom = '"+NomServeur+"' and appareils.id > '"+id+"' order by description";
                        System.out.println(baf);
                        base = new DataBase(baf);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            double utiliser = resultat.getDouble("utiliser");
                           System.out.println(utiliser+","+taille);
                            bar.setValue((int)(taille - utiliser));
                            bar.setString(Integer.toString(100*((int)(taille - utiliser))/(int)taille)+ " % ");
                            bar.setStringPainted(true);
                            table.setValueAt(" "+LettreDisque.get(i).toString(),i,0);
                            table.getColumnModel().getColumn(0).setCellRenderer(composant);
                            table.setValueAt(bar,i,1);
                            table.getColumnModel().getColumn(1).setCellRenderer(composant);
                            table.getColumnModel().getColumn(2).setCellRenderer(composant);
                            if(taille-utiliser < 1){
                                table.setValueAt(" " + Double.toString((taille-utiliser)*1024).substring(0,5)+" Mo/"+String.valueOf(taille*1024).substring(0, 5)+" Mo",i,2);
                                unite.addElement(false);
                            }
                            else{
                                table.setValueAt(" " + Double.toString((taille-utiliser)).substring(0,5)+" Go/"+String.valueOf(taille).substring(0, 5)+" Go",i,2);
                                unite.addElement(true);
                            }
     
    //                        for(int h=0;h<unite.size();h++)
    //                        	System.out.println(unite.elementAt(h));
                        }
                        base.fermer(); 
                    }
    		tabler = table;
                }
    		}
                catch(SQLException sqle){
                	System.out.println("Erreur SQL de la methode lancer");
                sqle.printStackTrace();}
            }
     
     
     
          public Serveurs(String Nom){
                this.NomServeur = Nom;
                this.lancer();
                add(pan);
                setLayout(new GridLayout(1,1));
                final ArrayList <Object> tabArray = new ArrayList<Object>();
     
          tabler.addKeyListener(new KeyListener(){
        			public void  keyPressed(KeyEvent e)
        				{
        				int i=0;
        				for(i=0;i<LettreDisque.size();i++)
        					{
     
        		            System.out.println(tabler.getSelectedRow());
        					}  			    			
        				}
        			public void keyReleased(KeyEvent evt)
        				{	
     
        				if (evt.getKeyCode() == KeyEvent.VK_DOWN | evt.getKeyCode() == KeyEvent.VK_UP | evt.getKeyChar()== KeyEvent.VK_ENTER)
        					{
        					if(tabler.getSelectedRow()>=0)
        						{
                                System.out.println(tabler.getSelectedRow());
        						}
        					}
        				}  
          			public void keyTyped(KeyEvent ev){}});
     
     
          tabler.addMouseListener(new MouseListener()
        		  {
     
        			public void mouseExited(MouseEvent e1) {
        				if(e1.getButton()==e1.BUTTON1)
        					System.out.println(tabler.getSelectedRow());
        			}
     
        			public void mouseEntered(MouseEvent e2) {
        				if(e2.getButton()==e2.BUTTON2)
        					System.out.println(tabler.getSelectedRow());
        			}
     
        			public void mouseClicked(MouseEvent e3) 
        				{
        				if(tabler.getSelectedRow()>=0)
    						{
     
    						}
    	    			if (e3.getClickCount() == 2)
    	    				{
     
    	    				}
    	    			}
        			public void mousePressed(MouseEvent e4) 
        				{
        				if(e4.getButton()==e4.BUTTON3)
        					System.out.println(tabler.getSelectedRow());
     
        				}
          			public void mouseReleased(MouseEvent e5) 
          			{
          				if(e5.getButton()==e5.MOUSE_WHEEL)
        					System.out.println(tabler.getSelectedRow());
     
          			}
        		});
    //            		try{                      
    //                        JProgressBar barre=null;
    //                        String lab=null;                    
    //                        //on créer une nouvelle fenetre pour le disques
    //                               barre = (JProgressBar)tabler.getValueAt(sel,1);
    //                               lab = tabler.getValueAt(sel,0).toString();
    //                               lab = lab.substring(1,3);                          
    //                               new DessinEspaceLibre(NomServeur,lab,tailleMax.elementAt(sel),sel,unite.elementAt(sel));   
    //                        }
    //                        catch(Exception exsc){
    //                        	System.out.println("Erreur dans nouveau DessinEspaceLibre");
    //                        exsc.printStackTrace();}
     
     
            }
     
          class CompTableCellRenderer extends JPanel implements TableCellRenderer{ 
        	  private static final long serialVersionUID = 1L;
                private JProgressBar barre;
    	    public CompTableCellRenderer(){}  
     
     
    	    public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column){
                    if(column == 1){
                        barre = (JProgressBar)value;
                        return barre;
                    }
                    else{
                        return new JLabel(value.toString());
                    } 
                }
          }
    }

  15. #15
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Euh ta JTable c'est tabler ou table?car la tu ecoutes le tableau tabler et je ne vois nul part tabler = new JTable(....

    Ah non j'ai rien dit tabler=table...désolé
    Pourquoi ca marche jamais?Vive le café!

  16. #16
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Essaie peut-etre avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TonModel.getSelectedRow();
    Pourquoi ca marche jamais?Vive le café!

  17. #17
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Oki je v tester

  18. #18
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Je n'ai pas de listselectionmodel pour faire ça , avec la methode getselectionModele() de JTable.

    Ma Jtable est initialisé comme ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    dat = new Modele(data,nom);
     
                JTable table = new JTable(dat);
    et Modele.java

    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
    package Carte;
     
    import javax.swing.JTable;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;
     
        class ModeleListener implements TableModelListener {
        	private static final long serialVersionUID = 1L;
        public ModeleListener(JTable table){
            table.getModel().addTableModelListener(this);
        }
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel)e.getSource();
            String columnName = model.getColumnName(column);
            Object data = model.getValueAt(row, column);
        }
    }
    public class Modele extends AbstractTableModel {
    	private static final long serialVersionUID = 1L;
            private String[] nom;
            private Object[][] data;
            public Modele(Object[][] data,String[] nom){
                this.data = data;
                this.nom = nom;
            }
     
            public int getColumnCount() {
                return nom.length;
            }
     
            public int getRowCount() {
                return data.length;
            }
     
            public String getColumnName(int col) {
                return nom[col];
            }
     
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
            public Class <?> getColumnClass(int c) {
     
                return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
                return false;
            }
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        }
    J'espere que ca vous va aider

  19. #19
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Quand tu fais tabler=table, je ne sais pas si tabler prend bien le modéle de table.
    Mais l'interface TableModeListener te sert à quoi là?retire le pour voir.
    Pourquoi ca marche jamais?Vive le café!

  20. #20
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Points : 99
    Points
    99
    Par défaut
    Re, je relance la discussion car je n'ai toujours pas toruvé la solution à mon problème : effectuer une action quand je clique sur une ligne de ma JTable (je veux le numéro de la ligne cliquée)

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Ajout JTable à un panneau dans une action sur un item
    Par rannou2609 dans le forum Composants
    Réponses: 1
    Dernier message: 21/04/2011, 20h53
  2. Pb Action sur un jbutton dans un jtable
    Par snay13 dans le forum Composants
    Réponses: 6
    Dernier message: 20/07/2010, 00h32
  3. [JTable] Action sur JButton après mise en attente
    Par 6ix dans le forum Composants
    Réponses: 2
    Dernier message: 28/02/2008, 07h27
  4. Action sur une jtable
    Par Karim93210 dans le forum Composants
    Réponses: 3
    Dernier message: 16/07/2007, 10h53
  5. [Flash MX] Action sur un bouton
    Par WriteLN dans le forum Flash
    Réponses: 9
    Dernier message: 20/10/2003, 14h01

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