Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Draw a sidebar on JPopupMenu like XP Start Menu

Posted on 2004-05-02
8
Medium Priority
?
361 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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