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] Ajout ligne


Sujet :

Composants Java

  1. #1
    Membre averti Avatar de rems033
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    513
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2007
    Messages : 513
    Points : 345
    Points
    345
    Par défaut [JTable] Ajout ligne
    Salut a tous,
    Je suis un gros newbie des JTable et je voudrai savoir pourquoi ma methode d'ajout de ligne dans un DefaultTableModel ne fonctionne pas !!
    Je vous laisse un bout de 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
     
    //initialisation de ma fenetre graphique
    public void init()
        {
            Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/myotis.png"));
            setIconImage(icon);
     
            initComponents();
     
            // Centrage de la fenetre
            Toolkit tk = Toolkit.getDefaultToolkit() ;
            Dimension d = tk.getScreenSize() ;		//  resolution de l'ecran
            this.hEcran = (int)(d.getHeight());		//  hauteur de l'ecran
            this.lEcran = (int)(d.getWidth());		//  largeur de l'ecran
            setLocation((lEcran-this.getWidth())/2,(hEcran-this.getHeight())/2);
     
            initJTable();
     
            Object[] o = new Object[5];
            for(int i=0 ; i<5 ; i++)
                o[i] = ""+i ;
     
            tm.addLigne(o);
            tm.addLigne(o);
     
            detection.setModel(tm );
     
            this.validate();
     
            this.setVisible(true);
        }
     
        // Methode d'initialisation de la JTable des voies
        private void initJTable()
        {
            this.tm = new TableModele();
     
            DefaultTableCellRenderer dr = new DefaultTableCellRenderer();
            dr.setHorizontalAlignment(SwingConstants.CENTER);
     
            this.detection.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
     
            this.detection.setModel(tm);
     
            for(int i=0;i<5;i++)
            {
                 TableColumn col = this.detection.getColumnModel().getColumn(i);
                 col.setCellRenderer(dr);
            }
     
        }
     
     
        // Classe representant les elements du tableau
        public class TableModele extends DefaultTableModel
        {
            private Object[][] data ;
            private Object[] colonnesNom ;
            private boolean ALLOW_ROW_SELECTION = true;
            private int row_count = 1 ;
     
            public TableModele()
            {
                this.colonnesNom = new Object[5];
                this.data = new Object[row_count][5];
                init();
            }
     
            public void init()
            {
                // Initialisation des titres
                this.colonnesNom[0] = new String("Heure");
                this.colonnesNom[1] = new String("Ratio Detect.");
                this.colonnesNom[2] = new String("Ratio Max.");
                this.colonnesNom[3] = new String("Max Ampl.");
                this.colonnesNom[4] = new String("Count");
     
                // Initialisation des cellules
     
                detection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                if (ALLOW_ROW_SELECTION) 
                { // true by default
                    ListSelectionModel rowSM = detection.getSelectionModel();
                    rowSM.addListSelectionListener(new ListSelectionListener() 
                    {
                        public void valueChanged(ListSelectionEvent e) 
                        {
                            //Ignore extra messages.
                            if (e.getValueIsAdjusting()) return;
     
                            ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                            if (lsm.isSelectionEmpty()) 
                            {
                                System.out.println("Lignes selectionnables");
                            } 
                            else 
                            {
                                int selectedRow = lsm.getMinSelectionIndex();
                                System.out.println("ligne " + selectedRow + " est selectionnee");
                            }
                        }
                    });
                } 
                else 
                {
                    detection.setRowSelectionAllowed(false);
                }
     
                super.setDataVector(this.data,this.colonnesNom);
            }
     
            public void addLigne(Object[] o)
            {
                super.addRow(o);
            }
     
            public Object getValueAt(int ligne, int col){return data[ligne][col];}
     
            public String getColumnName(int col){return (String)colonnesNom[col];}
     
            public void setValueAt(Object value, int ligne, int col)
            {
                this.data[ligne][col] = value ;
                fireTableCellUpdated(ligne, col);
            }
     
            public Class getColumnClass(int c) 
            {
                return getValueAt(0, c).getClass();
            }
     
            public boolean isCellEditable(int row, int col) 
            {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                return false ;
            }
        }
    Je me retrouve avec l'erreur suivante :
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at leesa.FenetreDetectionHDD$TableModele.getValueAt(FenetreDetectionHDD.java:141)
    at javax.swing.JTable.getValueAt(JTable.java:2638)
    at javax.swing.JTable.prepareRenderer(JTable.java:5652)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
    at javax.swing.JComponent.paintComponent(JComponent.java:763)
    at javax.swing.JComponent.paint(JComponent.java:1027)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JViewport.paint(JViewport.java:747)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paint(JComponent.java:1036)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
    at javax.swing.JComponent.paintChildren(JComponent.java:864)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:285)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1128)
    at javax.swing.JComponent.paint(JComponent.java:1013)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    at java.awt.Container.paint(Container.java:1797)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:734)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:679)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:659)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Merci pour votre aide car je galère depuis hier matin sur ce truc !!

  2. #2
    Membre averti Avatar de rems033
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    513
    Détails du profil
    Informations personnelles :
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2007
    Messages : 513
    Points : 345
    Points
    345
    Par défaut
    En fait j'ai simplement ajouté la méthode :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    public void setData(Object[][] data)
            {
                this.data = data ;
                this.fireTableDataChanged();
            }
    dans la classe héritant de AbstractTableModel
    et j'ai aussi ajouté la méthode :
    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
     
    public void ajouterLigne(Object[] o)
        {
            Object[][] data = this.tm.getData() ;
            Object[][] tmp = new Object[data.length+1][5];
            for(int i=0;i<data.length;i++)
            {
                for(int j=0;j<5;j++)
                {
                    tmp[i][j] = data[i][j] ;
                }
            }
            for(int i=0;i<5;i++)
                tmp[data.length][i] = o[i] ;
            this.tm.setData(tmp);
            this.detection.setModel(tm);
        }
    dans la classe principale...je pense que c'est un peu bourrin mais c'est ce que j'ai trouvé de plus efficace...
    Merci quand meme!

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

Discussions similaires

  1. [JTable] Ajouter ligne avec moins de colonnes
    Par encours dans le forum Composants
    Réponses: 1
    Dernier message: 27/12/2011, 09h57
  2. JTable : ajout ligne
    Par Tchoupi34 dans le forum Composants
    Réponses: 6
    Dernier message: 06/06/2010, 21h41
  3. Réponses: 15
    Dernier message: 09/06/2006, 12h13
  4. [debutant] [JTable] ajout d'une ligne
    Par lanfeustdetroll dans le forum Composants
    Réponses: 1
    Dernier message: 29/06/2005, 18h19
  5. [JTable] ajouter une ligne
    Par rvfranck dans le forum Composants
    Réponses: 3
    Dernier message: 30/03/2005, 14h25

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