JMenu and JTabbedPane

Hi,
I'm trying to create different JMenu depending on which tabbedpane you choose.
Any ideas how to do this???

add a change listener to the tabbed pane and when it fires, get the selected tab index and update your menus accordingly.

Similar Messages

  • JMenu and JTabbedPane questions

    Hello I got 2 questions :
    1) Is it possible to change the icon of JMenu when the mouse cursor is on it ? setRolloverIcon function doesnt work in this case, like it does for the JButton.
    2) When I create my application I got 1 JTabbedPane object visible. Now, every time I click a selected JMenuItem, I wanna add another JTabbedPane object to the window. So in the class MyMenu I got this code in actionListener:
    if(arg.equals("menu1")) {
         JTabbedPane tabbedPane = new JTabbedPane();
         tabbedPane.addTab("tab", null, null, "nothing");
         frame.getContentPane().add(tabbedPane);
    The problem is that it doesnt appear on the window, but instead when I resize it, there is a vertical line in the place where the right "edge" of the old window was.
    Can anyone tell me what do I do wrong ?
    Thx in advance.

    Actually I dont know but the simpliest way seems to be subclassing the menu Item, and listen for mouse events. When you learn a better way to do it you can change your code :)
    Java always allowed me to do such tricks :)

  • JMenu and JMenuItem - Images

    How to put an image in front of the JMenu text?
    JMenuItem has a constructor to insert the icon.
    Is their a reason why it not in the JMenu constructors?
    Sometimes I just want to put a 16 pixel gap in front of the JMenuItem or JMenu if it has no Image. Can one do that without inserting a blank
    Image?
    Thanks

    Have you tried just setting it via setIcon()? Since both JMenu and
    JMenuItem inherit from AbstractButton, this may work; I say may
    since more than likely it is up to the L&F UI to do the right thing.
    A quick browse shows that it ought to work for at least the basic
    L&Fs though.
    As far as the empty gap, looks like you might be out of luck by
    default, but this too is up to the L&F. For instance, some look and
    feels line up the text properly if an item doesn't have an icon.
    As a quick fix, though, you could probably just set the icon to
    something like
    menuItem.setIcon( new Icon() {
        public int getIconWidth() { return 16; }
        public int getIconHeight() { return 0; }
        public void paintIcon(Component c, Graphics g, int x, int y) {}
    });: jay

  • JMenu and Documentation

    Hello friends, couple questions here. Main one i am confused with is the Documentations. I cant quite seem to grasp how to use what they provide. If anyone would be so kind as to take the time and explain to me how to use what the documentation offers i would be grateful.
    http://wwwswt.fzi.de/~jjh/tools/swing/products/jfc/swingdoc-current/api/com.sun.java.swing.JMenuBar.html
    here is a link to a documentation of JMenu and how to make one but i do not get how to apply this into a program. Thank you very much - Jeff

    Specificially, you want this page
    http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
    This is part of a much bigger tutorial, here
    http://java.sun.com/docs/books/tutorial/
    There are also more tutorials if you click on Tutorials in the upper left of this page.

  • Problem with Alt , JMenu and Mnemonics

    I have built an application with a JMenuBar containing several JMenu's, the first of which is a File menu. All JMenus and JMenuItems have mnemonics associated with them. When I press the alt key the underlines show and focus appears to be given to the File menu (though it has not dropped down). If I then press a key, for example 'S', that is a mnemonic of the 'Save' jmenuitem in the File menu, the Save action is invoked. This should not occur because the menu has not opened yet.
    The behavior is what I'd expect had an accelerator (Alt+S) been defined for the Save menu item. But I have not defined any accelerators.
    Why does this happen and more importantly, is there a work around?

    Except for the 1st line in jbinit() it's all pretty standard stuff. Here's a snippet of the code:
    public class MainFrame extends JFrame implements ChangeListener {
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenuFile = new JMenu();
    JMenuItem jMenuFileNew = new JMenuItem();
    JMenuItem jMenuFileSave = new JMenuItem();
    JMenu m_editMenu = new JMenu();
    JMenuItem m_editCutMenuItem = new JMenuItem();
    JMenuItem m_editCopyMenuItem = new JMenuItem();
    JMenuItem m_editPasteMenuItem = new JMenuItem();
    public MainFrame() {
    jbInit();
    //Component initialization
    private void jbInit() {
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke(KeyEvent.VK_ALT, Event.ALT_MASK, false), "repaint");
    // file menu
    jMenuFile.setText("File");
    jMenuFile.setMnemonic(KeyEvent.VK_F);
    jMenuFileNew.setText("New...");
    jMenuFileNew.setMnemonic(KeyEvent.VK_N);
    jMenuFileNew.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileNewSpecial_actionPerformed(e);
    jMenuFileSave.setText("Save");
    jMenuFileSave.setMnemonic(KeyEvent.VK_S);
    jMenuFileSave.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    jMenuFileSaveSpecial_actionPerformed(e);
    jMenuFile.add(jMenuFileNew);
    jMenuFile.add(jMenuFileSave);
    // edit menu
    m_editMenu.setText("Edit");
    m_editMenu.setMnemonic(KeyEvent.VK_E);
    m_editCutMenuItem.setText("Cut");
    m_editCutMenuItem.setMnemonic(KeyEvent.VK_T);
    m_editCutMenuItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    editCutMenuItem_actionPerformed(e);
    m_editCopyMenuItem.setText("Copy");
    m_editCopyMenuItem.setMnemonic(KeyEvent.VK_C);
    m_editPasteMenuItem.setText("Paste");
    m_editPasteMenuItem.setMnemonic(KeyEvent.VK_P);
    m_editMenu.add(m_editCutMenuItem);
    m_editMenu.add(m_editCopyMenuItem);
    m_editMenu.add(m_editPasteMenuItem);
    jMenuBar1.add(jMenuFile);
    jMenuBar1.add(m_editMenu);
    this.setJMenuBar(jMenuBar1);
    etc...
    Pressing Alt+S invokes the action listener for the jMenuFileSave menu item. It should do nothing since there is no top level menu with a mnemonic of 'S'.

  • Resize Jpanel and JtabbedPane

    Hi All,
    i implement a resize process on jpanel that include JtabbedPane inside.
    it working fine except one thing.
    when i resize my panel it work fine, until some point it start to shrink and get smaller.
    i think it getting shrink because im getting to the end of my dialog.
    i want to avoid this beheviour and let the tabbedpanel extend as much as possible:
    this is my code:
    package test;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GraphicsConfiguration;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.HeadlessException;
    import java.awt.Point;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingUtilities;
    public class BaseMain extends JFrame {
         Point sp;
         int     jPanelStartHeight;
         int     jPanelStartWidth;
         int     jTabPaneStartHeight;
         int     jTabPaneStartWidth;
         Point offset;
         private static final long serialVersionUID = 1L;
         private JPanel jContentPane = null;
         private JPanel jPanel = null;
         private JTabbedPane jTabbedPane = null;
         private JPanel jPanel1 = null;
         private JTabbedPane jTabbedPane1 = null;
         private JPanel jPanel2 = null;
         private JTabbedPane jTabbedPane2 = null;
         public BaseMain() throws HeadlessException {
              // TODO Auto-generated constructor stub
              super();
              initialize();
         public BaseMain(GraphicsConfiguration arg0) {
              super(arg0);
              // TODO Auto-generated constructor stub
              initialize();
         public BaseMain(String arg0) throws HeadlessException {
              super(arg0);
              // TODO Auto-generated constructor stub
              initialize();
         public BaseMain(String arg0, GraphicsConfiguration arg1) {
              super(arg0, arg1);
              // TODO Auto-generated constructor stub
              initialize();
          * This method initializes jPanel     
          * @return javax.swing.JPanel     
         private JPanel getJPanel() {
              if (jPanel == null) {
                   GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
                   gridBagConstraints1.fill = GridBagConstraints.BOTH;
                   gridBagConstraints1.weighty = 1.0;
                   gridBagConstraints1.weightx = 1.0;
                   gridBagConstraints1.gridheight=3;
                   jPanel = new JPanel();
                   jPanel.setLayout(new BorderLayout());
                   jPanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
                   jPanel.add(getJTabbedPane(),BorderLayout.SOUTH/* gridBagConstraints1*/);
                   jPanel.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mousePressed(java.awt.event.MouseEvent e) {
                             System.out.println("mousePressed()"); // TODO Auto-generated Event stub mousePressed()
                             //original size
                             jPanelStartHeight = jPanel.getHeight();
                             jPanelStartWidth  = jPanel.getWidth();
                             jTabPaneStartHeight = jTabbedPane.getHeight();
                             jTabPaneStartWidth  = jTabbedPane.getWidth();
                              offset = SwingUtilities.convertPoint(e.getComponent(),e.getPoint(),jPanel);
                   jPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                        public void mouseDragged(java.awt.event.MouseEvent e) {
                             System.out.println("mouseDragged()"); // TODO Auto-generated Event stub mouseDragged()
                             Point p = SwingUtilities.convertPoint(e.getComponent(),e.getPoint(), jContentPane);
                             jPanel.setSize(jPanelStartWidth,p.y+jPanelStartHeight);
                             jTabbedPane.setSize(jTabPaneStartWidth,p.y+jTabPaneStartHeight);
                             jPanel.setLocation(0/*p.x-offset.x*/,p.y-offset.y);
              return jPanel;
          * This method initializes jTabbedPane     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane() {
              if (jTabbedPane == null) {
                   jTabbedPane = new JTabbedPane();
                   jTabbedPane.addTab(null, null, getJPanel1(), null);
                   jTabbedPane.addTab(null, null, getJPanel2(), null);
              return jTabbedPane;
          * This method initializes jPanel1     
          * @return javax.swing.JPanel     
         private JPanel getJPanel1() {
              if (jPanel1 == null) {
                   GridBagConstraints gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.fill = GridBagConstraints.BOTH;
                   gridBagConstraints.weighty = 1.0;
                   gridBagConstraints.weightx = 1.0;
                   jPanel1 = new JPanel();
                   jPanel1.setLayout(new GridBagLayout());
                   jPanel1.add(getJTabbedPane1(), gridBagConstraints);
              return jPanel1;
          * This method initializes jTabbedPane1     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane1() {
              if (jTabbedPane1 == null) {
                   jTabbedPane1 = new JTabbedPane();
              return jTabbedPane1;
          * This method initializes jPanel2     
          * @return javax.swing.JPanel     
         private JPanel getJPanel2() {
              if (jPanel2 == null) {
                   GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
                   gridBagConstraints2.fill = GridBagConstraints.BOTH;
                   gridBagConstraints2.weighty = 1.0;
                   gridBagConstraints2.weightx = 1.0;
                   jPanel2 = new JPanel();
                   jPanel2.setLayout(new GridBagLayout());
                   jPanel2.add(getJTabbedPane2(), gridBagConstraints2);
              return jPanel2;
          * This method initializes jTabbedPane2     
          * @return javax.swing.JTabbedPane     
         private JTabbedPane getJTabbedPane2() {
              if (jTabbedPane2 == null) {
                   jTabbedPane2 = new JTabbedPane();
              return jTabbedPane2;
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        BaseMain thisClass = new BaseMain();
                        thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        thisClass.setVisible(true);
          * This method initializes this
          * @return void
         private void initialize() {
              this.setSize(300, 200);
              this.setContentPane(getJContentPane());
              this.setTitle("JFrame");
          * This method initializes jContentPane
          * @return javax.swing.JPanel
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(new BorderLayout());
                   jContentPane.add(getJPanel(), BorderLayout.SOUTH);
              return jContentPane;
    }

    hey gabi,
    i tried your code and it works just fine.... there is no shrinking or any resizing problem?! i don't know... maybe i just don't get it? so maybe u could describe your problem a little more?!
    :)

  • Layout managers and JTabbedPane

    I have a JTabbed pane on my 'form' on which i want to insert two tables. the problem is that the tables are overstepping the 'boundaries' i have desgned for them ,whivh i suspect is a problem with the layout managers. I just cant seem to be able to restrict the tables within the fames and have scrollpanes, both horizontal and vertical for scrolling tables. I have included the code here:
    import java.awt.*;
    import javax.swing.*;
    public class FrmProducts extends JFrame{
         private          JTabbedPane tabbedPane;
         private          JPanel          factorsTab;
         private          JPanel          productListTab;
         private          JPanel          rateHistoryTab;
         private          JTable          forexFactorsTable;
         private          JTable      otherFactorsTable;
         private      JTable          productListTable;
         public FrmProducts(){
              initializeComponents();
         public  void DisplayForm(){
              java.awt.EventQueue.invokeLater(new Runnable(){
                   public void run(){
         private void initializeComponents(){
              setTitle( "Products administration" );
              setSize( 900, 550 );
              setBackground( Color.gray );
              JPanel topPanel = new JPanel();
              topPanel.setLayout( new BorderLayout() );
              getContentPane().add( topPanel );
              Toolkit kit = getToolkit();
              Dimension screenSize = kit.getScreenSize();
              int screenWidth = screenSize.width;                         //all this is to get
              int screenHeight = screenSize.height;                    //the form size and
              Dimension windowSize = getSize();                       //centre the form on
              int windowWidth = windowSize.width;                         //the screen
              int windowHeight = windowSize.height;
              int upperLeftX = (screenWidth - windowWidth)/2;
              int upperLeftY = (screenHeight - windowHeight)/2;
              setLocation(upperLeftX, upperLeftY);
              setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              // Create the tab pages
              createPage1();
              createPage2();
              createPage3();
              // Create a tabbed pane
              tabbedPane = new JTabbedPane();
              tabbedPane.addTab( "Facors", factorsTab );
              tabbedPane.addTab( "Products List", productListTab );
              tabbedPane.addTab( "Rate History", rateHistoryTab );
              topPanel.add( tabbedPane, BorderLayout.CENTER );
              setVisible(true);
         public void createPage1()
              factorsTab = new JPanel();
              factorsTab.setLayout( new GridLayout(2,1) );
              JPanel forexFactorsPanel;          //set up a frame with the forex factors details
              forexFactorsPanel = new JPanel();
              forexFactorsPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Forex factors"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
              String forexFactorsColumns[]={"Factor","ZAR","US$"};
              String dummyValues1[][]={ {"Costing exchange rate","485.00",   "3400.00"},
                                          {"Local product exchange rate","430.00","3000.00"},
                                          {"Duty exchange rate1","35.30",          "250.00"},
                                          {"Duty exchange rate2","35.30",          "250.00"}};
              forexFactorsTable=new JTable(dummyValues1,forexFactorsColumns);
              JScrollPane scrollPane1=new JScrollPane(forexFactorsTable);
              forexFactorsPanel.add( scrollPane1, BorderLayout.CENTER );
              factorsTab.add(forexFactorsPanel);
              JPanel otherFactorsPanel;
              otherFactorsPanel=new JPanel();
              otherFactorsPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Other factors"),
                     BorderFactory.createEmptyBorder(5,5,5,5)));
              String otherFactorsColumns[]={"Description","Value"};
              String dummyValues2[][]={{"Landing Factor",                         "1.16"},
                                            {"Duty factor",                        "1.065"},
                                            {"Loading for overseas sourcing",  "1.15"},
                                            {"Extra mark up local",             "0.0"},
                                            {"Extra mark up imports",          "0.0"}};
              otherFactorsTable=new JTable(dummyValues2,otherFactorsColumns);
              JScrollPane scrollPane2=new JScrollPane(otherFactorsTable);
              otherFactorsPanel.add(scrollPane2, BorderLayout.CENTER);
              factorsTab.add(otherFactorsPanel);
         public void createPage2()
              productListTab = new JPanel();
              productListTab.setLayout( new BorderLayout() );
              String productListColumns[]={"Code1","Code2","Code3","Sales Category","Product code","Short Description",
                                             "Long Description","Supplier/Manufacturer","Supplier Product code",
                                             "Units","Master stockist","Lead time","re-Order level","economic order qty",
                                             "APR","min shipping qty"};
              String sampleValues[][]={
                     {"AI","AC","CE","Switchgear inc Starters","AIACCE 270","Timer int pulse start 230v 2C/O 30mins",
                       "Timer interval pulse start 230v 2 closed open 30 minutes","AC/DC South Africa","IAP2 30M","each",
                       "Central Stores","2","500","5000","APR","5000"},
                       {"GI","IN","ZZ","Alternative power and accessories","GIINZZ 174","INV HT SERIES 2500W 12v/230v MOD SWV",
                            "INVERTER HT SERIES 2500W 12v/230v MODIFIED SINEWAVE","SINETECH","HT-P-2500-12","each",
                            "Central Stores","6","1000","5000","APR","10000"}
              productListTable=new JTable(sampleValues,productListColumns);
              JScrollPane scrollPane3=new JScrollPane(productListTable);
              productListTab.add(scrollPane3, BorderLayout.CENTER);
         public void createPage3()
              rateHistoryTab = new JPanel();
              rateHistoryTab.setLayout( new GridLayout( 3, 2 ) );
    }This class is called by invoking the DispalyForm() function from a main form. May you please run it and see how the 'factors' panel needs correcting and help me do that

    wondering if there's a method that can be used to show a window(i.e. dialog) within a frame (much like an MDI form). That is, all windows are shown w/in the frame's border or title bar.
    Here's what I have attempted but to no avail:
    java.awt.Dimension screen = getDefaultToolkit.getScreenSize();
    java.awt.Insets frameInsets = this.getInsets();  // frame's insets
    // set bounds of child (window)
    window1.setbounds(frameInsets.top, frameInsets.top,
       screen.width - frameInsets.top -2, screen.height - frameInsets.top -2);any help is appreciated

  • JTable and JTabbedPane questions

    I have an application which has some JTabbedPanes setup, one of which has a table on it.
    What i'm after is a way of having a user select a row from the table and in doing so this will send them to another tabbedpane.
    So what i need is a way of assigning each row an ID number, then being able to extract this ID, and being able to switch to a new tab without having to click on the tab itself.
    I hope someone can point me in the right direction.
    One more question as well.
    I'm designing a networked bulletinboard application (using javaspaces) and i'm having an implementation issue.
    At the moment I can list all the topics in a table which just contains |the author, topic title and date created|.
    I'm having trouble thinking of a good way of displaying the actuall topic itself (and multiple follow-up posts).
    I was thinking of doing this in another table, but i couldnt find a way of customizing the layout enough to display formatted text. So i thought I would just do a loop of JTextAreas for each topic.
    Something along the lines of:
    while (i < number_of_posts)
    Create_JTextArea(post[i]);
    Do you think this would be a suitable way of implementing this? Or is there a better way that i'm missing?

    So what i need is a way of assigning each row an ID numberStore the ID number in the TableModel. You then just remove the TableColumn from the TableColumnModel and the ID will not be displayed in the table. You then simply use model.getValueAt(...) to reference the ID.

  • JScrollPane for pane and JTabbedPane and Graphics

    Sort of have two topics with same problem.
    I have a set of Panels. I get a stream of jpegs which I have to decode to the fly.
    There are seperate video feeds in the stream and I have to read jpeg data to see which image goes where.
    I was trying to see if I could have a larger image in smaller area. this is why I tried to draw to panel inside a scrollpane.
    One: I tried to put a JPanel into JScrollPane and tried to draw an image to panel with Graphics g. No Scrollability and even when scroll bars are made to stay on permanently, they get overwritten.
    g=panel.getGraphics();
    if(isVisible()) g.drawImage(image,0,0,320,240,Color.BLUE,null);
    two: When putting the pane with the drawn images into toplevel pane into JtabbedPane. I can turn off the drawing of images with a check to isVisible(). When they are not in top level pane isVisible() does not work and the images get drawn over the other tabbed panes.
    BTW everything is swing. JPanel,JScrollPane,JTabbedPane.

    No Scrollability and even when scroll bars are made to stay on permanently,First of all the scrollbars will only appear when the preferred size of the panel is greater than the size of the scrollpane.
    A think a better design for updating the image is to extend JPanel and create a setImage method. The set image method would set the preferred size of the panel and then invoke repaint() on the panel. You would then also need to override the paintComponent(..) method to do the actual drawing of the image. Then when the image changes you just use the new setImage(..) method.

  • About JPanel and JTabbedPane

    I have a JPanel and i want to know how any tab buttons it contain.
    We have a method in JTabbedPane to find it out ,so how can i type cast the JPanel to JTabbedPane.
    Thanks

    JTabbedPane does not extend JPanel, so you can't cast JPanel to JTabbedPane.
    A JPanel does not contain any tabs.
    I think you might need to rephrase the question.

  • JMenu and Paint Problem

    Hello,
    I have been teaching my self Java and I have become quite proficient with it, however, I have a problem that I can not fix, or get around.
    I have a JMenuBar in a JFrame with some JMenu�s containing JMenuItems. I also have a JPanel that I draw inside (I Know that is probably not the wisest way to draw things, but it was the way I first learned to draw and now my program is too big and it is not worth while to change). Any ways, I draw some graphics inside of this JPanel.
    The menu items change the drawings; this is done by repainting the JPanel Graphic. However, when I click one of the menu items, the JPanel paints but there is a residual of the JMenu in the background that will not disappear.
    The menu is closed and if I open and then close the menu the residual goes away.
    => I would like to know how to prevent the residual from appearing?
    The problem also occurs when I use the popup menus outside of the JmenuBar.
    What I think is wrong.
    I think my problem is with the repaint. I don�t think the JFrame is repainting. I think I am simply repainting all the components in the JFrame but not the JFrame itself and thus the residual is not cleared from the JFrame. I don�t know how to fix this but I believe that is the problem because when I minimize the JFrame then Maximize it the JFrame will not paint, and I can see right through the JFrame, but the components paint fine.
    Sorry for the long question, but I don�t know what would be helpful.
    Any advice would be appreciated,
    Thank you
    Seraj

    // This is the code that listens for the menu item
    private void RBmmActionPerformed(java.awt.event.ActionEvent evt) {                                    
            calc.setIN(false);
            updateData();                    // updates some data
            paint2();                           // my special draw fuction shown below
        public void paint2()                          // this the special paint that draws on the JPanel
            Graphics page = jPanel1.getGraphics();
            if(start_end)
                myPic.draw(page);
            else
                page.setColor(Color.WHITE);
                page.fillRect((int)(OFFSET.getX()), (int)(OFFSET.getY()), (int)(R_BOUND-OFFSET.getX()), (int)(L_BOUND-OFFSET.getY()));
            repaint();             
    public void paint(Graphics g)               // this is the regular paint methode
            MessageHandler();
            ATD_age.repaint();
            StanderdCal.repaint();
            ChildSRF.repaint();
            MessageArea.repaint();
        }I hope that is helpful

  • JMenu and JList

    I want to hava a menu that would have some of the same functionality as a JList, such as the cell renderer, the borders and the list models. I'm working inside a JApplet so creating JPopupMenus are out because of it seems to open new Windows in browsers and has that ugly Applet Window warning.
    I want to open pop up menus over the list elements.
    For example, I have a JList that holds user names for a chat room. When an element in that JList is clicked I want to open a menu that lists commands like "go to users homepage", "private chat", "kick out", etc.
    I've written an applet that uses JMenus. But I'd like to have the user list remain a JList because my applet uses the ListModel to pass the list's content to different parts of the program.
    I'd like to have similar functionality to the following (but with a JList instead of a JMenuBar holding the contents of the list)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MenuTestApplet extends JApplet{
    JPanel p = new JPanel(new BorderLayout());
    JMenuBar menuBar = new JMenuBar();
    JScrollPane scroll;
    public MenuTestApplet(){
         getContentPane().add(p, BorderLayout.CENTER);
         menuBar.setBackground(Color.blue.brighter().brighter());
         menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
         for (int i=1; i<50;i++){
         addNewMenu("Menu "+i);
         p.add(new JLabel("nothing here"), BorderLayout.CENTER);
         scroll = new JScrollPane(menuBar);
         p.add(scroll, BorderLayout.CENTER);
    public void addNewMenu(String title){
         JMenu m = new JMenu(title);
         m.setBackground(Color.blue.brighter());
         JMenuItem item = new JMenuItem("one");
         item.setBackground(Color.blue.brighter());
         m.add(item);
         item = new JMenuItem("two");
         item.setBackground(Color.blue.brighter());
         m.add(item);
         item = new JMenuItem("THR33");
         item.setBackground(Color.blue.brighter());
         m.add(item);     
         m.setMinimumSize(m.getPreferredSize());
         m.setMenuLocation(7, 3);
         menuBar.add(m);
    I'm just looking for ideas right now.
    thanks

    would simply adding JMenu's to the JList do the trick?
    this was suggested by someone to me, but I thought that the elements in the JList are simply painted panels that use the objects toString() in the CellRenderer.

  • JMenu and JCheckBox

    Hi all,
    How to select/unselect more than one JCheckBoxMenuItem in a time in a JMenu ?
    (for instance i'm not able to select 3 item out of 10 in one time -need to do the manip 3 times ...)
    thanks in advance

    Ok, i give you an example to be more concrete:
    Lets say we have a JMenu with Checkbox giving different movie categories (action, thriller,comedy,western...) .
    -> i want to view in my jtable only the categories "action" and "western" by selecting them in the Menu.
    -> but as it's currently implemented, once you select one of the category, the JMenu directly auto-closes, so you have once again to open the menu to select the second.
    Suppose you have 10 checkbox (out of 20) to select...

  • JMenu and making the popup always visible

    hi,
    i need to keep the menu's popup visible always and eventually the selection of an item even when the focus moves to another component-a text field nearby for example.can this (and how if it is) be achieved or it is totally up to the look and feel and i can't do much about it?
    thank you.

    just for the record,it seems it actually is doable.at least i have a prototype working.it involves removing default mouse listeners from jmenu i jmenuitem and installing "custom" ones that manage the transition of the menu from one state to another.

  • JMenu and About Boxes

    Hi Guys,
    I'm making a small program in Java for a Uni project and part of the specification is that the user should be able to select an 'About This Application' option from the menu bar which shows up asmal dialog box with some text and an OK button to close the box, but I'm not sure how to code this. So far I have...
    menuBar = new JMenuBar();
         menuEdit = new JMenu("Edit"); //shows the menu item Edit.
         menuEdit.setFont(fontMenu);
         menuView = new JMenu("View"); //shows the menu item View.
         menuView.setFont(fontMenu);
         menuHelp = new JMenu("Help"); //shows the menu item Help.
         menuHelp.setFont(fontMenu);
         menuBar.add(menuEdit); //adds menu item "Edit" to the menu bar
         menuBar.add(menuView); //adds menu item "View" to the menu bar
         menuBar.add(menuHelp); //adds menu item "Help" to the menu bar
         setJMenuBar(menuBar);
    I was wondering if anybody could suggest how I can add to this code so that the user can then click on Help in the menu bar, select 'About this application' and on doing so a dialog box pops up with some text information and the OK button.
    Any help appreciated I'm in a bit of a fix here!
    Thanks!

    As a push in the right direction, look to use JOptionPane for creating simple dialogs. (dialog tutorial)

Maybe you are looking for