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

avec Java Discussion :

setAutoCreateRowSorter et IndexOutOfBoundsException


Sujet :

avec Java

  1. #1
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut setAutoCreateRowSorter et IndexOutOfBoundsException
    Bonjour à tous,

    J'utilise sur une JTable la méthode "setAutoCreateRowSorter()" afin de pouvoir trier ma JTable en cliquant sur les entêtes des colonnes. Jusque là pas de problème.

    Cette JTable a un contenu qui peut être vidée (plus aucune ligne dans la JTable) et là forcément, si l'utilisateur clique sur une des entêtes de colonne je me retrouve avec une exception de type "IndexOutOfBoundsException".

    Mon problème c'est que je ne sais pas comment désactiver ce tri si la JTable est vide. J'ai bien essayé de désactiver le tri de la même manière que je l'avais activé lorsque ma JTable se vide :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    maJTable.setAutoCreateRowSorter(false)
    Mais ça ne change rien

    Qu'est ce que j'ai loupé ?

  2. #2
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Bonjour, pourrait-on voir la trace complète de l'exception?

  3. #3
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut
    La voici :

    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
     
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    	at java.util.ArrayList.RangeCheck(Unknown Source)
    	at java.util.ArrayList.get(Unknown Source)
    	at tieconnector.TableInModel.getValueAt(TableInModel.java:30)
    	at tieconnector.TableInModel.getColumnClass(TableInModel.java:41)
    	at javax.swing.table.TableRowSorter.useToString(Unknown Source)
    	at javax.swing.DefaultRowSorter.updateUseToString(Unknown Source)
    	at javax.swing.DefaultRowSorter.sort(Unknown Source)
    	at javax.swing.DefaultRowSorter.shouldOptimizeChange(Unknown Source)
    	at javax.swing.DefaultRowSorter.rowsDeleted(Unknown Source)
    	at javax.swing.JTable.notifySorter(Unknown Source)
    	at javax.swing.JTable.sortedTableChanged(Unknown Source)
    	at javax.swing.JTable.tableChanged(Unknown Source)
    	at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
    	at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(Unknown Source)
    	at tieconnector.TableInModel.clearData(TableInModel.java:57)
    	at tieconnector.TIEConnectorWindow.actionPerformed(TIEConnectorWindow.java:561)
    	at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    	at javax.swing.JComboBox.contentsChanged(Unknown Source)
    	at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
    	at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
    	at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    	at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    	at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    	at java.util.ArrayList.RangeCheck(Unknown Source)
    	at java.util.ArrayList.get(Unknown Source)
    	at tieconnector.TableInModel.getValueAt(TableInModel.java:30)
    	at tieconnector.TableInModel.getColumnClass(TableInModel.java:41)
    	at javax.swing.JTable.getColumnClass(Unknown Source)
    	at javax.swing.JTable.getCellRenderer(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JViewport.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JLayeredPane.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Voici aussi la classe TableInModel dont il est question dans l'exception :

    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
     
    import java.util.ArrayList;
     
    import javax.swing.table.AbstractTableModel;
     
    public class TableInModel extends AbstractTableModel {
     
    	private static final long serialVersionUID = 1L;
    	private String[] columnNames = {"Fichier", "Chemin complet", "Taille (en octets)", "Date de réception"};
        private ArrayList<Object[]> data;
     
        public TableInModel(ArrayList<Object[]> data) {
        	this.data = data;
        }
     
        public int getColumnCount() {
            return columnNames.length;
        }
     
        public int getRowCount() {
            return data.size();
        }
     
        public String getColumnName(int col) {
            return columnNames[col];
        }
     
        public Object getValueAt(int row, int col) {
        	return data.get(row)[col];
        }
     
        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        @SuppressWarnings("unchecked")
    	public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
     
        /*
         * Rafraichit la table avec un nouveau jeu de données
         */
        public void refreshData(ArrayList<Object[]> data) {
        	this.data = data;
        	fireTableDataChanged();
        }
     
        /*
         * Vide le tableau
         */
        public void clearData(int lastRow) {
        	this.data = new ArrayList<Object[]>();
        	fireTableRowsDeleted(0, lastRow - 1);
        }
    }

  4. #4
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public void clearData(int lastRow) {
        this.data = new ArrayList<Object[]>();
        fireTableRowsDeleted(0, lastRow - 1);
    }
    Est-ce que ce -1 est correct?
    Est-ce que dans l'appel à cette méthode le -1 n'aurait pas été déjà fait?
    Tu devrais vérifier à cet endroit la longueur de this.data avant modification, la valeur de lastRow.

  5. #5
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut
    J'ai 10 lignes dans ma table et lorsque je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    System.out.println(this.data.size());
    J'ai bien 10 qui s'affiche dans ma console. Donc le lastRow - 1 est correct car je supprime les lignes de 0 à 9 (ce qui fait bien mes 10 lignes).

    Je ne sais pas si ça peut aider mais pour obtenir cette exception, il faut :

    1°) Trier sur n'importe quelle colonne de ma JTable
    2°) Vider la JTable

  6. #6
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Et si tu ignores lastRow et que tu fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public void clearData(int lastRow) {
        int rowCount = getRowCount();
        this.data = new ArrayList<Object[]>();
        fireTableRowsDeleted(0, rowCount - 1);
    }

  7. #7
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut
    Même résultat

    Par contre je suis persuadé que mon erreur vient du tri automatique que j'applique à ma table :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tableIn.setAutoCreateRowSorter(true);
    Lorsque je supprime cette ligne plus de soucis ... ce qui paraît normal vu que l'exception apparaît lorsque je clique sur une entête de colonne pour trier.

    En fait, dès que ma table est vide et que l'on tri on obtient une exception. Comment faire pour désactiver ce tri dès que la table est vide ? Existe-t-il des paramètres pour indiquer ça ou alors est-ce que je dois redéfinir la classe RowSorter ?

  8. #8
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut
    J'ai peut-être trouvé un début de piste avec la classe TableSorter.java et ce post.

    Le problème c'est que je ne comprends pas bien comment utiliser ça. Dans le post on peut lire :

    Il suffit de faire ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    TableSorter sorter = new TableSorter(new MyTableModel());
    pour rendre n'importe quel TableModel triable.
    J'ai bien fait ça, mais en cliquant sur mes entêtes de colonne ça ne trie pas

    Peut-être manque-t-il quelque chose ....

  9. #9
    Membre confirmé
    Inscrit en
    Novembre 2002
    Messages
    63
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 63
    Par défaut
    Bon et bien à priori c'était la bonne piste. Mon problème est résolu et pour ceux que ça intéresse, voici l'incantation magique pour utiliser la classe indiquée dans mon précédent post :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    ArrayList <Object[]> dataIn = new ArrayList();
    tableInModel = new TableInModel(dataIn);
    TableSorter sorterIn = new TableSorter(tableInModel); 
    tableInModel.setSorter(sorterIn);
    tableIn = new JTable(sorterIn);
    sorterIn.setTableHeader(tableIn.getTableHeader());
    Avec ça, plus d'exception sur le tri d'une JTable vide.

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

Discussions similaires

  1. Erreur java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    Par impossibeulman dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 07/08/2008, 15h18
  2. message erreur java.lang.IndexOutOfBoundsException
    Par coquero dans le forum Langage
    Réponses: 2
    Dernier message: 20/05/2008, 15h11
  3. IndexOutOfBoundsException liste dynamique
    Par burhud dans le forum Struts 1
    Réponses: 6
    Dernier message: 26/07/2007, 17h31

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