Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - aggiungere dati a una jlist
Forum - Java - aggiungere dati a una jlist

Avatar
Java5 (Ex-Member)
Rookie


Messaggi: 23
Iscritto: 19/10/2009

Segnala al moderatore
Postato alle 11:36
Lunedì, 23/11/2009
Ragazzi quello che vi posto è un semplicissimo programma che comprende: Un campo di testo una JList e un bottone.

Al click sul bottone, il contenuto della casella di testo dovrebbe essere aggiunto alla Jlist ma purtroppo non succede niente.

Codice sorgente - presumibilmente Java

  1. [CODE]
  2.  
  3. import java.awt.BorderLayout;
  4.  
  5. import javax.swing.DefaultListModel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JFrame;
  8. import javax.swing.JButton;
  9. import java.awt.Rectangle;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JList;
  12. import java.awt.Dimension;
  13. import javax.swing.JTextField;
  14.  
  15. public class List extends JFrame {
  16.  
  17.         private static final long serialVersionUID = 1L;
  18.         private JPanel jContentPane = null;
  19.         private JButton jButton = null;
  20.         private JScrollPane jScrollPane = null;
  21.         private JList jList = null;
  22.         private DefaultListModel listModel = null;
  23.         private JTextField jTextField = null;
  24.  
  25.         public List() {
  26.                 super();
  27.                 initialize();
  28.         }
  29.         private void initialize() {
  30.                 this.setSize(300, 232);
  31.                 this.setContentPane(getJContentPane());
  32.                 this.setTitle("JFrame");
  33.         }
  34.         private JPanel getJContentPane() {
  35.                 if (jContentPane == null) {
  36.                         jContentPane = new JPanel();
  37.                         jContentPane.setLayout(null);
  38.                         jContentPane.add(getJButton(), null);
  39.                         jContentPane.add(getJScrollPane(), null);
  40.                         jContentPane.add(getJTextField(), null);
  41.                 }
  42.                 return jContentPane;
  43.         }
  44.         private JButton getJButton() {
  45.                 if (jButton == null) {
  46.                         jButton = new JButton();
  47.                         jButton.setBounds(new Rectangle(63, 148, 162, 37));
  48.                         jButton.addActionListener(new java.awt.event.ActionListener() {
  49.                                 public void actionPerformed(java.awt.event.ActionEvent e) {
  50.                                         listModel = new DefaultListModel();
  51.                                         listModel.addElement(jTextField.getText());
  52.                                         jList = new JList(listModel);
  53.                                 }
  54.                         });
  55.                 }
  56.                 return jButton;
  57.         }
  58.         private JScrollPane getJScrollPane() {
  59.                 if (jScrollPane == null) {
  60.                         jScrollPane = new JScrollPane();
  61.                         jScrollPane.setBounds(new Rectangle(62, 46, 165, 92));
  62.                         jScrollPane.setViewportView(getJList());
  63.                 }
  64.                 return jScrollPane;
  65.         }
  66.         private JList getJList() {
  67.                 if (jList == null) {
  68.                         jList = new JList();
  69.                         jList.setModel(new DefaultListModel());
  70.                         jList.setVisibleRowCount(-1);
  71.                 }
  72.                 return jList;
  73.         }
  74.         private JTextField getJTextField() {
  75.                 if (jTextField == null) {
  76.                         jTextField = new JTextField();
  77.                         jTextField.setBounds(new Rectangle(62, 4, 164, 33));
  78.                 }
  79.                 return jTextField;
  80.         }
  81. }
  82.  
  83. [/CODE]



edit by netarrow: sistemato il titolo

Ultima modifica effettuata da netarrow il 23/11/2009 alle 12:55
PM Quote
Avatar
netarrow (Admin)
Guru^2


Messaggi: 2502
Iscritto: 12/05/2004

Segnala al moderatore
Postato alle 12:46
Lunedì, 23/11/2009
così è sistemato:

Codice sorgente - presumibilmente Java

  1. iimport java.awt.BorderLayout;
  2.  
  3. import javax.swing.DefaultListModel;
  4. import javax.swing.JPanel;
  5. import javax.swing.JFrame;
  6. import javax.swing.JButton;
  7. import java.awt.Rectangle;
  8. import javax.swing.JScrollPane;
  9. import javax.swing.JList;
  10. import java.awt.Dimension;
  11. import javax.swing.JTextField;
  12.  
  13. public class List extends JFrame {
  14.        
  15.     private static final long serialVersionUID = 1L;
  16.     private JPanel jContentPane = null;
  17.     private JButton jButton = null;
  18.     private JScrollPane jScrollPane = null;
  19.     private JList jList = null;
  20.     private JTextField jTextField = null;
  21.         private DefaultListModel listModel;
  22.        
  23.     public List() {
  24.         super();
  25.                 listModel = new DefaultListModel();
  26.         initialize();
  27.     }
  28.     private void initialize() {
  29.         this.setSize(300, 232);
  30.         this.setContentPane(getJContentPane());
  31.         this.setTitle("JFrame");
  32.                 this.setVisible(true);
  33.     }
  34.     private JPanel getJContentPane() {
  35.         if (jContentPane == null) {
  36.             jContentPane = new JPanel();
  37.             jContentPane.setLayout(null);
  38.             jContentPane.add(getJButton(), null);
  39.             jContentPane.add(getJScrollPane(), null);
  40.             jContentPane.add(getJTextField(), null);
  41.         }
  42.         return jContentPane;
  43.     }
  44.     private JButton getJButton() {
  45.         if (jButton == null) {
  46.             jButton = new JButton();
  47.             jButton.setBounds(new Rectangle(63, 148, 162, 37));
  48.             jButton.addActionListener(new java.awt.event.ActionListener() {
  49.                                                                           public void actionPerformed(java.awt.event.ActionEvent e) {
  50.                                                                           listModel.addElement(jTextField.getText());
  51.                                                                           }
  52.                                                                           });
  53.         }
  54.         return jButton;
  55.     }
  56.     private JScrollPane getJScrollPane() {
  57.         if (jScrollPane == null) {
  58.             jScrollPane = new JScrollPane();
  59.             jScrollPane.setBounds(new Rectangle(62, 46, 165, 92));
  60.             jScrollPane.setViewportView(getJList());
  61.         }
  62.         return jScrollPane;
  63.     }
  64.     private JList getJList() {
  65.         if (jList == null) {
  66.             jList = new JList();
  67.                         jList.setModel(listModel);
  68.             jList.setVisibleRowCount(-1);
  69.         }
  70.         return jList;
  71.     }
  72.     private JTextField getJTextField() {
  73.         if (jTextField == null) {
  74.             jTextField = new JTextField();
  75.             jTextField.setBounds(new Rectangle(62, 4, 164, 33));
  76.         }
  77.         return jTextField;
  78.     }
  79.        
  80.         public static void main(String args[]) {
  81.                 new List();
  82.         }
  83.        
  84. }



devi stare attento quando istanzi nuovi oggetti, non è che puoi fare new ogni volta che vuoi usare un'istanza, sennò la "resetti" ogni volta.

Ultima modifica effettuata da netarrow il 23/11/2009 alle 12:48
PM Quote
Avatar
Java5 (Ex-Member)
Rookie


Messaggi: 23
Iscritto: 19/10/2009

Segnala al moderatore
Postato alle 13:46
Lunedì, 23/11/2009
Perfetto NetArrow:hail:
grazie mille funziona alla grande!! :k:
avevo intuito che il problema poteva essere quello, ma non riuscivo ad uscirmene.
Grazie moltissimo davvero.
Ciao

PM Quote