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 :

[Débutante] Comment fonctionne une JTable?


Sujet :

Composants Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut [Débutante] Comment fonctionne une JTable?
    bonjour!

    je voudrai savoir comment je peux faire pour ajouter des champs dans un JTable, j'ai essayé plein de choses qui ne vont pas (pcq je ne comprend pas bien comment ça marche)

    je récupère mes champs ici:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Object[][] data = new Object[10][2];
    merci

  2. #2
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Tu veux juste insérer le tableau que tu recupère dans ta JTable c'est ca?
    Pourquoi ca marche jamais?Vive le café!

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    oui mais en fait j'en ai 3!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    		Object[][] data = new Object[10][2];
    		Object[][] data2 = new Object[10][2];
    		Object[][] data3 = new Object[10][2];
    en la 1ere cononne s'appelerait "Match" et l'autre "File"

  4. #4
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Tu voudrais pouvoir mettre 3 tableau dans un seul JTable, c'est ca ? Donc soit au départ tu mets tes 3 tableaux dans un, soit il faudra t'orienter vers un TableModel.

    Il y a un constructueur tout pret : JTable(String[] ColumnNames, Object[][] rows) qui te met directement le header.

  5. #5
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Le mieux je pense est de faire une boucle,dans le cas ou tu veux entrer le data tableau d'objets dans une JTable:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    for(int i=0;i<10;i++){
     
    TonJtable.setValueA(data[i][0],i,0);
    TonJtable.setValueA(data[i][1],i,1);
    }
    }
    Pourquoi ca marche jamais?Vive le café!

  6. #6
    Membre averti Avatar de nicotine002
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 577
    Points : 409
    Points
    409
    Par défaut
    Si tu veux des Headers---->wichtounet
    Pourquoi ca marche jamais?Vive le café!

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    merci!!

    et comment puis-je faire pour fixer la taille des colonnes pour qu'elles s'adapte a la longueur du champ?
    et pour mettre un ascenceur?

    (pcq la j'ai essayé des choses et ça a bien planté )

  8. #8
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    Je me rappelle plus trop pour le JTable mais soit :

    -> Il faut que tu crées un JScrollPane que tu ajoutes a ta JFrame. Apres tu ajoutes ton composant (genre JTextArea) dans le JScrollPane.

    -> Soit tu utilises (si elle existe :p ) la méthode genre setScrollable

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    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
     
    myTable = new JTable(rows,columnNames);
    //JTable(Object[][] rowData, Object[] columnNames)
    ScrollPane scroll = new JScrollPane(myTable);
    myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    myTable.setRowHeight(20);
     
    for (int k = 0; k < myTable.getColumnCount(); j++) 
            { 
                 myTable.getColumnModel().getColumn(j).setPreferredWidth(120);
     
            }
    scroll.setBounds(100, 100, 620, 300);
     
    pan1.add(myTable);
    pq est ce qu'il me fait plein d'erreurs???

    java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
    at be.microresearch.test.ListRep.<init>(ListRep.java:103)
    at be.microresearch.test.Frame$MonGestionnaireAction.actionPerformed(Frame.java:73)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.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.pumpOneEventForHierarchy(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)

  10. #10
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Pour fixer la taille des colonnes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    TableColumn colonne1 = tableMessage.getColumnModel().getColumn(0);
    		colonne1.setPreferredWidth(75);
     
    		TableColumn colonne2 = tableMessage.getColumnModel().getColumn(1);
    		colonne2.setPreferredWidth(170);

  11. #11
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Tu vas un rang trop loin, puisque ca commence à zéro, fais plutot ta boucle ainsi :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (int k = 0; k < myTable.getColumnCount()-1; j++)

  12. #12
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    ça n'a rien changé!!
    java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
    at be.microresearch.test.ListRep.<init>(ListRep.java:103)
    at be.microresearch.test.Frame$MonGestionnaireAction.actionPerformed(Frame.java:73)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.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.pumpOneEventForHierarchy(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)

  13. #13
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    De toute facon c'est t bien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for (int k = 0; k < myTable.getColumnCount(); j++)
    car tu vas de 0 à < à myTable.getColumnCount()
    il aurait fallu myTable.getColumnCount()-1 si tu avais mis <=

    Sinon il y a une seule erreur ... si il y a tous ces messages c'est parce q c'est la printStackTrace qui s'affiche ...

    D'ailleurs question ... l'erreur est sur quelle ligne ?

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    sur aucune
    je lance le pgm il fait quelques trucs puis j'ai ces erreurs qui appraissent dans la console

  15. #15
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Citation Envoyé par delph1983
    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
     
    myTable = new JTable(rows,columnNames);
    //JTable(Object[][] rowData, Object[] columnNames)
    ScrollPane scroll = new JScrollPane(myTable);
    myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    myTable.setRowHeight(20);
     
    for (int k = 0; k < myTable.getColumnCount(); j++) 
            { 
                 myTable.getColumnModel().getColumn(j).setPreferredWidth(120);
     
            }
    scroll.setBounds(100, 100, 620, 300);
     
    pan1.add(myTable);
    je crois avoir compris pourquoi ca va pas... Tu dis que tu fais la boucle tant que k est plus petit le nombre de colonnes mais tu incrémentes j...

    Donc ta boucle est infinie et se bloque quand elle arrive plus haut que le nombre de colonnes

  16. #16
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    ha oui c ça!!

    mais je me suis dit vu qu'il n'y a que 2 colonnes je fais ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    myTable.getColumnModel().getColumn(0).setPreferredWidth(310);  
    myTable.getColumnModel().getColumn(1).setPreferredWidth(310);
    Cooment faire pour que la table n'affiche que les champs remplis?
    autrement dit comment faire pour que ici:
    Object[][] rows = new Object[30][2];
    je ne mette pas 30 mais une variable qui augmenterait au fur et à mesure des ajouts???

  17. #17
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    loooooll

    Ah bah c sur que ca doit etre genant !!!!!!

  18. #18
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    Tu devrais utiliser un TableModel ... c'est vachement plus pratique et plus souple et ca corrigera ton pb ...

    je recherche le tuto q j'avais utiliser une fois et je te le re-balance

  19. #19
    Membre actif
    Inscrit en
    Mai 2005
    Messages
    217
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 217
    Points : 220
    Points
    220
    Par défaut
    Bon ... je viens de regarder ... et en fait nan! Il faut que tu connaisses le nombre de colonne de départ (et leurs noms)

    D'ailleurs d'un point de vue fonctionnel, comment ca se fait que tu ne connaisses pas le nombre de colonne ??? tes objets ne sont pas de meme type ??


    _____________________

    Merdeeeeeeee je viens de relire ton code ... c'est le nb de ligne qui te manques !!!!!!!!!

    Bon bah ca va marcher alors ... Je viens de retrouver du code d'un vieux truc q j'ai fait ... je l'epure un peu .... et je te le balance

  20. #20
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Elle connait le nombre colonnes mais celui du nombre de lignes...

    Ce que peut tout à fait régler un TableModel, il me semble

Discussions similaires

  1. Débutant : Comment ajouter une ligne à un DataGrid sur VB ?
    Par gamool dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 23/11/2005, 23h53
  2. [débutant] comment incorporer une librairie à netbeans
    Par djerbafr dans le forum NetBeans
    Réponses: 4
    Dernier message: 13/06/2005, 20h10
  3. [débutant] Comment créer une base ?
    Par laffreuxthomas dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 14/12/2004, 22h12
  4. comment fonctionne une interface graphique???
    Par elekis dans le forum Langages de programmation
    Réponses: 2
    Dernier message: 27/10/2004, 23h10
  5. [débutante] comment afficher une page asp ?
    Par boucher_emilie dans le forum ASP
    Réponses: 5
    Dernier message: 29/06/2004, 09h40

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