2012-07-19 18 views
25

मैं एक JTabbedPane के साथ काम कर रहा हूं, मुझे वर्तमान में बंद करने के लिए टैब में एक करीबी बटन जोड़ने की आवश्यकता है।JTabbedPane टैब पर बंद बटन कैसे जोड़ें?

मैं खोज की है और के रूप में मैं समझता हूँ कि मैं JPanel से विस्तार करने और बंद करें बटन जोड़ने के रूप में वे कहते हैं कि here लेकिन, JTabbedPane या वहाँ यह करने के लिए एक आसान तरीका है का विस्तार करीब बटन जोड़ने के लिए एक रास्ता है चाहिए?

अग्रिम धन्यवाद, मैं वास्तव में आपके समय और आपकी सहायता की सराहना करता हूं।

+1

यह उपयोगी हो सकता है - http://stackoverflow.com/questions/10620630/add-a-jlabel-in -the-jtabbedpane-header – user1329572

+0

धन्यवाद मैं इसे जांचने जा रहा हूं – Herman

उत्तर

39

अनिवार्य रूप से, आपको टैब के लिए "रेंडरर" की आपूर्ति करने की आवश्यकता होगी। अधिक जानकारी के लिए 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 के साथ इस्तेमाल किया के रूप में) के नाम का उपयोग करता है और उसके बाद को दूर है टैब और इसके संबंधित घटक ...

+0

धन्यवाद !!! ये बहुत अच्छा दिखता है। मैं इसे दोपहर में जांचने जा रहा हूं। मैं वास्तव में आपकी मदद की सराहना करता हूं – Herman

+1

@ हरमन कोई जांच :) – MadProgrammer

+1

यह कोशिश है। यदि आप कोड पढ़ते हैं, तो एक्शनलिस्टर चयनित टैब का उपयोग कर रहा है। आप टैब घटक को एक्शन श्रोता के साथ जोड़ते हैं, इंडेक्स ढूंढते हैं और इसके बजाय इसे हटाते हैं – MadProgrammer

6

मुझे एक टैब उदाहरण मिला (जावा साइट से) जो ऐसा लगता है, कम से कम उनके में। (हालांकि मैंने सोचा था, जब मैंने अतीत में कोशिश की, तो उसने वर्तमान में चयनित टैब को भी बंद कर दिया, हालांकि जब आप अपना उदाहरण चलाते हैं तो यह ठीक से काम करता है, हालांकि मुझे लगता है कि जब मैंने इसे टैब्ड जावा नोटपैड पर काम करने के लिए अपडेट किया था, तो यह बंद हो रहा था वर्तमान में, टैब का चयन किया है, हालांकि हो सकता है मैं गलत किया था।

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java

हाँ, मेरे बात अब काम कर रहा है! यह, बल्कि वर्तमान में चयनित टैब से उम्मीद है कि आप वास्तविक टैब के लिए काम करेगा!

+0

हालांकि, आपको पता होना चाहिए कि ऐसा करके, यह टैब आइकन सेट करने की आपकी क्षमता को खंडित करता है। (जब तक आप निम्न स्तरों के साथ जाना और गड़बड़ नहीं करना चाहते हैं और स्रोत कोड को देखते हैं और पेंट कॉम्पोनेंट() और सभी मजेदार सामान करते हैं, तो मैं उस मुद्दे के आसपास एक रास्ता नहीं देख सकता।) – MongooseLover

0

आपके प्रश्न का उत्तर मिला है। मैं एक लिंक देना चाहता हूं जो मेरे लिए बहुत उपयोगी था।

JTabbedPane with a close button

यहां कुछ कोड भी है।

public static void createAndShowGUI() 
{ 
    JFrame frame = new JFrame("Tabs"); 
    frame.setMinimumSize(new Dimension(500, 200)); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JTabbedPane tabbedPane = new JTabbedPane(); 

    JPanel panel = new JPanel(); 
    panel.setOpaque(false); 
    tabbedPane.add(panel); 
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Tab1")); 

    JPanel panel1 = new JPanel(); 
    panel1.setOpaque(false); 
    tabbedPane.add(panel1); 
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel1), getTitlePanel(tabbedPane, panel1, "Tab2")); 

    JPanel panel2 = new JPanel(); 
    panel2.setOpaque(false); 
    tabbedPane.add(panel2); 
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel2), getTitlePanel(tabbedPane, panel2, "Tab3")); 

    JPanel panel3 = new JPanel(); 
    panel3.setOpaque(false); 
    tabbedPane.add(panel3); 
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel3), getTitlePanel(tabbedPane, panel3, "Tab4")); 

    frame.add(tabbedPane); 

    // Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 
0

मैंने ऑरैकल के कोड में कुछ बदलाव किए हैं।

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java

