serializacja listy z obiektami do pliku

0

Witam,

mam problem z serializacją listy zawierającej obiekty do pliku - walczę już z tym drugi dzień, niestety bez większych efektów i tylko coraz większy chaos powstaje w kodzie i frustracja już człowieka bierze.

Niestety przy zamykaniu programu i podczas rzekomej serializacji wyskakuje cała litania error'ów:

gru 21, 2013 4:04:48 PM sl.pc.SLPC jButton5ActionPerformed
SEVERE: null
java.io.NotSerializableException: javax.swing.GroupLayout
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1181)
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:162)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1700)
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:479)
    at java.awt.Container.writeObject(Container.java:3681)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1375)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1171)
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:162)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1700)
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:479)
    at java.awt.Container.writeObject(Container.java:3681)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1375)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1171)
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:162)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1700)
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:479)
    at java.awt.Container.writeObject(Container.java:3681)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1375)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1171)
    at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:162)
    at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1700)
    at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:479)
    at java.awt.Container.writeObject(Container.java:3681)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1541)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1506)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at java.util.ArrayList.writeObject(ArrayList.java:742)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1429)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1175)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at sl.pc.SLPC.SerializacjaBazy(SLPC.java:314)
    at sl.pc.SLPC.jButton5ActionPerformed(SLPC.java:203)
    at sl.pc.SLPC.access$400(SLPC.java:24)
    at sl.pc.SLPC$5.actionPerformed(SLPC.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

Metoda odpowiedzialna za serializacje wygląda następująco (bałagan jest spowodowany licznymi próbami - niby tworzy plik, ma nawet swoje 16 kb, ale niestety nic poza tym):

 private void SerializacjaBazy () throws IOException
    {
        
        fos = new FileOutputStream("d:\\bazyWiedzy.db");
    out = new ObjectOutputStream(fos);
        
        //List<Baza> bazaSerializacja = new ArrayList<>();
        //bazaSerializacja.addAll(bazyWiedzy);
        
    out.writeObject(bazyWiedzy);
        
    out.close();
        
        /*
        ObjectOutputStream wy = new ObjectOutputStream(new FileOutputStream("bazyWiedzy.db"));
        wy.writeObject(bazyWiedzy);
        */
        /*
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("c:\\bazyWiedzy.db"));
        out.writeObject(bazyWiedzy);
        out.close();
        
        System.out.println("1");
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("bazyWiedzy.bw")));
        System.out.println("2");
        out.writeObject(bazyWiedzy);
        System.out.println("3");
        out.close();
        System.out.println("4");
        */
    }

Metoda do odczytu tego i zapisania do zmiennej:

 
private void DeserializacjaBazy () throws IOException, ClassNotFoundException
    {
        
        try
        {
           bazyWiedzy = new ArrayList<>();
           
           fis = new FileInputStream("d:\\bazyWiedzy.db");
           in = new ObjectInputStream(fis);
           
           bazyWiedzy = (ArrayList<Baza>) in.readObject();
           in.close();
            
            
            /*
            ObjectInputStream we = new ObjectInputStream(new FileInputStream("bazyWiedzy.db"));
           bazyWiedzy = (ArrayList<Baza>) we.readObject(); */
        }
        catch (IOException ex)
        {
            
        }
        
        
        
        /*
        try
        {
            try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("bazyWiedzy.bw")))) {
                bazyWiedzy = (ArrayList<Baza>) in.readObject();
            }
        }
        catch (IOException ex)
        {
            System.out.println("dupa");
            bazyWiedzy = new ArrayList<>();
        }
        * */
        
    }

Zmienna kolekcji do przetrzymywania obiektów jest inicjowana w głównej klasie: static public List<Baza> bazyWiedzy = new ArrayList<>();

Mógłby ktoś naprowadzić mnie w czym tkwi problem - zapewne to wynika z mojego niedopatrzenia, Javy dopiero się uczę i posiadam niestety niektóre "złe nawyki" z C#, które mimowolnie potrafię przemycić

0

A ten komunikat

java.io.NotSerializableException: javax.swing.GroupLayout
nic Ci nie mówi? Nie wiem jakiego typu jest serializowany obiekt, na pewno zawiera składową typu GroupLayout, a ta klasa nie implementuje interfejsu Serializable.

0

No właśnie zdaje mi się, że nie zawiera w żadnej klasie nie zostało nic użyte względem swing'a - dalej nie wiem czemu to nie biega.

Oto klasa Baza - ona jest inicjowana i bezpośrednio wrzucana do kolekcji jako obiekt

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sl.pc;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Maksym
 */


public class Baza implements Serializable  {
    
    public String nazwa;
    public List<Pytanie> pytania;
    
    // piekny prosty konstruktor bazy
    public Baza(String nazwa)
    {
        this.nazwa = nazwa;
        this.pytania = new ArrayList<>();
    }
    
    public void DodajPytanie (String pytanie, String odpowiedz,String kategoria)
    {
        Pytanie nowePytanie = new Pytanie(pytanie,odpowiedz,kategoria);
        pytania.add(nowePytanie);
    }
    
    public void UsunPytanie (String pytanieNazwa)
    {
        
        int licznik=0;
        int idRekord = 0;
        
        for (Pytanie rekord : pytania)
        {
            
            if (rekord.pytanie.equals(pytanieNazwa))
            // jest obiekt
            {
                idRekord = licznik;
                break;
            }
                    
                licznik++; 
        }
        
        pytania.remove(idRekord);
        
    }
    
    public void EdytujPytanie (String nazwaPytania, String nowaNazwa, String nowaOdpowiedz, String nowaKategoria)
    {
        
        for (Pytanie rekord : pytania)
        {
            
            if (rekord.pytanie.equals(nazwaPytania))
            {
                rekord.pytanie = nowaNazwa;
                rekord.odpowiedz = nowaOdpowiedz;
                rekord.kategoria = nowaKategoria;
                break;
            }
            
        }
        
    }
    
    public void UsunPytaniaPoKategorii (String nazwaKategorii)
    {
        
        int licznik = 0;
        
        for (Pytanie rekord : pytania)
        {
            
            if (rekord.kategoria.equals(nazwaKategorii))
            {
                pytania.remove(licznik);
                break;
            }
            
            licznik++;
        }
        
    }
    

}

Oraz jeszcze druga klasa powiązana:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sl.pc;

import java.io.Serializable;

/**
 *
 * @author Maksym
 */

public class Pytanie implements Serializable 
{
    
    public String pytanie;
    public String odpowiedz;
    public String kategoria;
    
    // oceny
    // Ocena1 - poprawnie (5)
    // Ocena2 - prawie dobrze (4)
    // Ocena3 - nie dobrze (3)
    // Ocena4 - nie wiem (2)
    // Ocena5 - uzyta podpowiedz (1)
    
    public int ocena1, ocena2, ocena3, ocena4, ocena5;
    
    // w ocenach jest podawana ilosc ocen 
    
    
    public Pytanie(String pytanie, String odpowiedz, String kategoria)
    {
        this.pytanie = pytanie;
        this.odpowiedz = odpowiedz;
        this.kategoria = kategoria;
    }
    
    
    // do ustalania kolejnosci pytania
    // im nizsza srednia, tym mniej potrafimy pojecie - dlatego czesciej musi sie pojawiac, bo sprawia trudnosc
    public double SredniaDlaPytania ()
    {
        return ((ocena1*5)+(ocena2*4)+(ocena3*3)+(ocena4*2)+(ocena5*1))/(ocena1+ocena2+ocena3+ocena4+ocena5);
    }
    
    
    
    
}

a tak wygląda główna klasa programu:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sl.pc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Maksym
 */
public class SLPC extends javax.swing.JFrame  {

    public List<Baza> bazyWiedzy = new ArrayList<>();
    
    private FileOutputStream fos;
    private ObjectOutputStream out;
    private FileInputStream fis;
    private ObjectInputStream in;
    
    
    /**
     * Creates new form SLPC
     */
    public SLPC() throws IOException, ClassNotFoundException {
        
        DeserializacjaBazy();
        
        initComponents();
        
        OdswiezUI();
    }

    /**
     * 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        dbw = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Simple Learn PC");
        setResizable(false);

        dbw.setText("Dodaj baze wiedzy");
        dbw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbwActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox1MouseClicked(evt);
            }
        });

        jLabel1.setText("Istniejace bazy wiedzy:");

        jButton2.setText("Edytuj");
        jButton2.setMaximumSize(new java.awt.Dimension(61, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(61, 25));
        jButton2.setPreferredSize(new java.awt.Dimension(61, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Usuń");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Zarządzanie pytaniami");

        jButton5.setText("Zamknij");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dbw, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dbw)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void dbwActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        

        while (true)
        {
            String nazwa = JOptionPane.showInputDialog("Podaj nazwe:");
            
            if (nazwa != null)
            {
                // sprawdzenie czy nazwa jest unikalna
                boolean jestBaza = false;
                for (Baza rekord : bazyWiedzy)
                {
                    if (rekord.nazwa.equals(nazwa)==true)
                    {
                        jestBaza = true;
                        break;
                    }
                }
                
                if (jestBaza == true)
                {
                    JOptionPane.showMessageDialog(this, "Baza o podanej nazwie juz istnieje! Podaj inna");
                }
                else
                {
                    Baza nowa = new Baza(nazwa) {};
                    bazyWiedzy.add(nowa);
                    break;
                }
                
            }
            
            
        }
        
        OdswiezUI();
        
        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DodajBaze().setVisible(true);
            }
        });
        */
        //this.dbw.setEnabled(false);
        
        
        
    }                                   

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            SerializacjaBazy();
        } catch (IOException ex) {
            Logger.getLogger(SLPC.class.getName()).log(Level.SEVERE, null, ex);
        }
            
            
        
        
        System.exit(0);
        
    }                                        

    private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add your handling code here:
        
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        int wybor = JOptionPane.showConfirmDialog(this,  "Usunąć baze wiedzy " + jComboBox1.getSelectedItem().toString() + " ?");

        if (wybor == JOptionPane.YES_OPTION) 
        {
            UsunBaze(jComboBox1.getSelectedItem().toString());
        }
        
        OdswiezUI();
        
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        
        String wybranaBaza = jComboBox1.getSelectedItem().toString();
        
        while (true)
        {
            String nazwa = JOptionPane.showInputDialog("Podaj nazwe:",jComboBox1.getSelectedItem().toString());
        
            if (nazwa != null)
            {
                for (Baza rekord : bazyWiedzy)
                {
                    if (rekord.nazwa.equals(wybranaBaza))
                    {
                        rekord.nazwa = nazwa;
                        break;
                        
                    }
                }
                break;
            }
            
        }
        
        OdswiezUI();
        
        
        
        
    }                                        

    private void DeserializacjaBazy () throws IOException, ClassNotFoundException
    {
        
        try
        {
           //bazyWiedzy = new ArrayList<>();
           
           fis = new FileInputStream("d:\\bazyWiedzy.db");
           in = new ObjectInputStream(fis);
           
           bazyWiedzy = (List<Baza>) in.readObject();
           
           in.close();
            
            
            /*
            ObjectInputStream we = new ObjectInputStream(new FileInputStream("bazyWiedzy.db"));
           bazyWiedzy = (ArrayList<Baza>) we.readObject(); */
        }
        catch (IOException ex)
        {
            
        }
        
        
        
        /*
        try
        {
            try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("bazyWiedzy.bw")))) {
                bazyWiedzy = (ArrayList<Baza>) in.readObject();
            }
        }
        catch (IOException ex)
        {
            System.out.println("dupa");
            bazyWiedzy = new ArrayList<>();
        }
        * */
        
    }
    
    private void SerializacjaBazy () throws IOException
    {
        
        fos = new FileOutputStream("d:\\bazyWiedzy.db");
    out = new ObjectOutputStream(fos);
        
        //List<Baza> bazaSerializacja = new ArrayList<>();
        //bazaSerializacja.addAll(bazyWiedzy);
        
    out.writeObject(bazyWiedzy);
        out.flush();
        out.close();
        setVisible(false);
    
        
        /*
        ObjectOutputStream wy = new ObjectOutputStream(new FileOutputStream("bazyWiedzy.db"));
        wy.writeObject(bazyWiedzy);
        */
        /*
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("c:\\bazyWiedzy.db"));
        out.writeObject(bazyWiedzy);
        out.close();
        
        System.out.println("1");
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("bazyWiedzy.bw")));
        System.out.println("2");
        out.writeObject(bazyWiedzy);
        System.out.println("3");
        out.close();
        System.out.println("4");
        */
    }
    
    
    private void OdswiezUI ()
    {
        
        jComboBox1.removeAllItems();
        for (Baza rekord : bazyWiedzy)
        {
            String nazwa = (String)rekord.nazwa;
            jComboBox1.addItem(nazwa);
        }
        
        if (jComboBox1.getItemCount() == 0)
        {
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
            jButton4.setEnabled(false);
        }
        else
        {
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            jButton4.setEnabled(true);
        }
        
    }
    
    private void UsunBaze (String nazwa)
    {
        int licznik=0;
        for (Baza rekord : bazyWiedzy)
        {
            if (rekord.nazwa.equals(nazwa))
            {
                break;
            }
            licznik++;
        }
        
        bazyWiedzy.remove(licznik);
        
    }
    
  
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException, ClassNotFoundException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SLPC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SLPC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SLPC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SLPC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        

            
        
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new SLPC().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(SLPC.class.getName()).log(Level.SEVERE, null, ex);
                
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SLPC.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        });
        
        
        
        
    }
    // Variables declaration - do not modify                     
    protected static javax.swing.JButton dbw;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    protected static javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

Będę bardzo wdzięczny za pomoc..

0

Usuń w tym wierszu nawiasy klamrowe.

Baza nowa = new Baza(nazwa){};
0

Dziękuje - działa :) Musze mniej częściej zacząć używać podpowiedzi z NetBeans'a

1 użytkowników online, w tym zalogowanych: 0, gości: 1