简体   繁体   English

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

[英]How to update JList dynamically from another JLIst?

I have two Jlist(blockList and layerList) and I am trying to update the values of the blockList when I click on a different item from the layerList. 我有两个Jlist(blockList和layerList),当我单击来自layerList的另一个项目时,我试图更新blockList的值。 For some reason it is not working. 由于某种原因,它不起作用。 Here is my code: 这是我的代码:

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

} }

When I choose the file the selected index of layersList is 0 which means that the selected item is "Layer 1" and the blockList is showing the correct array. 当我选择文件时,所选的layersList索引为0,这意味着所选项目为“第1层”,并且blockList显示正确的数组。 But when I press item "Layer 2" it is not updating. 但是当我按项目“第2层”时,它没有更新。

Any ideas why ? 有什么想法吗? Thank you in advance! 先感谢您!

The problem is that you're creating a new JList , and setting the data of that: 问题是您要创建一个新的JList并设置其数据:

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

The new list has not been added to the container, so it won't be visible anywhere. 新列表尚未添加到容器中,因此在任何地方都不可见。 You should simply change the data of the already existing JList instead. 您应该简单地更改现有JList的数据。

@chikito1990: Reason is you are instantiating blockList in valueChanged() that is why every time it is re instantiating while you should have one instantiate and for that you should instantiate blockList in the constructor. @ chikito1990:原因是您要在valueChanged()中实例化blockList ,这就是为什么每次重新实例化时都应该有一个实例化,因此您应该在构造函数中实例化blockList

public MainPage() {
...

blockList = new JList();

...

}

try this, it will work. 试试这个,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM