2010-11-21 26 views
5

पर एक आइकन प्रदर्शित करें टैब के शीर्षक पर एक आइकन "एक्स" दिखाना संभव है, टैब को बंद करने के लिए उपयोग किया जाता है? धन्यवादजावा टैब्ड पेन: शीर्षक

उत्तर

6

मैं की, How to Use Tabbed Panes ट्यूटोरियल को देखने का सुझाव देते हैं और अनुभाग टैब्स हकदार कस्टम अवयव के साथ के लिए नीचे स्क्रॉल होगा।

यदि आप उस अनुभाग के अंदर example index लिंक देखते हैं, तो नमूना कोड दिया जाता है।

3

मैंने वास्तव में अभी इस के कार्यान्वयन का निर्माण किया है। :)

कुछ इस तरह:

/* These need to be final so you can reference them in the MouseAdapter subclass 
* later. I personally just passed them to a method to add the tab, with the 
* parameters marked as final. 
* i.e., public void addCloseableTab(final JTabbedPane tabbedPane, ...) 
*/ 
final Component someComponent = ...; //Whatever component is being added 
final JTabbedPane tabbedPane = new JTabbedPane(); 
//I had my own subclass of AbstractButton, but that's irrelevant in this case 
JButton closeButton = new JButton("x"); 

/* 
* titlePanel is initialized containing a JLabel with the tab title, 
* and closeButton. (I don't recall the tabbed pane showing a title itself after 
* setTabComponentAt() is called) 
*/ 
JPanel titlePanel = ...; 
tabbedPane.add(someComponent); 
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(someComponent), titlePanel); 

closeButton.addMouseListener(new MouseAdapter() { 
    @Override 
    public void mouseClicked(MouseEvent e) { 
     tabbedPane.remove(someComponent); 
    } 
}); 
+0

ओह, और एक संभावित नकारात्मक पक्ष यह है, setTabComponentAt() जावा 6 के रूप में ही उपलब्ध है ... तो यह पिछले संस्करणों में काम नहीं करेगा। – swilliams

संबंधित मुद्दे