我有两个Jlist(blockList和layerList),当我点击layerList中的另一个项目时,我正在尝试更新blockList的值。由于某种原因,它无法正常工作。这是我的代码:
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,这意味着所选项目为“第1层”,并且blockList显示正确的数组。但是,当我按“第2层”项时,它没有更新。
任何想法为什么? 提前谢谢!
答案 0 :(得分:2)
问题是您正在创建新的JList
,并设置该数据:
public void valueChanged(ListSelectionEvent e) {
blockList = new JList();
...
新列表尚未添加到容器中,因此无法在任何位置显示。
您只需更改已存在的JList
的数据。
答案 1 :(得分:1)
@ chikito1990:原因是你在blockList
中实例化valueChanged()
这就是为什么每次重新实例化时你应该有一个实例化的原因,为此你应该在blockList
中实例化public MainPage() {
...
blockList = new JList();
...
}
构造
{{1}}
试试这个,它会起作用。