【问题标题】:My new dataModel doesn't replace the old one in my JList我的新数据模型不会替换 JList 中的旧数据模型
【发布时间】:2015-09-12 06:36:33
【问题描述】:

我正在开发一个小程序,它允许您在地图上放置东西,给它一个名称或描述。这些地方可以有一个类别,您可以从具有 defaultListModel 的 JList 中选择。如果我创建了一些类别并选择保存我的项目并在其他时间继续,当我加载保存的文件时,除了我的类别列表之外,一切都加载完美。它仍然是空白的,如果我有一个带有类别的项目并加载一个新项目,旧的类别仍然存在。我不知道出了什么问题,我一定错过了这里的一些更新?

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();
}
}

还有我的地点类别 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;
}
}

【问题讨论】:

  • 如果你能包含一些可编译的代码,对你的帮助会容易得多。
  • 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。
  • @AndrewThompson 加了一个小号
  • @Amber 加了一个小号
  • 当我保存时,关闭程序..然后打开程序并加载旧的保存。它加载,因为我可以在打印中看到内容。但它没有显示在我的 JList 中

标签: java swing jlist datamodel defaultlistmodel


【解决方案1】:

找到了实现它的方法!加载后添加:

categoryList.setModel(dataModel);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多