gpt4 book ai didi

java - 更新滚动 Pane 的模型不会更新窗口

转载 作者:行者123 更新时间:2023-12-01 10:19:41 24 4
gpt4 key购买 nike

使用javax.swing,我创建了一个显示列表的窗口。该列表有两个按钮,允许删除或添加列表中显示的商店商品。商店元素存储在文件中。我想在单击此类按钮时,列表通过列出其指定目录中的所有文件来更新自身(由于某些原因,我需要此方法,而不是直接使用 model.remove() 修改列表。)但是,当我执行 model = new DefaultListModel() 时,列表根本不会自行更新。以下是显示商店商品列表的面板代码。

public class ShopWindowPanel extends JPanel
{
private UserInterface window;
private JList<String> jList;
private DefaultListModel<String> model;
private JButton addButton;
private JButton deleteButton;
private ArrayList<ShopItem> arrayList;
private JScrollPane jScrollPane;

/**
* Constructor method.
* @param window The window linked to the shop window panel. It is used for error pop-ups.
*/
public ShopWindowPanel(UserInterface window) {
this.window = window;
initializePanel();
}

/**
* This method initializes the panel.
*/
public void initializePanel()
{
initializeButtons();
initializeScrollPane();

add(jScrollPane);

//setting up the layout of the panel
GroupLayout gl = new GroupLayout(this);
this.setLayout(gl);

gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);

gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
.addComponent(jScrollPane)
.addGroup(gl.createParallelGroup()
.addComponent(addButton)
.addComponent(deleteButton)
));

gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(jScrollPane)
.addGroup(gl.createSequentialGroup()
.addComponent(addButton)
.addComponent(deleteButton)
));

gl.linkSize(addButton, deleteButton);

window.pack();
}

/**
* This method initializes the buttons.
*/
public void initializeButtons() {

addButton = new JButton("Add");
deleteButton = new JButton("Delete");

addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

String text = JOptionPane.showInputDialog("Add a new item and its price in pound (item name, price");

try
{
String[] itemDetails = text.split(", ");
ShopItem shopItemToAdd = new ShopItem();
shopItemToAdd.setName(itemDetails[0]);
shopItemToAdd.setPrice(Integer.parseInt(itemDetails[1]));
shopItemToAdd.save(); //create a new file
initializeScrollPane(); //supposed to update the scroll pane
}
catch(Exception exception)
{

JOptionPane.showMessageDialog(window.getContentPane(), "An error has happened!" + exception.getClass() + ": " + exception.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

deleteButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent event) {

ListSelectionModel selmodel = jList.getSelectionModel();
int index = selmodel.getMinSelectionIndex();
if (index >= 0) {
arrayList.get(index).delete(); //delete the file
initializeScrollPane(); //supposed to update the scroll pane
}
}

});
}

/**
* This method initializes the list of the items.
*/
public void initializeScrollPane() {
//listing all shop items
model = new DefaultListModel<String>();
File shopItemFileFolder = new File(ShopItem.shopItemsFolder);
File[] shopItemsFiles = shopItemFileFolder.listFiles();
arrayList = new ArrayList<ShopItem>();
for(int i = 0; i<shopItemsFiles.length; i++)
{
try
{
ShopItem currentlyProcessedShopItem = new ShopItem();
currentlyProcessedShopItem.load(shopItemsFiles[i].getName());

model.addElement(currentlyProcessedShopItem.getName() + ": £" + (float) currentlyProcessedShopItem.getPrice()/100);
arrayList.add(currentlyProcessedShopItem);
}
catch(Exception e)
{
System.err.println("A problem has happened with an item.");
}
}

jList = new JList<>(model);
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

jScrollPane = new JScrollPane(jList);
}
}

最佳答案

创建一个单独的方法,它的作用不仅仅是生成ListModel...

public ListModel<String> initializeListModel() {
//listing all shop items
model = new DefaultListModel<String>();
File shopItemFileFolder = new File(ShopItem.shopItemsFolder);
File[] shopItemsFiles = shopItemFileFolder.listFiles();
arrayList = new ArrayList<ShopItem>();
for (int i = 0; i < shopItemsFiles.length; i++) {
try {
ShopItem currentlyProcessedShopItem = new ShopItem();
currentlyProcessedShopItem.load(shopItemsFiles[i].getName());

model.addElement(currentlyProcessedShopItem.getName() + ": £" + (float) currentlyProcessedShopItem.getPrice() / 100);
arrayList.add(currentlyProcessedShopItem);
} catch (Exception e) {
System.err.println("A problem has happened with an item.");
}
}
return model;
}

public void initializeScrollPane() {
jList = new JList<>(model);
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

jScrollPane = new JScrollPane(jList);
}

然后在按钮的 ActionListener 中,调用此方法并将结果应用到您的 JList...

    addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

String text = JOptionPane.showInputDialog("Add a new item and its price in pound (item name, price");

try {
String[] itemDetails = text.split(", ");
shopItemToAdd.setName(itemDetails[0]);
shopItemToAdd.setPrice(Integer.parseInt(itemDetails[1]));
shopItemToAdd.save(); //create a new file
jList.setModel(initializeListModel());
} catch (Exception exception) {

JOptionPane.showMessageDialog(ShopWindowPanel.this, "An error has happened!" + exception.getClass() + ": " + exception.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

deleteButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent event) {

ListSelectionModel selmodel = jList.getSelectionModel();
int index = selmodel.getMinSelectionIndex();
if (index >= 0) {
jList.setModel(initializeListModel());
}
}

});

关于java - 更新滚动 Pane 的模型不会更新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35682639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com