0

I'm working in a little program that allows you to place things on a map, give it a name or description. The places can have a category that you choose from a JList with a defaultListModel. If I have created some categories and choose to save my project and continue some other time, when I load the saved file everything loads perfect except my categorylist. It's still blank, and if I have a project with categories and load a new project the old categories is still there.. I can't figure out whats wrong, I've must have missed some update here?

package test;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.*;
import java.util.List;
import java.awt.event.*;
import java.io.*;
import java.io.File;
import java.util.*;


public class Testar extends JFrame {


JComboBox place;

JTextField searchField;
JButton searchButton, hideButton, deletePlaceButton, whatIsHere, hideCat, newCat, delCat;
JFileChooser jfc = new JFileChooser(".");
boolean changed = false;

DefaultListModel<PlaceCategory> dataModel = new DefaultListModel<>();
JList<PlaceCategory> categoryList = new JList<PlaceCategory>(dataModel);

//  DefaultListModel<PlaceCategory> NewDataModel = new DefaultListModel<>();

Testar(){
    super("test");

    //FILEMENU  TOPP
    JMenuBar fileBar = new JMenuBar();
    setJMenuBar(fileBar);

    JMenu archive = new JMenu("File");
    fileBar.add(archive);

    JMenuItem open = new JMenuItem("Open");
    archive.add(open);
    open.addActionListener(new OpenLis());

    JMenuItem save = new JMenuItem("Save");
    archive.add(save);
    save.addActionListener(new SaveLis());

    //kategorier ÖST

    JPanel east = new JPanel();
    add(east, BorderLayout.CENTER);

    east.add(new JLabel("Categories"));

    categoryList.setVisibleRowCount(3);
    categoryList.setFixedCellWidth(50);
    east.add(new JScrollPane(categoryList));
    categoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    //ACTIONLISTENER

    hideCat = new JButton("Hide category");
    east.add(hideCat);

    newCat = new JButton("New category");
    east.add(newCat);
    newCat.addActionListener(new NewCatLis());

    delCat = new JButton("Delete category");
    east.add(delCat);

    BoxLayout eastLayout = new BoxLayout(east, BoxLayout.Y_AXIS);
    east.setLayout(eastLayout);

    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(300,300);
    setVisible(true);
    setLocationRelativeTo(null);
    setResizable(false);
}


class NewCatLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        String categoryName;
        Color color = Color.BLACK;

        categoryName = JOptionPane.showInputDialog(Testar.this, "Name on category");
        color = JColorChooser.showDialog(Testar.this,"Chooser color", color);
        PlaceCategory pc = new PlaceCategory(categoryName, color);
        dataModel.addElement(pc);
    }
}

class OpenLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        int answer = jfc.showSaveDialog(Testar.this);
        if(answer != JFileChooser.APPROVE_OPTION){
            return;
        }

        File file = jfc.getSelectedFile();
        String filename = file.getAbsolutePath();

        try{

            FileInputStream fis = new FileInputStream(filename);
            ObjectInputStream ois = new ObjectInputStream(fis);

            dataModel = (DefaultListModel)ois.readObject();

            System.out.println(dataModel);

//              NewDataModel = dataModel;
//              
//              dataModel.clear();
//              
//              for(int i=0; i < NewDataModel.size(); i++){
//                  PlaceCategory pc = NewDataModel.get(i); 
//                  dataModel.addElement(pc);
//                  System.out.println("addar");
//              }
//              
//              NewDataModel.clear();


            ois.close();
            fis.close();
            pack();
            validate();
            repaint();
            changed = false;

        } catch(ClassNotFoundException e){
            JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
        } catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(Testar.this, "Can not open the file...");
        } catch(IOException e){
            JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
        }
    }
}

class SaveLis implements ActionListener{
    public void actionPerformed(ActionEvent ave){
        int answer = jfc.showSaveDialog(Testar.this);
        if(answer != JFileChooser.APPROVE_OPTION){
            return;
        }

        File file = jfc.getSelectedFile();
        String filename = file.getAbsolutePath();

        try{
            FileOutputStream fos = new FileOutputStream(filename);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(dataModel);

            oos.close();
            fos.close();

            changed = false;

        } catch(FileNotFoundException e){
            JOptionPane.showMessageDialog(Testar.this, "Can not open the file...");
        } catch(IOException e){
            JOptionPane.showMessageDialog(Testar.this, "Something went wrong... "+e.getMessage());
        }
    }
}

public static void main(String[] args){
    new Testar();
}
}

and my placecategory ofc!

package test;


import java.awt.*;
import java.io.*;

public class PlaceCategory implements Serializable {

private String name;
public Color color;


public PlaceCategory(String name, Color color){
    this.name = name;
    this.color = color;

}

public String toString(){
    return name;
}

public Color getColor() {

    return color;
}

public String getName(){
    return name;
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Filip Blom
  • 37
  • 1
  • 7

1 Answers1

0

Found a way to make it happen! added this after load:

categoryList.setModel(dataModel);
Filip Blom
  • 37
  • 1
  • 7