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.

Similar Messages

  • 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.

  • Help with TableRowSorter , JTable and JList

    Hello,
    I�m developing an application in which I�m using a Jtable and Jlist to display data from a database. Jtable and Jlist share the same model.
    public class DBSharedModel extends DefaultListModel implements TableModelFor the Jtable I set a sorter and an filter
    DBSharedModel dataModel = new DBSharedModel();
    Jtable jTable = new JTable(dataModel) ;
    jTable.createDefaultColumnsFromModel();
    jTable.setRowSelectionAllowed(true);
    TableRowSorter<DBSharedModel> sorter = new TableRowSorter <DBSharedModel> (dataModel);
    jTable.setRowSorter(sorter);From what I read until now JavaSE 6 has NOT a sorter for JList (like one for JTable).
    So, I am using one from a this article http://java.sun.com/developer/technicalArticles/J2SE/Desktop/sorted_jlist/index.html
    When I sort the data from the table, I need to sort the data from the list, too.
    My ideea is to make the Jlist an Observer for the Jtable.
    My questions are:
    1.     How can I find if the sorter makes an ASCENDING or DESCENDING ordering?
    2.     How can I find what the column that is ordered?
    Or if you have any idea on how can I do the ordering on Jlist to be simultaneous to Jtable .
    Please help!
    Thank you

    Oh what the hell:
    Sun's basic Java tutorial
    Sun's New To Java Center. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    http://javaalmanac.com. A couple dozen code examples that supplement The Java Developers Almanac.
    jGuru. A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    Bruce Eckel's Thinking in Java (Available online.)
    Joshua Bloch's Effective Java
    Bert Bates and Kathy Sierra's Head First Java. This one has been getting a lot of very positive comments lately.

  • ComboBox and JList working to gether?

    Hey
    Is it possible to get ComboBox and JList to work together?
    Eksampel.
    In the ComboBox there are 3 options to pick. Football, Hockey and Baseball. When one of these are picked all the names of the players
    from for example football is shown in a JList.
    Is this possbile? If yes how? can you give example or link to toturial?

    do you know a tutorial on this?Did you read the API for either JComboBox or JList????????
    Both contain links to the Swing tutorial.
    And why is it so difficult to post a Swing related question in the Swing forum???????

  • Strange UI sticking issue with drag/drop and JList

    Hi all,
    I have implemented drag/drop between a jtree and jlist. The jtree can accept drops from the jtree (nodes to other parent nodes) and from the jlist. The jlist can accept nodes of the tree being dropped on it.
    Let me say this much, it all works. Works fine and great. BUT, the purpose of my post is I am seeing a very strange and not easily reproducible problem.
    I should first say that after a successful drop, the list removes the selected items. I do support drag/drop for multiple items.
    When I drag items from the list to the tree, reasonbly normal (not too fast), everything seems to work every time. But if I drag an item quickly from the list to the tree, for some reason after the items are removed from the list, another item is selected, sometimes NOT in the place of where the item(s) were selected. Infact, when I drag quickly, I notice the selection changes in the list itself. I only accept tree objects being dropped in the list, so there is no ability to drag/drop list items onto itself, and thus if I drag a list item around the list normally, nothing changes..the item I am dragging stays selected and the cursor shows the drag arrow.
    So this problem seems seems odd. I can reproduce it fairly easily, but only when I click and drag quickly.
    Anyone have ideas? I don't see where in my list code that as I drag, when drop is done, etc that I select anything in the list. I have tried JDK 1.4.1 and 1.4.2 and it does it in both. I don't see a bug in the bug database for it, and I am not sure its a bug in the JVM or my own code!
    Any help is appreciated. Can't really post code as its a large set of code and it is dependent on our company project, so it would take some time to take the code out into its own app to test it again.

    Hi,
    +" Please note that dropSite==null is a legal value for collection components. dropSite=null and DropOrientation==ON indicates that the drop has happened outside the data portion of the collection component and should be treated as a drop on the entire component Also note that dropSite==null and DropOrientation==AFTER indicates that the drop has happened on an empty collection component and the user intends to append the data."+
    http://download.oracle.com/docs/cd/E17904_01/apirefs.1111/e10684/oracle/adf/view/rich/event/DropEvent.html#getDropSite__
    List dropRowKey = (List) dropEvent.getDropSite();
            //if no dropsite then drop area was not a data area
            if(dropRowKey == null){
                return DnDAction.NONE;
            }Frank
    Edited by: Frank Nimphius on Feb 18, 2011 11:18 PM

  • 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'.

  • 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 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

  • JTabbedPane and JList

    Hi,
    I'm trying to build an application for a clothing store. I have a JTabbedPane which contains 4 panels (Add,Delete,Modify,Display Customer). Each panel contains a list (JList in a JScroolPane) of customers lastnames. The problem is the following: When I add a customer, the customer list of Add panel is immediately updated (which is good); but when I look at the same list under Delete, Modify, Display panels, the list is not updated. I need to find a way of updating all lists on the panels when one of the lists change in any one panel. Does this make sense? Any suggestions?

    Use the Observer/Observable pattern: there is an implementation for JList in the Form of the ListModel (in the pakage javax.swing).
    Create a class that implements ListModel and which contains your data, and add, replace and remove functions. Then add that class to the different JLists with
    myJList.setListModel(myListModel);or passing myListModel in the constructor of JList. The JList wil then be added to the list of ListDataListeners in myListModel.
    For more information on how it works have a look at the apis: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ListModel.html

  • 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)

  • JMenu and doClick

    Hi;
    I have a class named as IntelliMenu which extends JMenu. I added speech recognition ability to the JMenu. However when the user says the action command of the menu ( for example "File") I can not make the menu items of this menu visible. Below is the code that I am using. Can you tell me what is wrong with the code? The interesting point is that it sometimes work and sometimes not.
    menu.doClick();
    menu.repaint();  //tried without this line too
    menu.validate();  //tried without this line too

    no relation with speech recognition.Then why did your question mention you where using speech recognition? Don't confuse the problem.
    Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html]How to Use Menus.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • JMenu and GridBagConstraints

    how do i add a JMenu item into a gridbagconstraints layout??

    how do a position a JMenu item to the top left corner using GRIDBAGLAYOUT It doesn't change my answer. It doesn't make sense that you are using a GridBagLayout for a JMenu. JMenu is designed to be added to a JMenuBar.
    Do some work on your own and post the code when you have a problem.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

Maybe you are looking for