2 Replies Latest reply: Aug 28, 2012 3:15 PM by mendescholl RSS

    Add JButton to JTabbedPane tab

    mendescholl
      Adding a JButton to a JTabbedPane tab is well documented. However, is there a way to make sure that the JButton completely fills the tab area so that when the user clicks on JButton it is not possible to click on the tab area.
        • 1. Re: Add JButton to JTabbedPane tab
          Nirvana
          Could you please elaborate more on your query as in what exactly you are looking for.


          import java.awt.BorderLayout;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.JTabbedPane;


          public class TabbedPaneExample extends JFrame
          {
               private static final long serialVersionUID = 1L;

               TabbedPaneExample()
               {
                    JTabbedPane tabbedPane = new JTabbedPane();
                    setTitle( "Tabbed Pane Application" );
                    setSize( 300, 200 );
                    
                    JPanel topPanel = new JPanel();
                    topPanel.setLayout( new BorderLayout() );
                    getContentPane().add( topPanel );
                    
                    JPanel panel = new JPanel();
                    JButton button = new JButton("Click!");
                    panel.add(button);
                    tabbedPane.addTab("Hello",panel);
                    
                    topPanel.add( tabbedPane, BorderLayout.CENTER );
                    
                    
               }

               
               public static void main(String[] args)
               {
                    TabbedPaneExample obj = new TabbedPaneExample();
                    obj.setVisible(true);

               }

          }
          • 2. Re: Add JButton to JTabbedPane tab
            mendescholl
            Thanks for the short code sample.
            In the tab, where you have the text "Hello", if that was a JButton which filled the whole tab(i.e. where the word "Hello" is) then that is exactly what I am trying to achieve. It should not be possible to activate the ChangeListener for this tab. Other tabs should be unaffected and should operate as normal.