Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Come utilizzare JTable

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561

    Come utilizzare JTable

    Vorrei creare una tabella con i nomi alle colonne e per questo ho scritto questo codice:

    String[] columnNames = {"First Name",
    "Last Name",
    "Sport",
    "# of Years",
    "Vegetarian"};
    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)}
    };


    Jtable jTable1 = new JTable(data, columnNames);

    I nomi alle colonne non mi vengono visualizzate ma solo i record.....qualcuno sa perchè?


    tulipan

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Come utilizzare JTable

    Originariamente inviato da tulipan
    Jtable jTable1 = new JTable(data, columnNames);

    I nomi alle colonne non mi vengono visualizzate ma solo i record.....qualcuno sa perchè?
    E scommetto che nel container hai aggiunto direttamente jTable1. No, devi mettere il JTable in un JScrollPane ed aggiungere quest'ultimo nel container.

    E qui sul tutorial dice pure:

    If you are using a table without a scroll pane, then you must get the table header component and place it yourself.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561
    Ho modificato il codice così senza però ottenere nessun risultato:



    String[] columnNames = {"First Name",
    "Last Name",
    "Sport",
    "# of Years",
    "Vegetarian"};
    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)}
    };


    jTable1 = new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(jTable1);
    * //jTable1.setFillsViewportHeight(true);
    jPanel1.add(scrollPane);

    L'istruzione * non viene riconosciuta come valida


    tulipan

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.