从JXTaskPaneContainer中查找并删除JXTaskpane

时间:2012-10-14 15:28:03

标签: java swing swingx jxtaskpane

我有一个模型视图控制器应用程序。该视图包含一个带有多个JXTaskPane的JXTaskContainer。 JXTaskPane有一个删除按钮,可以将其从容器中删除。

如何通过单击按钮自动添加JXTaskpanes,如何找到正确的JXTaskPane然后从容器中删除它?

`enter code here`class Holder extends JFrame {

Arraylist <Section> sectionList = new ArrayList<Section>();
JPanel holderPanel = new JPanel;
JXTaskPaneContainer sectionContainer = new JXTaskPaneContainer();

this.add(holderPanel);

// here goes other stuff



 class AddSectionAction implements ActionListener{

  //actionPerformed
    Section section = new Section();
    section.addActionListener(new DeleteSectionAction);
    sectionList.add(section);
    sectionContainer.add(section);

    holderPanel.add(sectionContainer);
    holderPanel.revalidate();   
    holderPanel.repain();

 }


 class DeleteSectionAction implements ActionListener{

   //actionPerformed

   sectionContainer.remove(THE SECTION I WANT TO REMOVE ); 

 }
}


public class Section extends JXTaskPane {

   JTextArea textArea;
   JButton deleteMe;

   //other stuff here

   public JButton  getDeleteMe{
     return deleteMe;
   }
 }

1 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一点,最简单的方法是将Section的引用传递给DeleteSectionAction侦听器

public class DeleteSectionAction implements ActionListener{
    private Section section;
    public DeleteSectionAction(Section section) {
        this.section = section;
    }

    public void actionPerformed(ActionEvent evt) {
        // Personally, I'd have a "removeSection" method in
        // the container that would also remove the
        // section from the array list...
        sectionContainer.remove(section); 
    }

 }