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

 Java Discussion :

Filtrer une JList par un bouton


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2012
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2012
    Messages : 1
    Par défaut Filtrer une JList par un bouton
    Bonjour, j'ai une Jlist avec des boutons alphabétique , le problème quand je clique par exemple sur le bouton "A" il ne filtre pas la liste et m'affiche pas une liste qui commence tous les prénoms par "A" . Voici mon code. J'ai vraiment besoin de votre aide merci.
    Code java : 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
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
     
    package client;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Types;
    import java.util.ArrayList;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
     
     
    public class Liste extends JFrame {
     
    	Vector columnNames = new Vector();
        Vector data = new Vector(); 
        static ResultSet rs;
    	static Connection conn;
    	static Statement stat;
    	private JTable table;
        private TableRowSorter<TableModel> sorter;
    	private JFrame frame;
    	private JTextField filtertxt; 
    	private JLabel l1;
    	private JButton btna,btnb,btnc,btnd,btne,btnf,btng,btnh,btni,btnj,btnk,btnl,btnm,btnn,btno,btnp,btnq,btnr,btns,btnt,btnu,btnv,btnw,btnx,btny,btnz;
    	private ArrayList alist;
     
    		public Liste() {
     
    			try
    		     {
    		         Class.forName("com.mysql.jdbc.Driver");
    		         System.out.println("com.mysql.jdbc.Driver found");
    		         conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/active","root","");
    		         System.out.println("Connexion Ok");
     
     
    		           //  Read data from a table
    		           String sql = "SELECT * FROM ajoututilisateur";
    		           Statement stmt = conn.createStatement();
    		           ResultSet rs = stmt.executeQuery(sql);
    		           ResultSetMetaData md = rs.getMetaData();
    		           int columns = md.getColumnCount();
     
    		           //  Get column names 
    		           for (int i = 1; i <= columns; i++)
    		           {
    		              columnNames.addElement( md.getColumnName(i) );
    		           }
     
    		           //  Get row data
    		           while (rs.next()) 
    		           {
    		              Vector row = new Vector(columns);
    		              for (int i = 1; i <= columns; i++)
    		              {
    		                 row.addElement(rs.getObject(i));
    		              }
    		              data.addElement(row);
    		           }
    		           rs.close();
    		           stmt.close();
    		        }
     
     
    	    catch(Exception cnfe)
    	    {
    	        System.out.println("Error:"+cnfe.getMessage());
    	    }
     
    	    TableModel model = new DefaultTableModel (data,columnNames)
    	    {
    	    public Class getColumnClass(int columnNames) {
    	        Class returnValue;
    	        if ((columnNames >= 0) && (columnNames < getColumnCount())) {
    	          returnValue = getValueAt(0, columnNames).getClass();
    	        } else {
    	          returnValue = Object.class;
    	        }
    	        return returnValue;
    	      }
    	    };
    	     table = new JTable (model) ; 
    	     sorter = new TableRowSorter<TableModel>(model) ; 
         	 JScrollPane scrollPane = new JScrollPane((table));
         	 table.setRowSorter (sorter) ; 
         	 getContentPane().setLayout(new GridLayout(2,1));
            getContentPane().add(scrollPane);
    	    JPanel form = new JPanel () ;
    	    JPanel form1 = new JPanel () ;
    	    l1=new JLabel("Recherche :");
    	    form.add (l1) ;
     
    	//  ------------Bouton actionlistener----------
     
    	    btna=new JButton("A");
    	    btnb=new JButton("B");
    	    btnc=new JButton("C");
    	    btnd=new JButton("D");
    	    btne=new JButton("E");
    	    btnf=new JButton("F");
    	    btng=new JButton("G");
    	    btnh=new JButton("H");
    	    btni=new JButton("I");
    	    btnj=new JButton("J");
    	    btnk=new JButton("K");
    	    btnl=new JButton("L");
    	    btnm=new JButton("M");
    	    btnn=new JButton("N");
    	    btno=new JButton("O");
    	    btnp=new JButton("P");
    	    btnq=new JButton("Q");
    	    btnr=new JButton("R");
    	    btns=new JButton("S");
    	    btnt=new JButton("T");
    	    btnu=new JButton("U");
    	    btnv=new JButton("V");
    	    btnw=new JButton("W");
    	    btnx=new JButton("X");
    	    btny=new JButton("Y");
    	    btnz=new JButton("Z");
     
    	    filtertxt = new JTextField (25) ;
    	    filtertxt.getDocument().addDocumentListener(new DocumentListener() 
    	     {
    	    	 public void changedUpdate(DocumentEvent e) 
    	    	 {
    	    		 newFilter();
    	    		 }
    	    	 public void insertUpdate(DocumentEvent e) 
    	    	 {
    	    		 table.clearSelection();
    	    		 newFilter();
    	    		 }
    	    	 public void removeUpdate(DocumentEvent e) 
    	    	 {
    	    		 newFilter();
    	    		 }
    	    	 });
     
     
     
    	     form.add (filtertxt) ;
    	     //form.add(form1);
    	     GridLayout grd=new GridLayout(3,2);
    	     getContentPane().setLayout(grd);
    	     getContentPane().add(form);
    	     getContentPane().add(form1);
    	      form1.add(btna);
    	     form1.add(btnb);
    	     form1.add(btnc);
    	     form1.add(btnd);
    	     form1.add(btne);
    	     form1.add(btnf);
    	     form1.add(btng);
    	     form1.add(btnh);
    	     form1.add(btni);
    	     form1.add(btnj);
    	     form1.add(btnk);
    	     form1.add(btnl);
    	     form1.add(btnm);
    	     form1.add(btnn);
    	     form1.add(btno);
    	     form1.add(btnp);
    	     form1.add(btnq);
    	     form1.add(btnr);
    	     form1.add(btns);
    	     form1.add(btnt);
    	     form1.add(btnu);
    	     form1.add(btnv);
    	     form1.add(btnw);
    	     form1.add(btnx);
    	     form1.add(btny);
    	     form1.add(btnz);
    	     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    	     table.getSelectionModel().addListSelectionListener(new ListeListener());
     
    	     AfficheBoutonListener abl=new AfficheBoutonListener();
     
    	     	btna.addActionListener(abl);
    			btnb.addActionListener(abl);
    			btnc.addActionListener(abl);
    			btnd.addActionListener(abl);
    			btne.addActionListener(abl);
    			btnf.addActionListener(abl);
    			btng.addActionListener(abl);
    			btnh.addActionListener(abl);
    			btni.addActionListener(abl);
    			btnj.addActionListener(abl);
    			btnk.addActionListener(abl);
    			btnl.addActionListener(abl);
    			btnm.addActionListener(abl);
    			btnn.addActionListener(abl);
    			btno.addActionListener(abl);
    			btnp.addActionListener(abl);
    			btnq.addActionListener(abl);
    			btnr.addActionListener(abl);
    			btns.addActionListener(abl);
    			btnt.addActionListener(abl);
    			btnu.addActionListener(abl);
    			btnv.addActionListener(abl);
    			btnw.addActionListener(abl);
    			btnx.addActionListener(abl);
    			btny.addActionListener(abl);
    			btnz.addActionListener(abl);
     
    		}
     
     
    	    private void newFilter()  
    	    {  
    	        RowFilter<TableModel , Integer> rf = null;   
    	        //declare a row filter for your table model  
    	        try  
    	        {  
    	            rf = RowFilter.regexFilter("^(?i)" +  filtertxt.getText(),6);    
    	            //initialize with a regular expression  
    	        }  
    	        catch (java.util.regex.PatternSyntaxException e)  
    	        {  
    	            return;  
    	        }  
    	        sorter.setRowFilter(rf);  
    	    }  
     
    		private class AfficheBoutonListener implements ActionListener{
     
    			public void actionPerformed (ActionEvent e)
    			{
    				RowFilter<TableModel , Integer> rf = null;   
        	        //declare a row filter for your table model  
        	        try  
        	        {  
        	            rf = RowFilter.regexFilter("^A" +  filtertxt.getText(),0);    
        	            //initialize with a regular expression  
        	        }  
        	        catch (java.util.regex.PatternSyntaxException a)  
        	        {  
        	            return;  
        	        }  
        	        sorter.setRowFilter(rf);  
     
        		if((JButton)e.getSource()==btna) 
    			{ 
    				Liste l=new Liste();
    				l.setVisible(true);
    				l.setSize(650,250);
     
    				} 
        	}
     
        }
     
     
     
    	     public class ListeListener implements ListSelectionListener
    		{
    			public void valueChanged(ListSelectionEvent e)
    			{
    				if(!e.getValueIsAdjusting())
    				{  
     
    				   table.getValueAt(table.getSelectedRow(),0);
    				   table.getValueAt(table.getSelectedRow(),1);
    				   table.getValueAt(table.getSelectedRow(),2);
    				   table.getValueAt(table.getSelectedRow(),4);
    				   table.getValueAt(table.getSelectedRow(),5);
    				   table.getValueAt(table.getSelectedRow(),6);
    				   table.getValueAt(table.getSelectedRow(),7);
    				  // table.getValueAt(table.getSelectedRow(),8);	
     
    					//InfosPersonne a= new InfosPersonne (data);
    					//a.setVisible(true);
    					//a.setSize(495,570);
    					}
    					}
    			}
     
     
     
    		public static void main(String[] args) {
    			Liste li = new Liste();
    			   li.setVisible(true);
    			   li.setSize(650,250);		   			   	      
    	}
     
    	     }

  2. #2
    Modérateur
    Avatar de kolodz
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    2 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 209
    Billets dans le blog
    52
    Par défaut
    Je pense qu'il y a moyen de rendre con code moins long en utilisant une liste de boutons.
    Quel est le problème ?
    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
    private class AfficheBoutonListener implements ActionListener{
     
    	public void actionPerformed (ActionEvent e)
    	{
    		RowFilter<TableModel , Integer> rf = null;   
    		//declare a row filter for your table model  
    		try  
    		{  
    			rf = RowFilter.regexFilter("^A" +  filtertxt.getText(),0);    
    			//initialize with a regular expression  
    		}  
    		catch (java.util.regex.PatternSyntaxException a)  
    		{  
    			return;  
    		}  
    		sorter.setRowFilter(rf);  
     
    		if((JButton)e.getSource()==btna) 
    		{ 
    			Liste l=new Liste();
    			l.setVisible(true);
    			l.setSize(650,250);
     
    		}
    	}
    }
    Action ne se déclencha pas ? Le test sur le bouton ne fonctionne pas ?
    Il est possible que le btna ne soit pas reconnu comme étant "Liste.this.btna"
    Ou c'est le filtre en lui-même qui pose problème ?
    As-tu tester ton filtre pour afficher directement que ce qui commence par A ? Et donc sans gestion d’évènement, pour isoler le problème ?
    Si une réponse vous a été utile pensez à
    Si vous avez eu la réponse à votre question, marquez votre discussion
    Pensez aux FAQs et aux tutoriels et cours.

Discussions similaires

  1. Ouvrir et fermer une catégorie par un bouton
    Par snyfir dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 29/02/2008, 10h57
  2. Réponses: 1
    Dernier message: 27/02/2008, 10h24
  3. Recupere la valeur d'une liste par un bouton
    Par megami dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 16/01/2007, 11h27
  4. filtrer une table par programmation
    Par programeur dans le forum Delphi
    Réponses: 2
    Dernier message: 11/01/2007, 12h38
  5. appeler une page par un bouton
    Par ph_anrys dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 28/06/2006, 17h04

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