अनिवार्य रूप से, आपको टैब के लिए "रेंडरर" की आपूर्ति करने की आवश्यकता होगी। अधिक जानकारी के लिए JTabbedPane.setTabComponentAt(...) पर एक नज़र डालें।
मूल विचार टैब पर रखे गए घटक को आपूर्ति करना है।
मैं आम तौर पर एक जेपीनल बनाता हूं, जिस पर मैं एक जेएलएबल (शीर्षक के लिए) जोड़ता हूं और, जो मैं प्रदर्शित करना चाहता हूं उसके आधार पर, किसी प्रकार का नियंत्रण जो करीबी कार्रवाई के रूप में कार्य करता है।
tabPane.addTab(title, tabBody);
int index = tabPane.indexOfTab(title);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
JLabel lblTitle = new JLabel(title);
JButton btnClose = new JButton("x");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
pnlTab.add(lblTitle, gbc);
gbc.gridx++;
gbc.weightx = 0;
pnlTab.add(btnClose, gbc);
tabPane.setTabComponentAt(index, pnlTab);
btnClose.addActionListener(myCloseActionHandler);
अब कहीं और, मैं अब कार्रवाई हैंडलर ...
public class MyCloseActionHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Component selected = tabPane.getSelectedComponent();
if (selected != null) {
tabPane.remove(selected);
// It would probably be worthwhile getting the source
// casting it back to a JButton and removing
// the action handler reference ;)
}
}
}
की स्थापना, तुम बस के रूप में आसानी आप की तरह किसी भी घटक है और उपयोग करने के लिए यह एक माउस श्रोता देते हैं और माउस क्लिक की निगरानी। ..
अपडेट किया गया
ऊपर के उदाहरण केवल वर्तमान में सक्रिय टैब निकाल देंगे, वहाँ वा के एक जोड़े हैं इसे ठीक करने के लिए ys।
सबसे अच्छा शायद कार्रवाई टैब उससे संबद्ध है खोजने के लिए कुछ साधन उपलब्ध कराने के ...
public class MyCloseActionHandler implements ActionListener {
private String tabName;
public MyCloseActionHandler(String tabName) {
this.tabName = tabName;
}
public String getTabName() {
return tabName;
}
public void actionPerformed(ActionEvent evt) {
int index = tabPane.indexOfTab(getTabName());
if (index >= 0) {
tabPane.removeTabAt(index);
// It would probably be worthwhile getting the source
// casting it back to a JButton and removing
// the action handler reference ;)
}
}
}
यह पता लगाने के लिए टैब (JTabbedPane#addTab
के साथ इस्तेमाल किया के रूप में) के नाम का उपयोग करता है और उसके बाद को दूर है टैब और इसके संबंधित घटक ...
यह उपयोगी हो सकता है - http://stackoverflow.com/questions/10620630/add-a-jlabel-in -the-jtabbedpane-header – user1329572
धन्यवाद मैं इसे जांचने जा रहा हूं – Herman