Solved

Draw a sidebar on JPopupMenu like XP Start Menu

Posted on 2004-05-02
8
356 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

829 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