?
Solved

Draw a sidebar on JPopupMenu like XP Start Menu

Posted on 2004-05-02
8
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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