gpt4 book ai didi

java - 如何从另一个 JLIst 动态更新 JList?

转载 作者:行者123 更新时间:2023-12-01 23:10:54 30 4
gpt4 key购买 nike

我有两个 Jlist( block 列表和层列表),当我单击层列表中的不同项目时,我尝试更新 block 列表的值。由于某种原因它不起作用。这是我的代码:

  JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;
JList layerList;
JList blockList;
DefaultListModel model;
String[] layers = {"Layer 1", "Layer 2", "Layer 3", "Layer 4", "Layer 5"};
String[] blocksListMenu1 = {"Block 1", "Block 2", "Block 3", "Block 4", "Block 5"};
String[] blocksListMenu2 = {"Block 6", "Block 7", "Block 8", "Block 9", "Block 10"};
String[] blocksListMenu3 = {"Block 11", "Block 12", "Block 13", "Block 14", "Block 15"};
String[] blocksListMenu4 = {"Block 16", "Block 17", "Block 18", "Block 19", "Block 20"};
String[] blocksListMenu5 = {"Block 21", "Block 22", "Block 23", "Block 24", "Block 25"};

public MainPage() {
super(new BorderLayout());

JPanel content = new JPanel(new BorderLayout());

final JPanel layerPanel = new JPanel(new BorderLayout());
layerPanel.setBackground(Color.WHITE);
layerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JLabel layerLabel = new JLabel("Layers:");
layerList = new JList(layers);
layerList.setSelectedIndex(0);
layerList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
blockList = new JList();
String selectedLayer = layers[layerList.getSelectedIndex()];
if(selectedLayer.equals("Layer 1"))
blockList.setListData(blocksListMenu1);
else if(selectedLayer.equals("Layer 2"))
blockList.setListData(blocksListMenu2);
else if(selectedLayer.equals("Layer 3"))
blockList.setListData(blocksListMenu3);
else if(selectedLayer.equals("Layer 4"))
blockList.setListData(blocksListMenu4);
else if(selectedLayer.equals("Layer 5"))
blockList.setListData(blocksListMenu5);;
}
});

layerPanel.add(layerLabel, BorderLayout.PAGE_START);
layerPanel.add(layerList, BorderLayout.CENTER);
layerPanel.setVisible(false);

//--------------------------------------------------------------------------

final JPanel blockPanel = new JPanel(new BorderLayout());
blockPanel.setBackground(Color.WHITE);
blockPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

JLabel blockLabel = new JLabel("Blcoks:");
blockList = new JList(blocksListMenu1);
blockPanel.add(blockLabel, BorderLayout.PAGE_START);
blockPanel.add(blockList, BorderLayout.CENTER);
blockPanel.setVisible(false);

final JTextField path = new JTextField();
JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("DXF Files", "dxf");
fileChooser.setFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
path.setText(selectedFile.getPath());
layerPanel.setVisible(true);
blockPanel.setVisible(true);
}
}
});

content.add(path, BorderLayout.CENTER);
content.add(button, BorderLayout.LINE_END);
content.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
add(content, BorderLayout.PAGE_START);
add(layerPanel, BorderLayout.LINE_START);
add(blockPanel, BorderLayout.LINE_END);

}

当我选择文件时,layersList 的所选索引为 0,这意味着所选项目为“Layer 1”,并且 blockList 显示正确的数组。但是当我按下项目“第 2 层”时,它没有更新。

有什么想法吗?预先感谢您!

最佳答案

问题是您正在创建一个新的 JList,并设置其中的数据:

public void valueChanged(ListSelectionEvent e) {
blockList = new JList();
...

新列表尚未添加到容器中,因此在任何地方都看不到它。您只需更改现有 JList 的数据即可。

关于java - 如何从另一个 JLIst 动态更新 JList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21983152/

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