Results 1 to 10 of 10
  1. #1
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default Using JtabbedPane

    Hi All ,

    I have a tabbed pane created with three panels added to it . Now I want to add a menubar to the tabbed pane and make it work just like eclipse . There is no setMenu..option in JtabbedPane like frame so can somebody please help me out with this .

    Thanks in Advance ....

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

    Default

    Why not add the menu bar to the JFrame (or other root pane)? If not, then you can add it BorderLayout.NORTH to whatever Container is holding your JTabbedPanes.

  3. #3
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Actually I nedd to customize mytabbed pane in the same way as in eclipse .
    I need to select a tab and display the corresponding menuBar . If I add the menu bar to the frame it won't work out with the same look and feel.

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

    Default

    I have Eclipse, and I'm still not sure what you mean here. In my version of Eclipse, the menubar is part of the main displayed window (their equivalent of a JFrame), not part of the tabbed pane.

    edit: I think I see what you mean. You want a menu bar below the tabs. Then you need to add a JMenuBar to the JPanels that the JTabbedPane tabs hold.
    Last edited by Fubarable; 03-24-2010 at 11:26 AM.

  5. #5
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Hi ..

    In eclipse we have the lower tabbed pane where we have the console pane in that on selecting console tab you get to see a toolbar on the right . And in the simlar manner on selecting different tabe u get different toolbar Iwant to achieve the same using menubar.

  6. #6
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    on selecting console tab you get to see a toolbar on the right . And in the simlar manner on selecting different tabe u get different toolbar
    see API for methods of JTabbedPane.
    getSelectedIndex()

    setSelectedIndex()

    removeTabAt(int index)

    addTab

    to accomplish ur task from menubar.

  7. #7
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    I am not able to find a way to add menu bar at the extreme right on the tabbed pane directly . If I use add method it adds the menu as a tab which I don't want .

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

    Default

    You want to add menu icons to the JTabbedPane's tab bar, the narrow rectangle that holds the tabs themselves. I don't know how to do this in Swing or if it's possible. Let's see if any of the Swing pros come across this thread and can give you some advice.

  9. #9
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    to add menu bar at the extreme right on the tabbed pane directly
    u can add menubar to a panel, using appropriate layout.
    then add that panel to tabbedpane..

    very closer to the above suggested,

    Why not add the menu bar to the JFrame (or other root pane)? If not, then you can add it BorderLayout.NORTH to whatever Container is holding your JTabbedPan
    es.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Can't be done directly, but this posting of mine shows a way to fake it.
    How to add a jbutton to tabbed pane headder?

    I've modified the example for a JMenu in place of a JButton.
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class TabbedPanePlusButton {
    
      static final int NUM_TABS = 5;
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new TabbedPanePlusButton().makeUI();
          }
        });
      }
    
      public void makeUI() {
        final JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setPreferredSize(new Dimension(400, 400));
        for (int i = 0; i < NUM_TABS; i++) {
          JPanel panel = new JPanel();
          panel.setName("tab" + (i + 1));
          panel.add(new JLabel("This is panel " + (i + 1)));
          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("Click");
        //button.setPreferredSize(new Dimension(button.getPreferredSize().width,
        //      (int) tabBounds.getHeight() - 2));
        JMenu menu = new JMenu("Menu");
        menu.setPreferredSize(new Dimension(menu.getPreferredSize().width,
                (int) tabBounds.getHeight() - 2));
        for (int i = 0; i < NUM_TABS; i++) {
          JMenuItem item = new JMenuItem("Tab " + (i + 1));
          final int j = i;
          item.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
              tabbedPane.setSelectedIndex(j);
            }
          });
          menu.add(item);
        }
        JMenuBar bar = new JMenuBar();
        bar.add(menu);
    
        //glassPane.add(button, gbc);
        glassPane.add(bar, gbc);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    db

    edit Note that it is your responsibility to see that the tabs and the menu don't overlap. The menu being added to the glass pane means that it will always be displayed over the tabbed pane.
    Last edited by DarrylBurke; 03-24-2010 at 12:36 PM.

Similar Threads

  1. [SOLVED] How To Freeze Tab/s from JTabbedPane
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-10-2009, 09:42 AM
  2. Traversing a JTabbedPane
    By Inks in forum AWT / Swing
    Replies: 12
    Last Post: 03-11-2009, 05:15 AM
  3. How to use JTabbedPane
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  4. Help with JTabbedPane
    By lenny in forum SWT / JFace
    Replies: 1
    Last Post: 08-07-2007, 06:18 AM
  5. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM

Posting Permissions

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