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 :

Edition automatique d'une cellule d'une JTable au clavier


Sujet :

Composants Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 64
    Points : 47
    Points
    47
    Par défaut Edition automatique d'une cellule d'une JTable au clavier
    Voilà en fait j'arrive très bien à éditer la celllule d'une JTable (et plus particulièrement d'une JXTable) avec la touche TAB du clavier mais c'est pour récupérer le bon Index de la ligne de la table !

    Le problème est que j'applique des filtres ainsi qu'un sorting à ma table, ce qui fait que la première ligne de ma JXTable n'est pas forcément celle portant l'indice 0.

    En gros, l'édition se passe normalement au début en cliquant sur une cellule avec la souris. Le fait d'appuyer sur la touche TAB du clavier permet de passer à la ligne du dessous et d'amener le focus directement dans la cellule ayant la même colonne que la ligne du dessus.

    Problème : Comment connaitre l'index de la ligne juste en dessous de celle que je suis en train d'éditer ?.

    De plus j'ai remarqué que la fonction editCellAt(int row, int column) finissait par tourner dans le vide au bout d'un moment, ce qui fait qu'on a une belle boucle sans fin.....

    Comment remédier à ce second problème ?

    Merci .

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    La solution tient dans les méthodes convertRowIndexToModel(int row)et convertRowIndexToView(int row) de la JXTable qui permettent de connaître l'index d'une ligne de la JTabl dans le modèle et inversement

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 64
    Points : 47
    Points
    47
    Par défaut
    Exact je les utilise déja mais ca ne marche pas.

    Ces méthodes sont utiles lorsqu'il y a seulement un filtering mais vu que j'ai un sorting en plus et bien comme je l'expliquai la ligne 2 n'est pas forcément celle portant l'index numero 2 (en mode vue).

    Donc comment récupérer l'index de la ligne se trouvant dessous la ligne cliqué à la souris par l'utilisateur ?

    Faut-il que j'utilise la fonction rowAtPoint(int x, int y) dans une boucle while en faisant varier le Y (avec Y les coordonnées de la souris en Y) jusqu'à ce que l'index de la ligne retourné change ???

    Le problème viendra par la suite, lorsque j'arriverais en bas de ma JTable de retrouver l'index de la première ligne .


    Autre solution svp ?

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2007
    Messages : 90
    Points : 62
    Points
    62
    Par défaut
    Tu utilises quel sorter pour ta JXTable ? Celui par défaut ?

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 64
    Points : 47
    Points
    47
    Par défaut
    oui c'est le sorter par défault avec différent type de sorting selon les colonnes.

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2007
    Messages : 90
    Points : 62
    Points
    62
    Par défaut
    Tu pourrais mettre le bout de code qui correspond à la déclaration de tes filtres et de ton rowsorter ?

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 64
    Points : 47
    Points
    47
    Par défaut
    Ma création de JXTable :

    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
        /**
         * Create a JXTable
         * 
         * @return table
         */
        private JXTable createTable(){
     
            // new table
            final JXTable table = new JXTable();
     
            // column identifiers
            final String[] columnIdentifiers =
                    { "Référence", "Nom", "Marque", "Prix" };
     
            // new products table model
            final ProductsTableModel productsTableModel =
                    new ProductsTableModel(this, table, columnIdentifiers, this
                            .getListProducts());
     
            // set model
            table.setModel(productsTableModel);
     
            // set row selection allowed
            table.setRowSelectionAllowed(true);
     
            // set not focusable
            // table.setFocusable(false);
     
            // set not reordering
            table.getTableHeader().setReorderingAllowed(false);
     
            // set single selection mode
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
            for (int i = 0; i < table.getColumnCount(); i++){
     
                // set table cell renderer
                table.getColumn(i).setCellRenderer(new ProductsTableCellRenderer());
            }
     
            // table header
            final JTableHeader tableHeader = table.getTableHeader();
     
            // set hand cursor
            tableHeader.setCursor(new Cursor(Cursor.HAND_CURSOR));
     
            // set default editor
            table.setDefaultEditor(Object.class, new ProductsTableCellEditor(this,
                    productsTableModel));
     
            // set price comparator
            table.getColumnExt(3).setComparator(new PriceComparator());
     
            // set price column not visible
            table.getColumnExt(3).setVisible(false);
     
            // set hand cursor
            table.setCursor(new Cursor(Cursor.HAND_CURSOR));
     
            // add mouse listener
            table.addMouseListener(this);
     
            // add mouse motion listener
            table.addMouseMotionListener(this);
     
            // add key listener
            table.addKeyListener(this);
     
            // add list selection listener
            table.getSelectionModel().addListSelectionListener(this);
     
            // input map
            final InputMap inputMap =
                    (InputMap) UIManager.get("Table.ancestorInputMap");
     
            // remove input map to enter key
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
     
            // remove input map to tab key
            inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
     
            return table;
     
        }

    Mon cell Editor :

    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
    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
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    package org.ynostel.jpricesstudy.editors;
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.EventObject;
     
    import javax.swing.DefaultCellEditor;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;
    import javax.swing.table.TableCellEditor;
     
    import org.apache.log4j.Logger;
    import org.jdesktop.swingx.decorator.SortOrder;
    import org.ynostel.jpricesstudy.JPricesStudy;
    import org.ynostel.jpricesstudy.JPricesStudyUtils;
    import org.ynostel.jpricesstudy.entities.Product;
    import org.ynostel.jpricesstudy.entities.Shop;
    import org.ynostel.jpricesstudy.filters.NumericDocument;
    import org.ynostel.jpricesstudy.models.ProductsTableModel;
     
    /**
     * @author ynostel
     * @version 0.1b
     */
    public class ProductsTableCellEditor extends DefaultCellEditor implements
            TableCellEditor, CellEditorListener, MouseListener, ActionListener,
            KeyListener {
     
        /** decimal precision */
        private static final int DECIMAL_PRECISION = 2;
     
        /** log */
        private static final Logger log =
                Logger.getLogger(ProductsTableCellEditor.class);
     
        /** serial uid */
        private static final long serialVersionUID = -2625070973853113455L;
     
        /** clicked component */
        private Component clickedComponent;
     
        /** main application */
        private final JPricesStudy mainApplication;
     
        /** popup menu */
        private final JPopupMenu popUpMenu;
     
        /** products table model */
        private final ProductsTableModel tableModel;
     
        /** textfield */
        private JTextField textField;
     
        /**
         * Constructor
         * 
         * @param mainApplication
         *            main application
         * @param tableModel
         *            table model
         */
        public ProductsTableCellEditor(JPricesStudy mainApplication,
                ProductsTableModel tableModel){
     
            // call super
            super(new JTextField());
     
            // table model
            this.tableModel = tableModel;
     
            // main application
            this.mainApplication = mainApplication;
     
            // popup menu menu item names
            final String[] popupMenuItemNames =
                    { "Couper", "Copier", "Coller", "Supprimer", "---",
                            "Tout selectionner" };
     
            // popup menu item actions
            final String[] popupMenuItemActions =
                    { "cut", "copy", "paste", "delete", "---", "selectAll" };
     
            // popup menu item accelerators
            final KeyStroke[] popupMenuItemAccelerators =
                    { null, null, null, null, null, null };
     
            this.popUpMenu =
                    this.createPopupMenu(popupMenuItemNames, popupMenuItemActions,
                        popupMenuItemAccelerators);
     
        }
     
        /*
         * (non-Javadoc)
         * @see
         * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        /**
         * 
         */
        @Override
        public void actionPerformed(ActionEvent arg0){
     
        }
     
        /**
         * Create a popup menu
         * 
         * @param menuItemNames
         *            menu item names
         * @param menuItemActions
         *            menu item actions
         * @param menuItemAccelerators
         *            menu item accelerators
         * @return popupMenu
         */
        public JPopupMenu createPopupMenu(String[] menuItemNames,
                String[] menuItemActions, KeyStroke[] menuItemAccelerators){
     
            final JPopupMenu popupMenu = new JPopupMenu();
     
            // no focus
            popupMenu.setFocusable(false);
     
            for (int i = 0; i < menuItemNames.length; i++){
     
                if (menuItemNames[i].equals("---")){
     
                    // add separator
                    popupMenu.addSeparator();
                }
                else{
     
                    // new menu item
                    final JMenuItem menuItem = new JMenuItem(menuItemNames[i]);
     
                    // preferred size
                    menuItem.setPreferredSize(new Dimension(100, 20));
     
                    // action command
                    menuItem.setActionCommand(menuItemActions[i]);
     
                    if (menuItemAccelerators != null){
     
                        // accelerator
                        menuItem.setAccelerator(menuItemAccelerators[i]);
                    }
     
                    // hand cursor
                    menuItem.setCursor(new Cursor(Cursor.HAND_CURSOR));
     
                    // add listener
                    menuItem.addActionListener(this);
     
                    // add item to menu
                    popupMenu.add(menuItem);
     
                }
            }
            return popupMenu;
        }
     
        /*
         * (non-Javadoc)
         * @see
         * javax.swing.event.CellEditorListener#editingCanceled(javax.swing.event
         * .ChangeEvent)
         */
        /**
         * 
         */
        @Override
        public void editingCanceled(ChangeEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see
         * javax.swing.event.CellEditorListener#editingStopped(javax.swing.event
         * .ChangeEvent)
         */
        /**
         * 
         */
        @Override
        public void editingStopped(ChangeEvent arg0){
     
            // get current sort order from the price column
            final SortOrder sortOrder =
                    this.getTableModel().getTable().getSortOrder(3);
     
            // set not sortable before setting the value
            this.getTableModel().getTable().getColumnExt(3).setSortable(false);
     
            // value
            final Object value = this.getCellEditorValue();
     
            // string value
            String stringValue = value.toString();
     
            if (!stringValue.equals("")){
     
                if (!stringValue.contains(".")){
     
                    stringValue += ".";
                }
     
                // decimal count
                final int decimalCount =
                        (stringValue.length() - stringValue.indexOf("."));
     
                for (int i = decimalCount; i <= ProductsTableCellEditor.DECIMAL_PRECISION; i++){
     
                    stringValue += "0";
                }
     
                if (Double.parseDouble(stringValue) == 0){
     
                    stringValue = "";
     
                }
                // set value
                this.getTableModel().setValueAt(stringValue,
                    this.getTableModel().getTable().getSelectedRow(),
                    this.getTableModel().getTable().getSelectedColumn());
     
                // set sortable
                this.getTableModel().getTable().getColumnExt(3).setSortable(true);
     
                // reset sort order
                this.getTableModel().getTable().setSortOrder(3, sortOrder);
     
                if (!stringValue.equals("")){
     
                    // set product price
                    this.getSelectedProduct().setPrice(
                        (Shop) this.getMainApplication().getListShopsCB()
                                .getSelectedItem(),
                        this.getMainApplication().getDatePicker().getDate(),
                        Double.parseDouble(stringValue));
                }
     
            }
     
        }
     
        /*
         * (non-Javadoc)
         * @see javax.swing.DefaultCellEditor#getCellEditorValue()
         */
        /**
         * 
         */
        @Override
        public Object getCellEditorValue(){
     
            return this.getTextField().getText();
        }
     
        /**
         * @return the clickedComponent
         */
        public Component getClickedComponent(){
     
            return this.clickedComponent;
        }
     
        /**
         * @return the mainApplication
         */
        public JPricesStudy getMainApplication(){
     
            return this.mainApplication;
        }
     
        /**
         * @return the popUpMenu
         */
        public JPopupMenu getPopUpMenu(){
     
            return this.popUpMenu;
        }
     
        /**
         * @return the selected product
         */
        public Product getSelectedProduct(){
     
            return (Product) this.getTableModel().getValueAt(
                this.getTableModel().getTable().getSelectedRow(), 1);
        }
     
        /*
         * (non-Javadoc)
         * @see
         * javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing
         * .JTable, java.lang.Object, boolean, int, int)
         */
        /**
         * 
         */
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column){
     
            // set textfield
            this.setTextField((JTextField) super.getTableCellEditorComponent(table,
                value, isSelected, row, column));
     
            // add mouse listener
            this.getTextField().addMouseListener(this);
     
            // add key listener
            this.getTextField().addKeyListener(this);
     
            log.debug("product price = "
                    + this.getSelectedProduct().getPrice(
                        (Shop) this.getMainApplication().getListShopsCB()
                                .getSelectedItem(),
                        this.getMainApplication().getDatePicker().getDate()) + "");
     
            // set numeric document
            this.getTextField().setDocument(
                new NumericDocument(this.getTextField(), 2, false));
     
            // set background
            this.getTextField().setBackground(new Color(255, 240, 100));
     
            // add cell editor listener
            this.addCellEditorListener(this);
     
            if (value != null){
     
                log.debug("value" + value);
     
                // set text
                this.getTextField().setText(value.toString());
            }
     
            return this.getTextField();
        }
     
        /**
         * @return the tableModel
         */
        public ProductsTableModel getTableModel(){
     
            return this.tableModel;
        }
     
        /**
         * @return the textfield
         */
        public JTextField getTextField(){
     
            return this.textField;
        }
     
        /*
         * (non-Javadoc)
         * @see javax.swing.DefaultCellEditor#isCellEditable(java.util.EventObject)
         */
        /**
         * 
         */
        @Override
        public boolean isCellEditable(EventObject arg0){
     
            if (arg0 instanceof KeyEvent){
     
                log.debug("Key event");
     
            }
     
            if (arg0 instanceof MouseEvent){
     
                // mouse event
                final MouseEvent evt = (MouseEvent) arg0;
     
                // mouse overed row
                final int row =
                        this.getTableModel().getTable().rowAtPoint(
                            new Point(evt.getX(), evt.getY()));
     
                // set selection interval
                this.getTableModel().getTable().getSelectionModel()
                        .setSelectionInterval(row, row);
     
                if (evt.getClickCount() >= 1){
     
                    return true;
                }
            }
     
            return true;
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
         */
        /**
         * 
         */
        @Override
        public void keyPressed(KeyEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
         */
        /**
         * 
         */
        @Override
        public void keyReleased(KeyEvent arg0){
     
            if (arg0.getKeyCode() == KeyEvent.VK_TAB){
     
                // stop cell editing
                this.stopCellEditing();
     
                log.debug("Row count view : "
                        + this.getTableModel().getTable().getRowCount());
     
                log.debug("Selected row view : "
                        + this.getTableModel().getTable().getSelectedRow());
     
                if ((this.getTableModel().getTable().getSelectedRow()) <= this
                        .getTableModel().getTable().getRowCount()){
     
                    // set row selection
                    this
                            .getTableModel()
                            .getTable()
                            .getSelectionModel()
                            .addSelectionInterval(
                                this.getTableModel().getTable().getSelectedRow() + 1,
                                this.getTableModel().getTable().getSelectedRow() + 1);
     
                }
     
                log.debug("Row model to edit : "
                        + this.getTableModel().getTable().convertRowIndexToModel(
                            this.getTableModel().getTable().getSelectedRow()));
     
                // edit cell
                this.getTableModel().getTable().editCellAt(
                    this.getTableModel().getTable().convertRowIndexToModel(
                        this.getTableModel().getTable().getSelectedRow()), 3);
     
                // request focus
                this.getTextField().requestFocus();
     
            }
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
         */
        /**
         * 
         */
        @Override
        public void keyTyped(KeyEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
         */
        /**
         * 
         */
        @Override
        public void mouseClicked(MouseEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
         */
        /**
         * 
         */
        @Override
        public void mouseEntered(MouseEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
         */
        /**
         * 
         */
        @Override
        public void mouseExited(MouseEvent arg0){
     
        }
     
        /*
         * (non-Javadoc)
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
         */
        /**
         * 
         */
        @Override
        public void mousePressed(MouseEvent arg0){
     
            if (arg0.getSource() instanceof JTextField){
     
                // textfield
                final JTextField textField = (JTextField) arg0.getSource();
     
                if (SwingUtilities.isRightMouseButton(arg0)
                        && textField.isEditable()){
     
                    if (textField.getSelectedText() == null){
     
                        // cut and copy menu disabled
                        this.popUpMenu.getComponent(0).setEnabled(false);
                        this.popUpMenu.getComponent(1).setEnabled(false);
                        this.popUpMenu.getComponent(3).setEnabled(false);
                    }
                    else{
     
                        // cut and copy menu enabled
                        this.popUpMenu.getComponent(0).setEnabled(true);
                        this.popUpMenu.getComponent(1).setEnabled(true);
                        this.popUpMenu.getComponent(3).setEnabled(true);
                    }
     
                    if (JPricesStudyUtils.getTextFromClipBoard() == null){
     
                        // past menu disabled
                        this.popUpMenu.getComponent(2).setEnabled(false);
                    }
                    else{
     
                        // past menu enabled
                        this.popUpMenu.getComponent(2).setEnabled(true);
                    }
     
                    // show popup menu
                    this.popUpMenu.show(arg0.getComponent(), arg0.getX(), arg0
                            .getY());
     
                    // request focus
                    textField.requestFocus();
     
                    // set clicked component
                    this.setClickedComponent(textField);
                }
            }
     
        }
     
        /*
         * (non-Javadoc)
         * @see
         * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
         */
        /**
         * 
         */
        @Override
        public void mouseReleased(MouseEvent arg0){
     
        }
     
        /**
         * @param clickedComponent
         *            the clickedComponent to set
         */
        public void setClickedComponent(Component clickedComponent){
     
            this.clickedComponent = clickedComponent;
        }
     
        /**
         * @param textField
         *            the textField to set
         */
        private void setTextField(JTextField textField){
     
            this.textField = textField;
        }
    }

    Mon filter :

    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
    package org.ynostel.jpricesstudy.filters;
     
    import java.util.ArrayList;
     
    import org.apache.log4j.Logger;
    import org.jdesktop.swingx.decorator.Filter;
    import org.ynostel.jpricesstudy.entities.Product;
    import org.ynostel.jpricesstudy.entities.ProductCategory;
    import org.ynostel.jpricesstudy.entities.Shop;
    import org.ynostel.jpricesstudy.models.ProductsTableModel;
     
    /**
     * @author ynostel
     * @version 0.1b
     */
    public class ProductsFilter extends Filter {
     
        /** log */
        private static final Logger log = Logger.getLogger(ProductsFilter.class);
     
        /** product category */
        private final ProductCategory productCategory;
     
        /** shop */
        private final Shop shop;
     
        private final ProductsTableModel tableModel;
     
        /** to previous */
        private ArrayList<Integer> toPrevious;
     
        /**
         * Constructor
         * 
         * @param shop
         *            shop
         * @param productCategory
         *            product category
         * @param tableModel
         *            table model
         */
        public ProductsFilter(Shop shop, ProductCategory productCategory,
                ProductsTableModel tableModel){
     
            // call super
            super();
     
            // product category
            this.productCategory = productCategory;
     
            // table model
            this.tableModel = tableModel;
     
            // shop
            this.shop = shop;
        }
     
        /**
         * Find a product
         * 
         * @param row
         *            row
         * @return hasFound
         */
        public boolean findProduct(int row){
     
            log.debug("row = " + row);
     
            final Product product =
                    (Product) this.getTableModel().getValueAt(row, 1);
     
            if ((this.getShop() == null) && (this.getProductCategory() == null)){
     
                return true;
            }
            else if (this.getShop() == null){
     
                return (product.findCategory(this.getProductCategory()) != null);
            }
            else{
     
                if (this.getProductCategory() == null){
     
                    return (this.getShop().contains(product));
                }
                else{
     
                    return (this.getShop().contains(product) && (product
                            .findCategory(this.getProductCategory()) != null));
                }
            }
     
        }
     
        /**
         * @return the productCategory
         */
        public ProductCategory getProductCategory(){
     
            return this.productCategory;
        }
     
        /**
         * @return the shop
         */
        public Shop getShop(){
     
            return this.shop;
        }
     
        /*
         * (non-Javadoc)
         * @see org.jdesktop.swingx.decorator.Filter#getSize()
         */
        /**
         * 
         */
        @Override
        public int getSize(){
     
            return this.getToPrevious().size();
        }
     
        /**
         * @return the tableModel
         */
        private ProductsTableModel getTableModel(){
     
            return this.tableModel;
        }
     
        /**
         * @return the toPrevious
         */
        private ArrayList<Integer> getToPrevious(){
     
            return this.toPrevious;
        }
     
        /**
         * @param toPrevious
         *            the toPrevious to set
         */
        private void setToPrevious(ArrayList<Integer> toPrevious){
     
            this.toPrevious = toPrevious;
        }
     
        /*
         * (non-Javadoc)
         * @see org.jdesktop.swingx.decorator.Filter#filter()
         */
        @Override
        protected void filter(){
     
            // input size
            final int inputSize = this.getInputSize();
     
            int current = 0;
     
            for (int i = 0; i < inputSize; i++){
     
                if (this.findProduct(i)){
     
                    // add integer
                    this.getToPrevious().add(new Integer(i));
     
                    // generate inverse map entry while we are here
                    this.fromPrevious[i] = current++;
                }
            }
     
        }
     
        /*
         * (non-Javadoc)
         * @see org.jdesktop.swingx.decorator.Filter#init()
         */
        @Override
        protected void init(){
     
            // new arraylist
            this.setToPrevious(new ArrayList<Integer>());
        }
     
        /*
         * (non-Javadoc)
         * @see org.jdesktop.swingx.decorator.Filter#mapTowardModel(int)
         */
        @Override
        protected int mapTowardModel(int row){
     
            return this.getToPrevious().get(row);
        }
     
        /*
         * (non-Javadoc)
         * @see org.jdesktop.swingx.decorator.Filter#reset()
         */
        @Override
        protected void reset(){
     
            // clear
            this.getToPrevious().clear();
     
            // input size
            final int inputSize = this.getInputSize();
     
            // from previous
            this.fromPrevious = new int[inputSize];
     
            for (int i = 0; i < inputSize; i++){
     
                this.fromPrevious[i] = -1;
            }
        }
     
    }

    Tout est là.

    nb : J'applique le filtre à la table après que l'utilisateur ait fait une sélection dans une comboBox.

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2007
    Messages : 90
    Points : 62
    Points
    62
    Par défaut
    J'ai peur d'avoir le regret de t'annoncer que si tu utillises le sorter par défaut avec les filtres en même temps, ça ne fonctionne pas
    SwingX et Java 6 ne font pas bon ménage pour le sorting si j'ai bien compris.

    http://wiki.java.net/bin/view/Javade...tFilter#5_vs_6

    J'ai actuellement le même problème.

    As tu pensé à rajouter à ta FilterLine un ShuttleSorter ?

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 64
    Points : 47
    Points
    47
    Par défaut
    Non pas de ShuttleShorter.

    C'est censé corriger qquechose ?

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Octobre 2007
    Messages : 90
    Points : 62
    Points
    62
    Par défaut
    Bah c'est exactement comme un filtre sauf que ça trie :

    http://swinglabs.org/hudson/job/Swin...tleSorter.html

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     public ShuttleSorter(int col,  boolean ascending)
    Je n'ai pas trouvé d'autre solution pour pallier ce problème

Discussions similaires

  1. Réponses: 1
    Dernier message: 13/04/2015, 11h17
  2. [Toutes versions] coller les données d'une plage d'une cellule dans une cellule d'une autre feuille[VBA]
    Par arthson dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 24/01/2012, 17h37
  3. [XL-2007] problème de codage en vba ("copie d'une partie d'une cellule dans une cellule vide")
    Par skipeemed dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 26/10/2010, 17h01
  4. [XL-2002] Macro de comparaison d'une cellule d'une feuille avec une cellule d'une autre feuille.
    Par steelydan dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 08/09/2010, 12h59
  5. Recherche une valeur d'une cellule dans une colonne d'une autre feuille
    Par kourria dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 21/06/2007, 13h48

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