Java 按下tab按钮时更改SelectedIndex

Java 按下tab按钮时更改SelectedIndex,java,swing,button,jtabbedpane,selectedindex,Java,Swing,Button,Jtabbedpane,Selectedindex,我有一个JTabbedPane,其中有一个自定义选项卡,显示文件名,后跟一个按钮。按下按钮后,我想关闭该选项卡。我的第一个想法是(希望)当我点击按钮时,它会首先切换到该选项卡,然后我就可以在该索引处关闭该选项卡。但事实并非如此。我正在寻找一种方法来检索按钮所在的选项卡索引,然后关闭该选项卡。我试着研究并提出了indexOfComponent,但我不确定如何在我的情况下使用它。我的选项卡是动态创建的,因此我将内容存储在向量中,该向量创建JPanel,然后根据所选索引显示当前信息。(选项卡0的所有组

我有一个JTabbedPane,其中有一个自定义选项卡,显示文件名,后跟一个按钮。按下按钮后,我想关闭该选项卡。我的第一个想法是(希望)当我点击按钮时,它会首先切换到该选项卡,然后我就可以在该索引处关闭该选项卡。但事实并非如此。我正在寻找一种方法来检索按钮所在的选项卡索引,然后关闭该选项卡。我试着研究并提出了
indexOfComponent
,但我不确定如何在我的情况下使用它。我的选项卡是动态创建的,因此我将内容存储在向量中,该向量创建JPanel,然后根据所选索引显示当前信息。(选项卡0的所有组件都位于vec[0]内,选项卡1=vec[1]等)

我的TextEdit类的设置如下:

public class TextEdit extends JTabbedPane {
  private Vector<JTextArea> vec; // User can make and edit files
  private Vector<JPanel> tabPanel; // Panel that has a JLabel for fileName and JButton to close file
  private Vector<JLabel> absolutePath; // Path to which file will be saved

  public TextEdit() {
    // Sets up actionlisteners, initializes variables, etc
  }

  protected void AddTab(String fileName) {
    JPanel panel = new JPanel; // Where textarea will be
    panel.setLayout(new GridLayout(0,1));
    JTextArea ta = new JTextArea();
    JScrollPane sp = new JScrollPane(ta);
    panel.add(sp);

    JPanel tp = new JPanel(); // The tab panel
    tp.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 2));
    JLabel label = new JLabel(fileName);
    JButton button = new JButton("X");
    button.addActionListener(CloseOne); // My Action that will close the file
    tp.add(label);
    tp.add(button);

    this.addTab(fileName, panel); // Sets the text area into the tab
    this.setTabComponentAt(this.getSelectedIndex, tp); // Sets the tab to the fileName and button

    vec.add(ta); // Add textarea to vector for future use
    tabPanel.add(tp); // Add the tabpanel for future use. this is where the button is, I need to setSelectedIndex to the tab that the button resides onclick

}
TextEditor类包含我的所有操作,并保存一个TextEdit(选项卡等所在的位置)

My textedit具有设置和获取所选索引的方法


回到indexOfComponent,我需要指定我正在使用的组件,但是,我的组件是动态添加的,我必须找出selectedindex才能进入vector或JPanel,甚至可以使用它。还是我遗漏了什么?有什么建议吗?

在AddTab方法中,将actionCommand设置为关闭按钮,作为新添加选项卡的索引,如下所示

JButton button = new JButton("X");
button.setActionCommand( "" + tabPanel.size() );
button.addActionListener(CloseOne);
在TextEdit类中添加removeTabComponentAtmethod

  public void removeTabComponentAt( int index ) {
       vec.removeElementAt(index );  
       tabPanel.removeElementAt(index );
       this.removeTabAt( index );

  }
在关闭操作中,获取单击按钮的索引

Action CloseOne = new AbstractAction("CloseOne") {

    public Component getTextEdit ( Component comp) {
         if( comp instanceof TextEdit ) {
            return ( TextEdit  ) comp;
         } 
         if( comp.getParent() != null ) {
              return getTextEdit ( comp.getParent() );
         }  else {
              return null;
         }
    }
   @Override
    public void actionPerformed(ActionEvent e) {

         String indexStr = e.getActionCommand();
         int index=-1; 
         try {
              index = Integer.parseInt( indexStr );

         } catch( NumberFormatException ex ) {
         }
         if( index > -1 ) {
            TextEdit textEdit = getTextEdit( ( Component ) e.getSource() ) ;
              if( textEdit  != null ) {
                 textEdit.removeTabComponentAt( index );
              }
         }
    }
};

一个新的计划将大有帮助。否则,如果需要,您可以通过递归调用
getParent()
找到父容器。最好让你的tab JPanel成为它自己的类。不知怎么的,这在谷歌搜索的帮助下起到了作用。我完全忘记了e.getSource。我只需要得到按钮的父级,它得到了JPanel,我可以用它来获得indexOfTabComponent(面板),然后我可以相应地设置SelectedIndex。好。它看起来也是一个复制品
Action CloseOne = new AbstractAction("CloseOne") {

    public Component getTextEdit ( Component comp) {
         if( comp instanceof TextEdit ) {
            return ( TextEdit  ) comp;
         } 
         if( comp.getParent() != null ) {
              return getTextEdit ( comp.getParent() );
         }  else {
              return null;
         }
    }
   @Override
    public void actionPerformed(ActionEvent e) {

         String indexStr = e.getActionCommand();
         int index=-1; 
         try {
              index = Integer.parseInt( indexStr );

         } catch( NumberFormatException ex ) {
         }
         if( index > -1 ) {
            TextEdit textEdit = getTextEdit( ( Component ) e.getSource() ) ;
              if( textEdit  != null ) {
                 textEdit.removeTabComponentAt( index );
              }
         }
    }
};