我有兩個Jlist(blockList和layerList),我試圖更新blockList的值,當我點擊layerList中的一個不同的項目。由於某種原因,它不起作用。這是我的代碼:如何從其他JLIst動態更新JList?
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」和塊列表被顯示正確的數組。但是當我按下項目「Layer 2」時,它不會更新。
任何想法爲什麼? 提前謝謝!
謝謝!我現在覺得很愚蠢,因爲它很明顯:)謝謝你的幫助! – chikito1990
@ chikito1990不客氣。有時很難發現明顯的錯誤,因爲作爲我們對他們是盲目的。 – kiheru