Results 1 to 13 of 13
  1. #1
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default How to add a jbutton to tabbed pane headder?

    I need to have a jbutton in a tabbed pane. Actually i need three tabs in the tabbed pane and a button next to the tabbed pane headder as we see in the eclipse. Kindly help me regarding this.

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Do you have any code for this project?

  3. #3
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Sorry I am struggling with starting the execution. I want to know, is there a way to add button to the tabbed pane. In tabbed pane we usually add panels which will display as tabs. I want to add a button in the place where the the tab icon is displayed.

    Thank you.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    You can add the button to the panel and then add the panel.

  5. #5
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Actually I dont need the button in the pannel. I need the button in the tabbed pane itself.

    Thank you.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Actually I dont need the button in the pannel. I need the button in the tabbed pane itself.
    This doesn't make sense. Norm (I think) is talking about using a JPanel that is held by the tabbed pane, perhaps BorderLayout.CENTER, so that the JPanel actually becomes the tabbed pane, except for the tab. Are you talking about adding a JButton on the tab itself? Could you go into more detail?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Does the API doc for tabbed pane have an add() method? What does it say?

  8. #8
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Sorry! if I confused a lot. Here I have attached a image file which will give you a clear idea of my requirement.

    FYI: Norm, the API doc for tabbed pane is having an add(component) method.

    Thank you.
    Attached Thumbnails Attached Thumbnails How to add a jbutton to tabbed pane headder?-mywish.jpg  

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    On the Sun forum, you said "Yes, <link snipped/> this page is the answer."
    Swing - How to draw a component on a Panel?

    Another way is to add the button to the frame's glass pane, with an appropriate layout, and set the glass pane's visibility.
    Java Code:
    public class TabbedPanePlusButton {
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
    
             @Override
             public void run() {
                new TabbedPanePlusButton().makeUI();
             }
          });
       }
    
       public void makeUI() {
          JTabbedPane tabbedPane = new JTabbedPane();
          for (int i = 0; i < 3; i++) {
             JPanel panel = new JPanel();
             panel.setName("tab" + (i + 1));
             panel.setPreferredSize(new Dimension(400, 400));
             tabbedPane.add(panel);
          }
    
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(tabbedPane);
          frame.pack();
    
          Rectangle tabBounds = tabbedPane.getBoundsAt(0);
    
          Container glassPane = (Container) frame.getRootPane().getGlassPane();
          glassPane.setVisible(true);
          glassPane.setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          gbc.fill = GridBagConstraints.NONE;
          gbc.insets = new Insets(tabBounds.y, 0, 0, 15);
          gbc.anchor = GridBagConstraints.NORTHEAST;
    
          JButton button = new JButton("My Button Position");
          button.setPreferredSize(new Dimension(button.getPreferredSize().width,
                (int) tabBounds.getHeight() - 2));
          glassPane.add(button, gbc);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    Note that GridBagLayout isn't the only layour manager you can use to achieve this.

    db

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Very nice.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Thanks Norm

    Quote Originally Posted by Norm View Post
    Very nice.
    Very nice for me that someone appreciated the effort :)

    Seems to have been wasted on the OP thoiugh :mad:

    db

  12. #12
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Thank you Darryl,

    This is what I expected, very cool.

    Thank you.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Your thanks are appreciated, but now it's time you start to solve your problems yourself.

    db

Similar Threads

  1. Tabbed pane using struts 2.x......?
    By prabhurangan in forum Web Frameworks
    Replies: 1
    Last Post: 07-19-2008, 06:48 AM
  2. Replies: 0
    Last Post: 07-07-2008, 04:44 PM
  3. Replies: 0
    Last Post: 07-07-2008, 04:43 PM
  4. Layered Pane Demo
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:44 PM
  5. AWT can we make a Tabbed container?
    By Panchitopro in forum AWT / Swing
    Replies: 0
    Last Post: 05-15-2008, 10:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •