Adding a JPopupMenu to a JTabbedPane

I have been trying for quite a while to accomplish this and I've been unsuccessful thus far.
I have a program that spawns a JEditorPane that displays html documents containing help information. Before, when another instance of the help command was issued another window would pop up, but I've recently converted this interface to a JTabbedPane instead. I'd like to add the functionality to close a pane individually through a context menu when the user rightclicks on a tab but no luck so far.
My problem doesn't necessarily deal with getting the correct tab to close at this point, it's getting the menu to display period.
I mimiced the procedure listed in the following sun tutorial to no avail:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html#popup
I'm using java 1.4.0_01 on 2k.
Thanks in advance,
Justin

Are you using the new SCROLL_TAB_LAYOUT option? If
so, you're up against a known bug that isn't going to
be fixed before the 1.5 release; go back to the
wrapped layout and see if it works. If that isn't the
problem, then you'd better post your code.That's what it was -- thanks for the help, hopefully we can get it resolved in the meantime.
Justin

Similar Messages

  • Add a jpopupmenu to a jtabbedpane

    hi guys,
    i want to add a popupmenu to each tab in a jtabbedpane. i dont know how to implement it so i ask here.
    it just shall have a close-menuitem.
    does someone know how to solve that? please help!

    Hello,
    I have a test I've made long ago. It works.
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JTabbedPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    public class TabbedPaneDemo extends JPanel {
        public TabbedPaneDemo() {
            JTabbedPane tabbedPane = new JTabbedPane();
              addListenerToPane(tabbedPane);
            Component panel1 = makeTextPanel("Blah");
            tabbedPane.addTab("One", null, panel1, "Does nothing");
            tabbedPane.setSelectedIndex(0);
            Component panel2 = makeTextPanel("Blah blah");
            tabbedPane.addTab("Two", null, panel2, "Does twice as much nothing");
            Component panel3 = makeTextPanel("Blah blah blah");
            tabbedPane.addTab("Three", null, panel3, "Still does nothing");
            Component panel4 = makeTextPanel("Blah blah blah blah");       
            tabbedPane.addTab("Four", null, panel4, "Does nothing at all");
            //Add the tabbed pane to this panel.
            setLayout(new GridLayout(1, 1));
            add(tabbedPane);
         private void addListenerToPane(final JTabbedPane tabbed)
              tabbed.addMouseListener(new MouseAdapter() {
                                  public void mousePressed(MouseEvent me)
                                       maybeShowPopup(me);
                                  public void mouseReleased(MouseEvent me)
                                       maybeShowPopup(me);
        protected Component makeTextPanel(String text) {
            JPanel panel = new JPanel(false);
            JLabel filler = new JLabel(text);
            filler.setHorizontalAlignment(JLabel.CENTER);
            panel.setLayout(new GridLayout(1, 1));
            panel.add(filler);
            return panel;
        public static void main(String[] args) {
            JFrame frame = new JFrame("TabbedPaneDemo");
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            frame.getContentPane().add(new TabbedPaneDemo(),
                                       BorderLayout.CENTER);
            frame.setSize(400, 125);
            frame.setVisible(true);
         private void maybeShowPopup(final MouseEvent me)
              if (me.isPopupTrigger()) {
                   JPopupMenu popup = new JPopupMenu();
                   JMenuItem item = new JMenuItem("Close");
                   popup.add(item);
                   item.addActionListener(new ActionListener() {
                             public void actionPerformed(ActionEvent e)
                                  JTabbedPane tabbed = (JTabbedPane)me.getSource();
                                  int i = tabbed.getSelectedIndex();
                                  tabbed.remove(i);
                   popup.show(me.getComponent(), me.getX(), me.getY());
    }

  • JComboBox added to JPopupMenu

    Hi,
    I have the following issue. I am trying to find a way to edit a cell in JTable by making selection from 4 comboboxes. The idea was to show JPopupMenu once the cell editor gains focus, and then allow to make selection from 4 comboboxes shown on the popup. The issue is once I start to edit a combobox, JPopupMenu disappears. Is there a was to show a popup, and still keep another popup showing?
    Thanks!!

    Hi,
    I have the following issue. I am trying to find a way
    to edit a cell in JTable by making selection from 4
    comboboxes. The idea was to show JPopupMenu once the
    cell editor gains focus, and then allow to make
    selection from 4 comboboxes shown on the popup. The
    issue is once I start to edit a combobox, JPopupMenu
    disappears. Is there a was to show a popup, and still
    keep another popup showing?
    I have the following issue. I am trying to find a way
    to edit a cell in JTable by making selection from 4
    comboboxes. The idea was to show JPopupMenu once the
    cell editor gains focus, and then allow to make
    selection from 4 comboboxes shown on the popup. The
    issue is once I start to edit a combobox, JPopupMenu
    disappears. Is there a was to show a popup, and still
    keep another popup showing?How will the user indicate that they are finished? Have you considered the more traditional approach of having the editor popup a dialog or undecorated window in which the data is entered? Something like:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editor
    Jim S.

  • JPopupMenu with JInternalFrame, popup menu doesn't work

    hi, i have this problem, as shown in the sample code at the end of this post.. basically, i have a table, and i added a JPopupMenu onto the table.. the popup menu works well when running the table class, though, when i call the table class in a JInternalFrame environment, the popup menu won't work.. anyone know why?
    ///Basic sample table code, when run this alone, the popup menu will work
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TableBasic extends JPanel
         private JTable table;
         public TableBasic()
              String[] columnNames = { "Date", "String", "Integer", "Boolean" };
              Object[][] data =
                   {  { new Date(), "A", new Integer(1), Boolean.TRUE },
                        { new Date(), "B", new Integer(2), Boolean.FALSE },
                        { new Date(), "C", new Integer(9), Boolean.TRUE },
                        { new Date(), "D", new Integer(4), Boolean.FALSE}
              table = new JTable(data, columnNames)
                   //Returning the Class of each column will allow different
                   //renderers to be used based on Class
                   public Class getColumnClass(int column)
                        return getValueAt(0, column).getClass();
              table.setPreferredScrollableViewportSize(table.getPreferredSize());
              JScrollPane scrollPane = new JScrollPane(table);
              add(scrollPane);
         public void createPopupMenu()
              JMenuItem menuItem;
              //Create the popup menu.
              JPopupMenu popup = new JPopupMenu();
              menuItem = new JMenuItem("A popup menu item");
              //menuItem.addActionListener(this);
              popup.add(menuItem);
              menuItem = new JMenuItem("Another popup menu item");
              //menuItem.addActionListener(this);
              popup.add(menuItem);
              //Add listener to the text area so the popup menu can come up.
              MouseListener popupListener = new PopupListener(popup);
              table.addMouseListener(popupListener);
         public static void main(String[] args)
              JFrame frame = new JFrame();
              TableBasic table = new TableBasic();
              table.createPopupMenu();
              frame.setContentPane(table);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              //frame.setLocationRelativeTo(null);
              frame.setVisible(true);
         class PopupListener extends MouseAdapter
              JPopupMenu popup;
              PopupListener(JPopupMenu popupMenu)
                   popup = popupMenu;
              public void mousePressed(MouseEvent e)
                   maybeShowPopup(e);
              public void mouseReleased(MouseEvent e)
                   maybeShowPopup(e);
              private void maybeShowPopup(MouseEvent e)
                   if (e.isPopupTrigger())
                        popup.show(e.getComponent(), e.getX(), e.getY());
    ///when integrate the previous table into here, popup menu won't work
    import java.awt.*;
    import javax.swing.*;
    public class InternalFrameBasic
         extends JFrame
         //implements ActionListener
         private JDesktopPane desktop;
         private JInternalFrame menuWindow;
         public static final int desktopWidth = 800;
         public static final int desktopHeight = 700;
         public InternalFrameBasic(String title)
              super(title);
              //Set up the GUI.
              desktop = new JDesktopPane();
              desktop.putClientProperty("JDesktopPane.dragMode", "outline");
              //Because we use pack, it's not enough to call setSize.
              //We must set the desktop's preferred size.
              desktop.setPreferredSize(new Dimension(desktopWidth, desktopHeight));
              setContentPane(desktop);
              createMenuWindow();
              desktop.add(menuWindow); //DON'T FORGET THIS!!!
              Dimension displaySize = menuWindow.getSize();
              menuWindow.setSize(desktopWidth, displaySize.height);
         private void createMenuWindow()
              menuWindow =
                             new JInternalFrame("Event Watcher", true, //resizable
                                                                                                   true, //closable
                                                                                                   false, //not maximizable
                                                                                                   true); //iconifiable
              menuWindow.setContentPane(new TableBasic());
              menuWindow.pack();
              menuWindow.setVisible(true);
          * Create the GUI and show it.  For thread safety,
          * this method should be invoked from the
          * event-dispatching thread.
         private static void createAndShowGUI()
              //Make sure we have nice window decorations.
              //JFrame.setDefaultLookAndFeelDecorated(true);
              //Create and set up the window.
              JFrame frame = new InternalFrameBasic("Example Internal Frame");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Display the window.
              frame.pack();
              frame.setVisible(true);
         public static void main(String[] args)
              //Schedule a job for the event-dispatching thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable()
                   public void run()
                        createAndShowGUI();
    }

    table.createPopupMenu();The above line should not be in the main method. It should be in the constructor class of TableBasic.
    You never execute that method in your InternalFrameBasic class to the mouse listener never gets added to the table.

  • How to adjust for different numbers of components on a JTabbedPane

    Hi,
    I have five panels on a JTabbedPane. On one, I have six components, with each being an array of five (in other words, I have six "columns" of five "rows" each). On another pane, I have only two components (not arrays). I understand that the JTabbedPane will be as large as the largest panel. In my case, the panel with two components is as large as the one with the arrays and the two components take up the entire panel. I have tried to adjust this by using the createGlue(), createHorizontalStrut(), and createVerticalStrut() methods, but it is somewhat "ugly." I am also using the Box layout. Is there a better way to do this? I have included some of my code for reference; (sorry for its length; I am including the code for only two of the five panels). Any assistance would be appreciated.
    TIA,
    Jeff
    Here is the code that creates the panel with the "columns" and "rows":
    public MailGen construct()
        //- create the controls for the emocPanel
        JLabel emocActionLabel = new JLabel( "ACTION" );
        emocActionLabel.setHorizontalAlignment( JLabel.CENTER );
        JLabel emocOrgIdLabel = new JLabel( "ORGANIZATION" );
        emocOrgIdLabel.setHorizontalAlignment( JLabel.CENTER );
        JLabel emocAircraftLabel = new JLabel( "AIRCRAFT" );
        emocAircraftLabel.setHorizontalAlignment( JLabel.CENTER );
        JLabel emocCatLabel = new JLabel( "CAT." );
        emocCatLabel.setHorizontalAlignment( JLabel.CENTER );
        JLabel emocSequenceLabel = new JLabel( "SEQ. # STARTS WITH" );
        emocSequenceLabel.setHorizontalAlignment( JLabel.CENTER );
        JLabel emocQuantityLabel = new JLabel( "QTY OF EMAILS" );
        emocQuantityLabel.setHorizontalAlignment( JLabel.CENTER );
        JComboBox emocActionComboBox;
        JTextField emocOrgTextField;
        JTextField emocAircraftTextField;
        JComboBox emocCategoriesComboBox;
        JTextField emocSequenceTextField;
        JTextField emocQuantityTextField;
        //- create the boxes that will contain the EMOC labels and controls
        Box northEmocBox = new Box( BoxLayout.X_AXIS );
        Box centerEmocBox = new Box( BoxLayout.X_AXIS );
        Box southEmocBox = new Box( BoxLayout.X_AXIS );
        //- add the EMOC labels to the northEmocBox
        //- HERE I AM USING STRUTS
        northEmocBox = Box.createHorizontalBox();
        northEmocBox.add( Box.createHorizontalStrut( 15 ) );
        northEmocBox.add( emocActionLabel );
        northEmocBox.add( Box.createHorizontalStrut( 40 ) );
        northEmocBox.add( Box.createVerticalStrut( 25 ) );
        northEmocBox.add( emocOrgIdLabel );
        northEmocBox.add( Box.createHorizontalStrut( 40 ) );
        northEmocBox.add( Box.createVerticalStrut( 25 ) );
        northEmocBox.add( emocAircraftLabel );
        northEmocBox.add( Box.createHorizontalStrut( 40 ) );
        northEmocBox.add( Box.createVerticalStrut( 25 ) );
        northEmocBox.add( emocCatLabel );
        northEmocBox.add( Box.createHorizontalStrut( 5 ) );
        northEmocBox.add( Box.createVerticalStrut( 25 ) );
        northEmocBox.add( emocSequenceLabel );
        northEmocBox.add( Box.createHorizontalStrut( 15 ) );
        northEmocBox.add( Box.createVerticalStrut( 25 ) );
        northEmocBox.add( emocQuantityLabel );
        northEmocBox.add( Box.createHorizontalStrut( 15 ) );
        JPanel mainEmocPanel = new JPanel();
        mainEmocPanel.setLayout( new BoxLayout( mainEmocPanel, BoxLayout.Y_AXIS ) );
        mainEmocPanel.add( northEmocBox );
        //- add the EMOC controls to the centerEmocBox
        Object[][] emocFieldTable = new Object[5][6];
        for ( int index = 0; index < 5; index++ )
          centerEmocBox = Box.createHorizontalBox();
          emocFieldTable[index][0] = new JComboBox( actions );
          ( ( JComboBox ) emocFieldTable[index][0] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          emocFieldTable[index][1] = new JTextField( 3 );
          ( ( JTextField ) emocFieldTable[index][1] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          emocFieldTable[index][2] = new JTextField( 3 );
          ( ( JTextField ) emocFieldTable[index][2] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          emocFieldTable[index][3] = new JComboBox( categories );
          ( ( JComboBox ) emocFieldTable[index][3] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          emocFieldTable[index][4] = new JTextField( 3 );
          ( ( JTextField ) emocFieldTable[index][4] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          emocFieldTable[index][5] = new JTextField( 3 );
          ( ( JTextField ) emocFieldTable[index][5] ).setBorder(
          BorderFactory.createLineBorder( Color.BLACK, 1 ) );
          centerEmocBox.add( ( JComboBox )emocFieldTable[index][0] );
          centerEmocBox.add( ( JTextField )emocFieldTable[index][1] );
          centerEmocBox.add( ( JTextField )emocFieldTable[index][2] );
          centerEmocBox.add( ( JComboBox )emocFieldTable[index][3] );
          centerEmocBox.add( ( JTextField )emocFieldTable[index][4] );
          centerEmocBox.add( ( JTextField )emocFieldTable[index][5] );
          mainEmocPanel.add( centerEmocBox );
        //- create the SEND and CANCEL buttons
        JButton emocSendButton = new JButton( "SEND EMAIL" );
        emocSendButton.setBackground( Color.white );
        emocSendButton.setBorder( raisedBevel );
        JButton emocCancelButton = new JButton( "CANCEL" );
        emocCancelButton.setBackground( Color.white );
        emocCancelButton.setBorder( raisedBevel );
        //- add the buttons to the southEmocBox
        southEmocBox = Box.createHorizontalBox();
        southEmocBox.add( emocSendButton );
        southEmocBox.add( emocCancelButton );
        mainEmocPanel.add( southEmocBox );Here is the code that creates the panel with only two components:
    //- create the controls for preguardPanel
        JLabel preguardActionLabel = new JLabel( "SELECT ACTION" );
        preguardActionLabel.setHorizontalAlignment( JLabel.CENTER );
        preguardActionLabel.setPreferredSize( new Dimension( 100, 10 ) );
        JComboBox preguardActionComboBox = new JComboBox( actions );
        preguardActionComboBox.setBorder( BorderFactory.createLineBorder(
            Color.BLACK, 1 ) );
        preguardActionComboBox.setPreferredSize( new Dimension( 30, 10 ) );
        //- create the SEND and CANCEL buttons
        JButton preguardSendButton = new JButton( "SEND ACTION" );
        preguardSendButton.setBackground( Color.white );
        preguardSendButton.setBorder( raisedBevel );
        JButton preguardCancelButton = new JButton( "CANCEL" );
        preguardCancelButton.setBackground( Color.white );
        preguardCancelButton.setBorder( raisedBevel );
        //- create the boxes that will contain the preguard label and control
        Box northPreguardBox = new Box( BoxLayout.X_AXIS );
        Box centerPreguardBox = new Box( BoxLayout.X_AXIS );
        Box southPreguardBox = new Box( BoxLayout.X_AXIS );
        //- add the preguard label to the northPreguardBox
        northPreguardBox = Box.createHorizontalBox();
        northPreguardBox.add( Box.createGlue() );
        //northPreguardBox.add( Box.createGlue() );
        northPreguardBox.add( preguardActionLabel );
        //northPreguardBox.add( Box.createGlue() );
        northPreguardBox.add( Box.createGlue() );
        JPanel mainPreguardPanel = new JPanel();
        mainPreguardPanel.setLayout( new BoxLayout(
            mainPreguardPanel, BoxLayout.Y_AXIS ) );
        mainPreguardPanel.add( northPreguardBox );
        //- add the preguard control to the centerPreguardBox
        //- HERE IS THE "GLUE" I AM USING
        centerPreguardBox = Box.createHorizontalBox();
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( preguardActionComboBox );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        centerPreguardBox.add( Box.createGlue() );
        mainPreguardPanel.add( centerPreguardBox );
        //- add the buttons to the southPreguardBox
        southPreguardBox = Box.createHorizontalBox();
        southPreguardBox.add( Box.createHorizontalStrut( 10 ) );
        southPreguardBox.add( preguardSendButton );
        southPreguardBox.add( preguardCancelButton );
        mainPreguardPanel.add( southPreguardBox );Here, I am adding the panels to the JTabbedPane:
    //- add the panels to the tabbed pane
       JTabbedPane pane = new JTabbedPane();
       pane.add( "EMOC", mainEmocPanel );
       pane.add( "PREGUARD", mainPreguardPanel );
    }

    This seems to be a commercial product. For the best chance at finding a solution I would suggest posting in the forum for HP Business Support!
    You can find the Commercial Designjet board here:
    http://h30499.www3.hp.com/t5/Printers-Designjet-Large-Format/bd-p/bsc-414
    Best of Luck!
    You can say thanks by clicking the Kudos Star in my post. If my post resolves your problem, please mark it as Accepted Solution so others can benefit too.

  • Bug: JTabbedPane setTabLayoutPolicy in 1.4

    I noticed weird behavior when using setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT). When adding a MouseListener to a JTabbedPane with this property set (it's a recent addition since 1.4), mouseClicked, mouseReleased, and mousePressed events do not occur. These events don't occur when clicked on the tab name (it works in other various areas though), but this is not standard behavior. When adding a MouseListener to a JTabbedPane, these events should occur no matter where you clicked on the JTabbedPane. Expected results are received with the default value, setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT). Here's someone else (the only other person I know) who experienced this problem:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=185937
    Try running his code (my code is too long to show, it's part of a big project) and let me know if this bug exists for whatever operating system you use. Could anyone let me know if there's a workaround for this problem? I filed this bug and I hope Sun will fix it soon :) Thanks.

    I dunno whether its a bug or not but in my old application Tab key stopped working when run on JRE1.4.1!
    And if I had Java Console open Tab worked fine (earlier we were using JRE1.3 plugin, now I tried the application on JRE1.4.1 ).
    Only after setting the myApplet.FocusCycleRoot(true) did it started working again.
    Cheers,
    Amit

  • When does a JTabbedPane set the size of the component inside a tab?

    I would like to know how big a component inside a tab is directly after I've added it to a tab in a JTabbedPane.
    I thought once I've "added" a component via the JTabbedPane.add(String, Component) method, the Component would be realized with correct sizes.
    Where or when should I request the information about how big a component has become inside a tab??
    Example:
    1. "Add" a tab via the menu.
    2. Notice that the size of the panel has not changed even though we see it on the screen.
    -Js
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    public class TabbedPaneShowingTest extends JFrame
         //GUI
         private JTabbedPane tabbedPane;
              private JPanel tabPanel;
         //MENU
         private JMenuBar mainMenuBar;
              private JMenu actionMenu;
                   private JMenuItem addTabMenuItem;
         public TabbedPaneShowingTest()
              this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
              this.setContentPane(getTabbedPane());
              this.setJMenuBar(getMainMenuBar());
              this.pack();
              this.setVisible(true);
         //GUI
         private JTabbedPane getTabbedPane()
              if(tabbedPane == null)
                   tabbedPane = new JTabbedPane();
                   tabbedPane.setPreferredSize(new Dimension(100,100));
              return tabbedPane;
         private JPanel getTabPanel()
              if(tabPanel == null)
                   tabPanel = new JPanel();
                   tabPanel.setBackground(Color.GREEN);
              return tabPanel;
         //MENU
         private JMenuBar getMainMenuBar()
              if(mainMenuBar == null)
                   mainMenuBar = new JMenuBar();
                   mainMenuBar.add(getActionMenu());
              return mainMenuBar;
         private JMenu getActionMenu()
              if(actionMenu == null)
                   actionMenu = new JMenu("Action");
                   actionMenu.add(getAddLineMenuItem());
              return actionMenu;
         private JMenuItem getAddLineMenuItem()
              if(addTabMenuItem == null)
                   addTabMenuItem = new JMenuItem("Add Tab");
                   addTabMenuItem.addActionListener(new ActionListener()
                        public void actionPerformed(ActionEvent e)
                             System.out.println("BEFORE TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());
                             getTabbedPane().add("Tab",getTabPanel());
                             System.out.println("AFTER TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());
              return addTabMenuItem;
         public static void main(String args[])
              new TabbedPaneShowingTest();
    }

    Once again... a little experimenting is a good thing. Just use SwingUtilities.InvokeLater() to retrieve the proper size.
    -Js
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    public class TabbedPaneShowingTest extends JFrame
         //GUI
         private JTabbedPane tabbedPane;
              private JPanel tabPanel;
         //MENU
         private JMenuBar mainMenuBar;
              private JMenu actionMenu;
                   private JMenuItem addTabMenuItem;
         public TabbedPaneShowingTest()
              this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
              this.setContentPane(getTabbedPane());
              this.setJMenuBar(getMainMenuBar());
              this.pack();
              this.setVisible(true);
         //GUI
         private JTabbedPane getTabbedPane()
              if(tabbedPane == null)
                   tabbedPane = new JTabbedPane();
                   tabbedPane.setPreferredSize(new Dimension(100,100));
              return tabbedPane;
         private JPanel getTabPanel()
              if(tabPanel == null)
                   tabPanel = new JPanel();
                   tabPanel.setBackground(Color.GREEN);
              return tabPanel;
         //MENU
         private JMenuBar getMainMenuBar()
              if(mainMenuBar == null)
                   mainMenuBar = new JMenuBar();
                   mainMenuBar.add(getActionMenu());
              return mainMenuBar;
         private JMenu getActionMenu()
              if(actionMenu == null)
                   actionMenu = new JMenu("Action");
                   actionMenu.add(getAddLineMenuItem());
              return actionMenu;
         private JMenuItem getAddLineMenuItem()
              if(addTabMenuItem == null)
                   addTabMenuItem = new JMenuItem("Add Tab");
                   addTabMenuItem.addActionListener(new ActionListener()
                        public void actionPerformed(ActionEvent e)
                             System.out.println("BEFORE TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());
                             getTabbedPane().addTab("Tab",getTabPanel());
                             SwingUtilities.invokeLater(new Runnable()
                                  public void run()
                                       System.out.println("AFTER TAB SIZE: " + getTabPanel().getWidth() + "," + getTabPanel().getHeight());
              return addTabMenuItem;
         public static void main(String args[])
              new TabbedPaneShowingTest();
    }

  • How can I add some space between each tab in JTabbedPane?

    I added some tabs in a JTabbedPane and want to add some spaces between each of them. How can I do this?

    There are different questions, aren't they? if you ask how to move a component to the right, and get an answer,
    then ask how to move it lower, it may seem to be a different question,
    but the concept is the same, and the answer would almost certainly
    be the same (with a tweak). Basically you should be smart enough to work
    out how to move it lower for yourself.
    if you don't understand the how concept of my response in the linked post
    relates to this post, then you're in the wrong game.
    to make it easier for you to understand
    set your own UI, overriding calculateTabHeight(..), setting whatever you want
    set your own UI, overriding paintTab(..), setting whatever you want for rects[..].x
    having pointed you to the area where you do this stuff, you should have been
    smart enough to work it out for yourself.
    at some point you will have to take your feet off the desk.

  • JTabbedPane question

    Hi all,
    I need a piece of advice about how using JTabbedPane. I create a JTabbedPane within there are 3 tabs grouped in a undertabpane component and added to YearPane ( the main JTabbedPane) by the addTab method.
    class YearPane extends JTabbedPane {
    public YearPane() {
    setBorder(BorderFactory.createLoweredBevelBorder());
    public void addYearTab() {
    JTabbedPane undertabpane = new JTabbedPane();
    undertabpane.setBorder(BorderFactory.createEtchedBorder());
    undertabpane.addTab("euro", null);
    undertabpane.addTab("dollar", null);
    undertabpane.addTab("chf", null);
    undertabpane.addTab("graphic", null);
    this.addTab(title, undertabpane);
    So, My question is how can I interact with the added tabs like "euro", "dollar" or "chf" ? How can I put or get informations to or from only one of this tab ? By example, is it possible to put a JTextField only inside the "euro" tab ?
    Thanks for your help.

    undertabpane.addTab("euro", null);
    undertabpane.addTab("dollar", null);
    undertabpane.addTab("chf", null);
    undertabpane.addTab("graphic", null);
    this.addTab(title, undertabpane);
    So, My question is how can I interact with the added
    tabs like "euro", "dollar" or "chf" ? How can I put or
    get informations to or from only one of this tab ? By
    example, is it possible to put a JTextField only
    inside the "euro" tab ?Instead of passing null for the component to the addTab() method calls create a component and pass it to addTab(). E.G. Create a JPanel that contains the JTextField objects that you want to display on the "euro" tab and pass ito the the "euro" addTab() call.

  • JTabbedPane and a Clsoe icon

    i know this has been asked before, but noone has ever answered completly
    i want to add a close button <an X basically> o nthe tab itself <a la Eclipse>, and while i can obviosly put an icon there, ther eis no way to add an action listener to the tab.
    i cna add an action listener to the tabpane, which will tell me that a tab was clicked on, and which one, but what i need really is a wya to see the x/y coordinates of where on the tab the click happened. This makes me think i need to overide the listener soewhere, but i am nto sure where.
    thanks

    You can get the location of the click by adding a MouseListener to the JTabbedPane. To map it to the tab, you'll have to use the TabbedPaneUI returned by getUI() - it has a method tabForCoordinate(JTabbedPane, int, int) which will tell you which tab was clicked, and a method getTabBounds(JTabbedPane, int) which will tell you the bounds of the tab, and allow you to work out where the click occurred within the tab. However, if you want to paint a cross on the tab as well, you'll have to write your own TabbedPaneUI with a custom paint method. You can install it with JTabbedPane.setUI.

  • Popup menu on tab components disables JTabbedPane mouse clicks

    When I add a popup menu to the tab components, the underlying JTabbedPane doesn't respond to any mouse click. How can we solve this?
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TabPopupDemo extends JFrame {
        private JLabel jLabel1;
        private JLabel jLabel2;
        private JMenuItem jMenuItem1;
        private JPopupMenu jPopupMenu1;
        private JTabbedPane jTabbedPane1;
        public TabPopupDemo() {
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            jPopupMenu1 = new JPopupMenu();
            jMenuItem1 = new JMenuItem("jMenuItem1");
            jTabbedPane1 = new JTabbedPane();
            jLabel1 = new JLabel("jLabel1");
            jLabel2 = new JLabel("jLabel2");
            jPopupMenu1.add(jMenuItem1);
            jTabbedPane1.addTab(null, jLabel1);
            jTabbedPane1.addTab(null, jLabel2);
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
            int tabCount = jTabbedPane1.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
                jTabbedPane1.setTabComponentAt(i, jLabel);
                jLabel.setName(String.valueOf(i));
                jLabel.setComponentPopupMenu(jPopupMenu1);
            jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {
                public void popupMenuCanceled(final PopupMenuEvent evt) {
                public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
                public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
                    JPopupMenu source = (JPopupMenu) evt.getSource();
                    JLabel invoker = (JLabel) source.getInvoker();
                    JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName()));
                    jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
        public static void main(final String args[]) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TabPopupDemo().setVisible(true);
    }

    I don't know what the best solution would be.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TabPopupDemo2 extends JFrame {
      private JLabel jLabel1;
      private JLabel jLabel2;
      private JMenuItem jMenuItem1;
      private JPopupMenu jPopupMenu1;
      private JTabbedPane jTabbedPane1;
      public TabPopupDemo2() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        jPopupMenu1 = new JPopupMenu();
        jMenuItem1 = new JMenuItem("jMenuItem1");
        jTabbedPane1 = new JTabbedPane();
        jLabel1 = new JLabel("jLabel1");
        jLabel2 = new JLabel("jLabel2");
        jPopupMenu1.add(jMenuItem1);
        jTabbedPane1.addTab(null, jLabel1);
        jTabbedPane1.addTab(null, jLabel2);
        getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        int tabCount = jTabbedPane1.getTabCount();
        TabMouseListener tml = new TabMouseListener(jTabbedPane1);
        for (int i = 0; i < tabCount; i++) {
          JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
          jTabbedPane1.setTabComponentAt(i, jLabel);
          jLabel.setName(String.valueOf(i));
          jLabel.addMouseListener(tml);
          jLabel.setComponentPopupMenu(jPopupMenu1);
        jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {
          public void popupMenuCanceled(PopupMenuEvent evt) {}
          public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
          public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            //JPopupMenu source = (JPopupMenu) e.getSource();
            //JLabel invoker = (JLabel) source.getInvoker();
            int index = jTabbedPane1.getSelectedIndex();
            JLabel invoker = (JLabel) jTabbedPane1.getTabComponentAt(index);
            JLabel component = (JLabel) jTabbedPane1.getComponentAt(index);
            jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
      static class TabMouseListener extends MouseAdapter{
        private final JTabbedPane tp;
        TabMouseListener(JTabbedPane tabbedPane) {
          this.tp = tabbedPane;
        private void dispatchEvent(MouseEvent me) {
          JLabel l = (JLabel)me.getSource();
          tp.dispatchEvent(SwingUtilities.convertMouseEvent(l,me,tp));
        public void mouseClicked(MouseEvent me)  { dispatchEvent(me); }
        public void mouseEntered(MouseEvent me)  { dispatchEvent(me); }
        public void mouseExited(MouseEvent me)   { dispatchEvent(me); }
        public void mousePressed(MouseEvent me)  { dispatchEvent(me); }
        public void mouseReleased(MouseEvent me) { dispatchEvent(me); }
      public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() { new TabPopupDemo2().setVisible(true); }
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TabPopupDemo3 extends JFrame {
      private JLabel jLabel1;
      private JLabel jLabel2;
      private JMenuItem jMenuItem1;
      private JPopupMenu jPopupMenu1;
      private JTabbedPane jTabbedPane1;
      public TabPopupDemo3() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        jPopupMenu1 = new JPopupMenu();
    //     jPopupMenu1 = new JPopupMenu() {
    //       public void show(Component c, int x, int y) {
    //         int i = jTabbedPane1.indexAtLocation(x, y);
    //         if(i>=0) {
    //           JLabel tab = (JLabel) jTabbedPane1.getTabComponentAt(i);
    //           JLabel component = (JLabel) jTabbedPane1.getComponentAt(i);
    //           jMenuItem1.setText(tab.getText() + ":  " + component.getText());
    //           super.show(c, x, y);
        jMenuItem1 = new JMenuItem("jMenuItem1");
        jTabbedPane1 = new JTabbedPane();
        jTabbedPane1.setComponentPopupMenu(jPopupMenu1);
        jLabel1 = new JLabel("jLabel1");
        jLabel2 = new JLabel("jLabel2");
        jPopupMenu1.add(jMenuItem1);
        jTabbedPane1.addTab(null, jLabel1);
        jTabbedPane1.addTab(null, jLabel2);
        getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        int tabCount = jTabbedPane1.getTabCount();
        for (int i = 0; i < tabCount; i++) {
          JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
          jTabbedPane1.setTabComponentAt(i, jLabel);
          jLabel.setName(String.valueOf(i));
          //jLabel.setComponentPopupMenu(jPopupMenu1);
        jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {
          public void popupMenuCanceled(final PopupMenuEvent evt) {}
          public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {}
          public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
            JPopupMenu source = (JPopupMenu) evt.getSource();
            int i = jTabbedPane1.getSelectedIndex();
            JLabel tab = (JLabel) jTabbedPane1.getTabComponentAt(i);
            JLabel component = (JLabel) jTabbedPane1.getComponentAt(i);
            if(tab.getMousePosition()!=null) {
              jMenuItem1.setText(tab.getText() + ":  " + component.getText());
            }else{
              jMenuItem1.setText("aaaaaaaaa");
      public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() { new TabPopupDemo3().setVisible(true); }
    }

  • No Event from JPopupMenu

    Hallo,
    In my application I added an JPopupMenu that contains MenuItems for a ContextMenu (push the right mouse button to display it).
    It works fine if the JPopupMenu display inside the frame.
    If the JPopupMenu displays with only one pixel or more out of the frame none ActionEvent will be fired.
    Can someone tell me why and knows the solution?
    Kind regards
    Markus

    Hi,
    Nop.

  • Help with JTabbedPane.Please advise

    I am unable to display the contents on a JList component
    which is in a JPanel.
    But if I extend JFrame,the contents are displayed.
    I guess this is due to the show() method,
    Is there a method similar to show in JPanel??
    Can some one advise??

    Hi,
    Where are you actually adding the JPanel, are u adding the JPanel on the JTabbedPane?
    As per your doubt, JPanel consists of JList, actually you will have to add this JPanel on to the JFrame, then when you say JFrame.show() / JFrame.setVisible(true), i think that should work and you will be able to see the JList box.
    Regards,
    Balaji

  • JPopupmenu can't overlap Canvas

    Hello,
    I have an application which displays a Canvas object I've created. The problem occurs when I activate a popupmenu; a part of it isn't displayed because it overlaps my Canvas.
    How could I fix this ?
    I have no problem when my popupmenu overlaps a jtable.
    Thanks.

    I found a solution, I don't know if it's the best one.
    To put it clear, I have a variable : graph which is an instance of my Graph class (extends JComponent).
    First I tried to add directly my graph object into a JTabbedPane, and I tried to: graph.setBackground(Color.white)
    nothing changed.
    I even tried to do : graph.setOpaque(true) before, but it didn't work (I read this in a former post).
    So I've added my graph object into a JPanel, and then added the JPanel into my JTabbedPane.
    I did : myPanel.setBackground(Color.white)
    it works.
    If anyone has a better solution...
    Thanks for the answers.

  • JPopupMenu no actionEvent

    Hallo,
    In my application I added a JPopupMenu for a ContextMenu (klick on right Mousebutton to display it).
    It works fine if the JPopMenu display inside the Frame.
    But if the JPopMenu display with only one Pixel out of the Frame there will be none actionEvent fired.
    knows someone a solution

    In order to identify currently selected JTable,do one thing;
    table.addMouseListener();With the help of mouseReleased you may be able to determine the last selected JTable's object.
    Now u have got the source for mouseReleased Event.
    get the selected row by
    table.getSelectedRow();Since u have got the Table and its selected rows, u can do the actions for the table with ur PopupMenu.

Maybe you are looking for

  • Need help on monitoring poor performance in BIWS

    Hello All,    We have a strange behavior with all our Dashboard on SAP 4.0 SP5 FP3 with BIWS connexions.  1 time on 10 (approx.) the BI Web Service connexion take so long.  I mean in normal circonstance, it take 2 seconds, then it take 30 seconds, 1

  • 17" ACD gone bright pink

    This issue could be with the Apple ADC to DVI convertor box or cable, but I can't confirm if it is that or my monitor. I am using a 17" clear ACD with a mac mini, and I started to get intermittant bright pink screens, which has progressed into pink a

  • Serial Number Already In Use

    I've upgraded from CS4 to CS5.5 to CS6 and it's been a NIGHTMARE in terms of licensing.  I only use the program on my work PC and my traveling laptop.  I've been told from an Adobe customer service rep this is a perfectly acceptable configuration.  A

  • CS6 free trial download does not complete, help?

    Have Mac OS X 10.6.8 version. Adobe download assistant does not finish downloading! help

  • Join 2 lines with Arc

    Anyone could give me a example to join 3 lines with Arc? I´ve already tried to join using QuadCurveTo, but my lines were wrong. Thanks, André Rezende