Solved

Draw a sidebar on JPopupMenu like XP Start Menu

Posted on 2004-05-02
8
357 Views
Last Modified: 2012-05-04
What can I do to draw a colored sidebar in a JPopupMenu like microsoft start menu ?
I try to add a panel with BorderLayout at left of the container but everything I add is resized and relocated by the layout manager and treated as a menuitem.
0
Comment
Question by:rubentrancoso
  • 3
  • 3
8 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 10976473
I think you mean a 'submenu'.

You can put a submenu, expanding to the side, in every menu position.
This goes for ordinary menu's (from the top, the menu bar) and for pop-up menus.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10976540
It means, that in your code you write:

myMenu.add(new JMenuItem(...));  // That's a normal menu element.
myMenu.add(new JMenu(...));         // That's the 'root' of a submenu.
myMenu.add(new JMenuItem(...));  // That's a normal menu element again.

You must put the created JMenu in the middle into a variable, so that you can treat
it as an other menu, side to the 1st menu, and so you can add all JMenuItems you need it there.

In the SWING tutorial by SUN (downloadable) you'll find a complete code (+picture) example
at "How to Use Menus".

;JOOP!
0
 

Author Comment

by:rubentrancoso
ID: 10977580
thanks sciuriware, but is not that what I want.
Here is the code.

<pre>

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

/*
 * SideBarPopup.java
 *
 * Created on 3 de Maio de 2004, 10:23
 */

/**
 *
 * @author  trancoso
 */
public class SideBarPopup extends JFrame {
   
    CustomPopup cp;
   
    /** Creates a new instance of SideBarPopup */
    public SideBarPopup() {
        this.addWindowListener(
            new WindowAdapter() {
                public void windowClosing( WindowEvent e ) {
                    System.exit(0);
                }
            }
        );
        this.setSize( 500, 500 );
        show();
        cp = new CustomPopup();
        this.addMouseListener(
            new MouseAdapter() {
                public void mousePressed ( MouseEvent e ) {
                    checkPopupTrigger( e );
                }
                public void mouseReleased ( MouseEvent e ) {
                    checkPopupTrigger( e );
                }
                private void checkPopupTrigger( MouseEvent e ) {
                    if ( e.isPopupTrigger() ) {
                        cp.show( e.getComponent(), e.getX(), e.getY() );
                    }
                }
            }
        );
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SideBarPopup sbp = new SideBarPopup();
    }
   
    class CustomPopup extends JPopupMenu {
        public CustomPopup() {
            JMenu m = new JMenu("SubMenu");
            m.add(new JMenuItem("SubItem 1"));
            m.add(new JMenuItem("SubItem 2"));
            this.add( m );
            this.add( new JMenuItem("Item 1"));
            this.add( new JMenuItem("Item 2"));
           
            JPanel sidebar = new JPanel();
            sidebar.setBackground( Color.BLUE );
            this.add( sidebar );
        }
    }
}

</pre>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rubentrancoso
ID: 10977803
put this line
          this.add( sidebar, BorderLayout.WEST );
after "this.add( sidebar );"

I need this sidebar at left of the Container and the MenuItems ( be Items or SubMenus ) must stay at right side.  When the mouse is over the sidebar it canot stay over any item.

I can paint the JPopupMenu and resize...

        public void paint( Graphics g ) {
            super.paint(g);
            g.setColor( Color.BLUE );
            g.fillRect( 0, 0, SIDEBARWIDTH, this.getHeight() );
        }

        public void show( Component c, int x, int y ) {
            Dimension d = this.getPreferredSize();
            this.setPopupSize( d.width + SIDEBARWIDTH, d.height );
            super.show( c, x, y );
        }

But I can´t move the itens to rigth.


0
 

Author Comment

by:rubentrancoso
ID: 10979423
Fow now I will close this question, I think I have the solution. Indeed it need some work. Thank you by now.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.plaf.basic.DefaultMenuLayout;

/*
 * SideBarPopup.java
 *
 * Created on 3 de Maio de 2004, 10:23
 */

/**
 *
 * @author  trancoso
 */
public class SideBarPopup extends JFrame {
   
    CustomPopup cp;
   
    /** Creates a new instance of SideBarPopup */
    public SideBarPopup() {
        this.addWindowListener(
            new WindowAdapter() {
                public void windowClosing( WindowEvent e ) {
                    System.exit(0);
                }
            }
        );
        this.setSize( 500, 500 );
        show();
        cp = new CustomPopup();
        this.addMouseListener(
            new MouseAdapter() {
                public void mousePressed ( MouseEvent e ) {
                    checkPopupTrigger( e );
                }
                public void mouseReleased ( MouseEvent e ) {
                    checkPopupTrigger( e );
                }
                private void checkPopupTrigger( MouseEvent e ) {
                    if ( e.isPopupTrigger() ) {
                        cp.show( e.getComponent(), e.getX(), e.getY() );
                    }
                }
            }
        );
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SideBarPopup sbp = new SideBarPopup();
    }
   
    class CustomPopup extends JPopupMenu {
       
        static final int SIDEBARWIDTH = 20;
       
        public CustomPopup() {
            JMenu m = new JMenu("SubMenu");
            JMenuItem mi = new JMenuItem("SubItem 1");
            m.add(mi);
            mi = new JMenuItem("SubItem 2");
            m.add(mi);
            this.add( m );
            mi = new JMenuItem("Item 1");
            this.add( mi );
            mi = new JMenuItem("Item 2");
            this.add( mi );
           
            JPanel sidebar = new JPanel();
            Dimension d = this.getPreferredSize();
            this.setPopupSize( d.width + SIDEBARWIDTH, d.height );
           
            for ( int i=0; i<this.getComponentCount(); i++ ) {
                ((JMenuItem)this.getComponent(i)).setMaximumSize(d);
                ((JMenuItem)this.getComponent(i)).setAlignmentX(1);
            }
        }
       
        public void show( Component c, int x, int y ) {
            super.show( c, x, y );
        }
       
        public void paint( Graphics g ) {
            super.paint(g);
            g.setColor( Color.BLUE );
            g.fillRect( 0, 0, SIDEBARWIDTH, this.getHeight() );
        }
    }

}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10980509
No objections, he did it himself; quite interesting!

;JOOP!
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10981230
PAQ-ing the question and refunding 100  points

Thanks sciuriware !

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 36
eclipse console opening separately 2 35
ejb entity bean example issue 2 25
Java Inheritance super keyword use 8 29
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question