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

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

Composants Java Discussion :

Jtable setAutoCreateRowSorter qui plante


Sujet :

Composants Java

  1. #1
    Membre régulier
    Inscrit en
    Mars 2006
    Messages
    126
    Détails du profil
    Informations forums :
    Inscription : Mars 2006
    Messages : 126
    Points : 105
    Points
    105
    Par défaut Jtable setAutoCreateRowSorter qui plante
    Bonjour,

    Voici un petit code qui fonctionne si je retire la ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    jTable1.setAutoCreateRowSorter(true);
    Ce code fonctionne aussi si la Jtable est remplie, mais pas quand elle est vide.

    Sinon il plante :
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at HandsTableModell2.getValueAt(testIHM.java:107)
    at HandsTableModell2.getColumnClass(testIHM.java:111)

    Et j'avoue que je ne comprends pas bien d'où vient le problème...
    Si vous avez des idées...
    merci

    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
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
     
    public class testIHM extends JFrame{
    	private static final long serialVersionUID = 7856325953273045719L;
        private javax.swing.JScrollPane jScrollPane1;
        private HandsTableModell2 model;
    	private javax.swing.JTable jTable1;
    	private javax.swing.JButton btnRefresh;
     
    	public testIHM(){
            jScrollPane1 = new javax.swing.JScrollPane();
            btnRefresh = new javax.swing.JButton();
     
            setResizable(false);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);   
     
            btnRefresh.setText("Filtrer");
            btnRefresh.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                	btnRafraichirActionPerformed(evt);
                }
            });
     
            model = new HandsTableModell2(null);
            jTable1 = new JTable(model);        
            jTable1.setAutoCreateRowSorter(true);
     
            jScrollPane1 = new javax.swing.JScrollPane(jTable1);
     
            FlowLayout experimentLayout = new FlowLayout();        
            final JPanel compsToExperiment = new JPanel();
            compsToExperiment.setLayout(experimentLayout);
     
            compsToExperiment.add(jScrollPane1);
            compsToExperiment.add(btnRefresh);
            add(compsToExperiment);
     
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
    	}
     
        private void btnRafraichirActionPerformed(java.awt.event.ActionEvent evt) {    
        	Object[][] data = {
        		    {"Mary", "Campione",
        		     "Snowboarding", new Integer(5), new Boolean(false)},
        		    {"Alison", "Huml",
        		     "Rowing", new Integer(3), new Boolean(true)},
        		    {"Kathy", "Walrath",
        		     "Knitting", new Integer(2), new Boolean(false)},
        		    {"Sharon", "Zakhour",
        		     "Speed reading", new Integer(20), new Boolean(true)},
        		    {"Philip", "Milne",
        		     "Pool", new Integer(10), new Boolean(false)}
        		};
     
        	model.setData(new Object[0][0]);
        	//model.setData(data);
        }  
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new testIHM();
    	}
     
    }
     
    class HandsTableModell2 extends AbstractTableModel {
    	private static final long serialVersionUID = 3685438063323573991L;
    	String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};
     
            private Object[][] data = {};
     
            public HandsTableModell2 (Object[][] donnees){
                this.data = donnees;
            }
     
            boolean[] canEdit = new boolean [] {
            		false,false, false, false, false, false
            };
     
            public int getColumnCount() {
                return columnNames.length;
            }
     
            public int getRowCount() {            
                if (data == null) return 0;
                return data.length;
            }
     
            public String getColumnName(int col) {
                return columnNames[col];
            }
     
            public Object getValueAt(int row, int col) {
           		return data[row][col];
            }
     
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
     
            /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
     
            /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
     
            public void setData(Object[][] tablo) {
            	data = tablo;        		
            	fireTableDataChanged();
            }
    }

  2. #2
    Membre actif Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Points : 275
    Points
    275
    Par défaut
    Citation Envoyé par Hesiode Voir le message
    je ne comprends pas bien d'où vient le problème...
    Si vous avez des idées...
    Quand la table est vide, logiquement la méthode suivante ne peut pas trouver de valeurs dans la table:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    Le code suivant résout le problème:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    private Class[] types = {String.class, String.class, String.class,
        Integer.class, Boolean.class};
    public Class getColumnClass(int c) {
        return types[c];
    }

Discussions similaires

  1. PB d'update qui plante aléatoirement sans renvoyer d'erreur
    Par plc402 dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 01/08/2005, 09h10
  2. [Novice] Problème avec Eclipse 3.0.x qui plante
    Par esolarc dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 27/05/2005, 13h22
  3. Programme qui plante
    Par harris_macken dans le forum C++
    Réponses: 1
    Dernier message: 22/05/2005, 23h50
  4. Pc qui plante
    Par tooms2028 dans le forum Ordinateurs
    Réponses: 9
    Dernier message: 19/03/2005, 17h32
  5. [JTextArea] redessin qui plante
    Par phil_ma dans le forum Composants
    Réponses: 3
    Dernier message: 04/01/2005, 05h19

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