Jeszcze nie udało mi się nic specjalnie wykombinować, może opiszę to dokładniej. Mam klasę świat:
public class Swiat {
/** kontener przechowujacy grupy */
ArrayList<Grupa> grupy = new ArrayList<Grupa>();
i w tej klasie mam kontener z obiektami klasy Grupa. Klasa Grupa wygląda tak:
public class Grupa {
/** nazwa grupy */
String nazwaGrupy;
i tutaj jest między innymi w Stringu przechowywana nazwa grupy.
W tym całym swoim pierwszym oknie dodałem dwa buttony, jeden dodaje grupy drugi je usuwa, Wygląda to tak:
public class Program extends javax.swing.JFrame {
Swiat s = new Swiat();
int licznik = 0;
PokazGrupy pg;
/**
* Creates new form NewJFrame
*/
public InteligentnyBudynek() {
super("Inteligentny Budyenk");
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String text = JOptionPane.showInputDialog(null, "Wprowadz nazwe grupy: ");
s.grupy.add(new Grupa(text));
if(text != null) {
JOptionPane.showMessageDialog(null, "Grupa: " + text + " została dodana");
licznik++;
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
pg = new PokazGrupy(s, licznik);
pg.setVisible(true);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String text = JOptionPane.showInputDialog(null, "Wprowadz nazwe grupy do usuniecia: ");
if(text != null) {
for(Grupa g : s.grupy) {
if(g.nazwaGrupy.equals(text)) {
s.grupy.remove(g);
licznik--;
JOptionPane.showMessageDialog(null, "Grupa: " + text + " została usunieta");
}
else {
JOptionPane.showMessageDialog(null, "Grupa: " + text + " nie istnieje");
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InteligentnyBudynek().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
I tam jest ten button1, który wyświetla kolejne okno. Kod wygląda tak:
public class PokazGrupy extends javax.swing.JFrame {
static Swiat sw;
static int l;
/**
* Creates new form PokazGrupy
*/
public PokazGrupy(Swiat s, int licznik) {
sw = s;
l = licznik;
initComponents();
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
String[] items = new String[licznik];
for (int i = 0; i < items.length; i++) {
items[i] = sw.grupy.get(i).nazwaGrupy;
}
for (int i=0; i < items.length; i++) {
model.add(i, items[i]);
}
list.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void list1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PokazGrupy(sw, l).setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.List list1;
// End of variables declaration
}
Jak widać próbowałem dodać ta listę, ale jednak nie działa:
public PokazGrupy(Swiat s, int licznik) {
sw = s;
l = licznik;
initComponents();
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
String[] items = new String[licznik];
for (int i = 0; i < items.length; i++) {
items[i] = sw.grupy.get(i).nazwaGrupy;
}
for (int i=0; i < items.length; i++) {
model.add(i, items[i]);
}
list.setVisible(true);
}
Przekazuje do konstruktora Swiat, w którym są te Grupy, oraz licznik, w którym przechowywana jest ilość Grup. Tworzę tę listę. Do tablicy stringów dodaję nazwy Grupy, które zostały utworzone. A następnie tak jak było w przykładzie inicializuję listę tymi nazwami. Jednak, żadna lista nie jest wyświetlana. Co zrobiłem źle?
Nie ma jakiegoś innego sposobu? Ja po prostu chcę wyświetlić te nazwy jak System.out.println();d Tylko żeby to było w okienku.