2013-05-29 3 views
1

각 탭에 닫기 버튼이있는 Java에서 탭 인터페이스를 만들고 싶습니다. 이를 위해 다음 클래스를 사용했습니다. ButtonTabComponentJTabbedPane - 닫기 버튼 참조가있는 탭

새 GUI를 만드는 GUI가있는 버튼이 있습니다. 새 탭 버튼을 4 번 누르면 4 개의 탭이 생성됩니다.

| 탭 0 | 탭 1 | 탭 2 | 탭 3 | 이제

를 (각 탭 닫기 버튼 포함), 나는 탭 1을 닫으려는와 문제가 내가 중간 탭을 폐쇄하고 때, 모든 인덱스가 다시 정렬되어 나타납니다 결정 -을 의미한다 :

| 탭 0 | 탭 2 | 탭 3 | 나는 새 탭을 만들 지금하려고하면

탭이 만들어 지지만 새 탭 (탭 2는 인덱스 1이됩니다) :

을 | 탭 0 | 탭 2 | 탭 3 | 탭 1 | |

을 : 내가 다시 새 탭을 클릭하면

, 나는 수 (탭 1 닫기 버튼이 없습니다) 탭 0 | 탭 2 | 탭 3 | 탭 1 | 탭 4 | 내가 탭 2를 종료하기로 결정 지금

내가 얻을 (탭 4는 닫기 버튼을 가지고, 괜찮습니다) :

을 | 탭 0 | 탭 3 | 탭 1 | 탭 4 | 나는 새로운 탭을 생성하는 경우

(탭 3 이제 인덱스 1이있을 것이다) :

을 | 탭 0 | 탭 3 | 탭 1 | 탭 4 | 탭 1 | (마지막 탭에는 닫기 버튼이 없습니다).

나는이가 인덱스에 의해 발생한다고 생각하고 여기에 유래에 비슷한 질문에 읽기 : stackoverflow.com/questions/15312252/jtabbedpane-arrayindexoutofboundsexception에 가능한 솔루션이 될 것이라고 :

이 탭 항목에 대한 참조를하지 패스의 탭 패널의 인덱스를 청취자에게 보냅니다.

어떻게해야할지 모르겠다. 누구든지 힌트가 있다면 정말 감사 하겠어. 각 탭마다 파일을 열고 파일에 저장할 수있는 기능이 있으므로 분명히 탭 인덱스가 안정적이지 않으므로 각 탭에 대한 견고한 참조를 유지해야합니다.

편집 : 나는 내 코드에서 새 탭을 추가하는 방법입니다 : 내가 코드를 확인하고 있었고, 내가 actionPerformed 메소드에

pane.remove(pane.getSelectedComponent()); 

을 사용하여 수행 할 수

...//main class 
private final JTabbedPane pane = new JTabbedPane(); 
//I am using an array to store the tabs created 
//I initialize the array with false. the idea was that when a new tab get created, one item in the array 
//gets the true value. when the tab is closed, the array item (based on the index) is set back to false 
arrTabList = new boolean[10]; 
     for(int i=0; i<10; i++){ 
      arrTabList[i] = false; 
     } 

... 


public void newFile() //this function opens a new tab 
{ 
//parse the array to check the first item with false status 
    for(int i=0; i<10; i++){ 
     if(!arrTabList[i]) { 
      System.out.println("false"); 
      PanelCounter = i; 
      break; 
      } 
    } 
    newTab t = new newTab(); //object which contains the tab content (a bunch of graphical components, input fields mostly) 
    pane.add("New Entry" + PanelCounter, t.createContentPane()); //adds the new tab to the main window 
    pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this)); 
    arrTabList[PanelCounter] = true; //sets the item array to true 
} 

//when a tab is closed, this function is called in the listener: 
public void decreaseCounter(int i) 
{ 
     arrTabList[i] = false; //sets the item array back to false 
} 
+0

인용 된 제안은 여기에 적용되지 않습니다. 'ButtonTabComponent'는 탭 인덱스를 내부적으로 저장하지 않지만 필요시 조회합니다. 오류는 새 탭을 추가하는 방식 때문일 수 있습니다. 그 코드를 보여줄 수 있습니까? – Arend

+0

코드가 추가되었습니다. 선택한 탭에 영향을주는 귀하의 입력 – Laur8

답변

1

오류는 호출 당신은 탭 인덱스 PanelCounter에하지만 방금 만든 하나에 버튼을 추가하지 않으

pane.setTabComponentAt(PanelCounter, new ButtonTabComponent(pane, this)); 

에있다. 인덱스는이 시점에서 당연히 따라서, 너무 높은 하나입니다 getTabCount()를 사용하여 얻을 수 있습니다

pane.setTabComponentAt(pane.getTabCount()-1, new ButtonTabComponent(pane, this)); 
+0

그게 다야! 고마워요 !! – Laur8

0

.

+0

주셔서 감사합니다. 탭이 선택되었는지 여부에 관계없이이 작업을 수행하려고합니다. 선택하지 않아도 탭을 닫을 수 있어야합니다. 감사합니다 – Laur8

0

여기에 내가 만드는 오전 JTabbedPane에 탭을, 각 탭 닫기 버튼과 함께. 닫기 버튼을 클릭하면 해당 탭만 닫힙니다.

//This is where a tab is created in some other function of same class 
jtp.addTab("Create",new JPanel()); //jtp is a global JTabbedPane variable 
int index = jtp.indexOfTab("Create"); 
jtp.setTabComponentAt(index,createTabHead("Create")); 

public JPanel createTabHead(String title) 
{ 
    final String st=title; 
    JPanel pnlTab = new JPanel(); 
    pnlTab.setLayout(new BoxLayout(pnlTab,BoxLayout.LINE_AXIS)); 
    pnlTab.setOpaque(false); 
    JButton btnClose = new JButton("x"); 
    JLabel lblTitle = new JLabel(title+" "); 
    btnClose.setBorderPainted(false); 
    btnClose.setOpaque(false); 

    btnClose.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      int i; 
      for(i=0;i<=jtp.getTabCount()-1;i++)//To find current index of tab 
      { 
      if(st.equals(jtp.getTitleAt(i))) 
        break; 
      }     
       jtp.removeTabAt(i); 
      } 
      }); 

    pnlTab.add(lblTitle); 
    pnlTab.add(btnClose); 
    return pnlTab; 
}