如何将复选框和图像添加到JList

时间:2014-06-30 21:14:39

标签: java swing jlist listselectionlistener

以下代码在JList中显示MRI图像。我想为每个图像添加一个复选框,以便用户可以选择所需的图像,然后对所有图像执行计算。我似乎无法为图片添加复选框。我试过了

list.setCellRenderer(new CheckBoxListRenderer); 

但它没有识别checkboxlistrenderer。有人知道我会这样做吗?

   DefaultListModel<ImageIcon> model = new DefaultListModel<ImageIcon>();
      JCheckBox check;
      int counter = 0;
      for(int i =0; i< image.getStackSize(); i++){
          image.setSliceWithoutUpdate(i);
          ImageIcon c = new ImageIcon();
          c.setImage(image.getBufferedImage());
          model.addElement(c);
          counter++;
        }
        final int finalCounter = counter;

        image.setPosition(1);
        JList<ImageIcon> list = new JList<ImageIcon>(model);

        list.addListSelectionListener(new ListSelectionListener(){

            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                // TODO Auto-generated method stub
                if(arg0.getValueIsAdjusting() ==false)
                    return;

                JList l = (JList) arg0.getSource();

                if(l.isSelectionEmpty())
                    System.out.println("List is empty!");

                int index = ((JList)arg0.getSource()).getSelectedIndex();
                StringBuilder sb = new StringBuilder();
                sb.append("Image #: ");

                for(int i =0; i< image.getStackSize();i++){
                    if(index == i){
                        sb.append(i+1);
                        sb.append("/");
                        sb.append(finalCounter);
                        String strI = sb.toString();
                        txt.setText(strI);
                        image.setSlice(i);
                        frame.setImage(image);
                    }
                }

            }



        });


        JScrollPane listScroll = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScroll, panel2);

0 个答案:

没有答案