Solved

Draw a sidebar on JPopupMenu like XP Start Menu

Posted on 2004-05-02
8
352 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:rubentrancoso
Comment Utility
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
Comment Utility
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
Comment Utility
No objections, he did it himself; quite interesting!

;JOOP!
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQ-ing the question and refunding 100  points

Thanks sciuriware !

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now