टैब, के साथ साथ करीब टैब बटन के लिए एक आइकन जोड़ने के लिए संभावना दे। उम्मीद है की वो मदद करदे।

public static void addTag(JTabbedPane tab, String title, Icon icon, int index){ 
    MouseListener close = new MouseAdapter() { 

     @Override 
     public void mouseClicked(MouseEvent e) { 
      //your code to remove component 
      //I use this way , because I use other methods of control than normal: tab.remove(int index); 
     } 

    }; 
    final ButtonClose buttonClose = new ButtonClose (title, icon, close); 

    tab.setTabComponentAt(index, buttonClose); 
    tab.validate(); 
    tab.setSelectedIndex(index); 

}

सार्वजनिक वर्ग ButtonClose JPanel {

public ButtonClose(final String title, Icon icon, MouseListener e) { 
    JLabel ic = new JLabel(icon); 
    ic.setSize(icone.getIconWidth(), icone.getIconHeight()); 

    JLabel text= new JLabel(title); 
    text.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); 

    ButtonTab button = new ButtonTab(); 
    button.addMouseListener(e); 
    button.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); 

    JPanel p = new JPanel(); 
    p.setSize(getWidth() - icone.getIconWidth(), 15); 
    p.add(text); 
    p.add(button); 

    add(ic); 
    add(p); 
} 

private class ButtonTab extends JButton { 

    public ButtonTab() { 
     int size = 13; 
     setPreferredSize(new Dimension(size, size)); 
     setToolTipText("Close"); 

     setUI(new BasicButtonUI()); 

     setFocusable(false); 
     setBorderPainted(false); 

     addMouseListener(listener); 
     setRolloverEnabled(true); 
    } 

    @Override 
    public void updateUI() { 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g.create(); 

     if (getModel().isPressed()) { 
      g2.translate(1, 1); 
     } 
     g2.setStroke(new BasicStroke(2)); 
     g2.setColor(new Color(126, 118, 91)); 

     if (getModel().isRollover()) { 
      g2.setColor(Color.WHITE); 
     } 

     int delta = 3; 
     g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1); 
     g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1); 
     g2.dispose(); 
    } 
} 

private final MouseListener listener = new MouseAdapter() { 
    @Override 
    public void mouseEntered(MouseEvent e) { 
     Component component = e.getComponent(); 
     if (component instanceof AbstractButton) { 
      AbstractButton button = (AbstractButton) component; 
      button.setContentAreaFilled(true); 
      button.setBackground(new Color(215, 65, 35)); 
     } 
    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     Component component = e.getComponent(); 
     if (component instanceof AbstractButton) { 
      AbstractButton button = (AbstractButton) component; 
      button.setContentAreaFilled(false); //transparent 
     } 
    } 
}; 

}

0

चेक बाहर पीटर-स्विंग here फैली हुई है। इसमें JClosableTabbedPane कक्षा, साथ ही साथ कई अन्य हैं।

जब आप jar फ़ाइल डाउनलोड करते हैं तो आप इसे चला सकते हैं और सभी कक्षाओं के उदाहरण प्राप्त कर सकते हैं।

0

आप एक JLabel नाम दिया जा सकता है "x" और का उपयोग mouseListener

private final JLabel l = new JLabel(); // this is the label for tabbedPane 
private final JLabel b = new JLabel("x");//Close Button 
if (closeable) 
     { 
      b.setToolTipText("Click to close"); 

      b.setOpaque(false); 
      b.setBackground(Color.gray); 

      b.addMouseListener(new MouseAdapter() 
      { 
       @Override 
       public void mouseExited(MouseEvent e) 
       { 
        b.setBorder(bordere); 
        b.setOpaque(false); 
       } 

       @Override 
       public void mouseEntered(MouseEvent e) 
       { 
        b.setBorder(borderl); 
       } 

       @Override 
       public void mouseReleased(MouseEvent e) 
       { 
        b.setOpaque(false); 
        b.repaint(); 

        if (b.contains(e.getPoint())) 
        { 
         b.setBorder(borderl); 

         if (confirmTabClosing()) 
         { 
          tab.remove(tabIndex()); 
          if(tab.getTabCount() == 0) 
           spacialTabComponent.maximizeOrRestore.doClick(); 
         } 
        } 
        else 
         b.setBorder(bordere); 

       } 

       @Override 
       public void mousePressed(MouseEvent e) 
       { 
        b.setOpaque(true); 
        b.repaint(); 
       } 
      }); 

      b.setBorder(bordere); 
      add(b, getLeftAlignedBothFilledGBC(1, 0, new Insets(0, 0, 0, 0), 0, 0)); 
     } 



    } 
0
jbCloseButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       int index = jtbMainTabbedPane.indexOfTabComponent(jbCloseButton); 
       jtbMainTabbedPane.remove(index); 
      } 
});