Results 1 to 14 of 14
  1. #1
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default How to remove "unused" space in JMenu

    Is there an easy way to remove unused space from JMenu. I mean the space for checkbox tick element. When I'm not using JCheckBoxMenuItem it looks just strange with that empty space... This is an URL to screen shot with marked space witch I'm talking about: img.polsl.eu/t,5122e.jpg

  2. #2
    ashok2004_urs is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    You can use JCheckBoxMenuItem.getPreferredWidth()

    It will automatically adjust width and remove unused spaces.

    or some thing like that.

  3. #3
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Not working because JPopupMenu.getPreferredSize().width and JMenuItem.getPreferredSize().width are almost the same (2px difference). When I'm trying to decrease the width of pop-up menu free space is still there but item text is truncated from the right... Any other ideas?

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Why is the code written with JCheckBoxMenuItem instead of JMenuItem? When I created a popup menu with JMenuItem-s, there was no leading space. I expect leading space is added to all menu items if any one of them has an icon or checkbox or radio button.

  5. #5
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Of course I'm using only JMenuItem in my code and thats why I'm curious why there is that free space like in check-boxes. There are only for sure JMenuItems with icons...

  6. #6
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    only for sure JMenuItems with icons...
    "with icons"??? Those icons will create the leading blank space.
    Try setting them to all have a null icon: setIcon(null);

  7. #7
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    With normal JMenuItem without any icons it looks like this: img.polsl.eu/i,be632.jpg... Again sorry for non-clickable link - still waiting for possibility of normal posting :)

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    Maybe you can't post a link but you can post code.

    For better help, post your SSCCE.

  9. #9
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    Try setting them to all have a null icon: setIcon(null);
    Nothing :(

    I would post some code before if it was something code depended for me but I don't think that it is...

    This is only a sample application with easiest code for JPopupMenu and in it the free space before JMenuItems text is present... Maybe it's something with WindowsLookAndFeel?

    Java Code:
    import java.awt.Dimension;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.UIManager;
    
    public class TestMenu extends JFrame {
    	private static final long serialVersionUID = -6727171074713974942L;
    
    	public TestMenu() {
    		super("Test");
    		
    		final JPopupMenu popupMenu = new JPopupMenu();
    		
    		popupMenu.add(new JMenuItem("Test #1"));
    		popupMenu.add(new JMenuItem("Test #2"));
    		
    		addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent e) {
    				popupMenu(e);
    			}
    			
    			@Override
    			public void mouseReleased(MouseEvent e) {
    				popupMenu(e);
    			}
    			
    			private void popupMenu(MouseEvent e) {
    				if (e.isPopupTrigger()) {
    					popupMenu.show(e.getComponent(), e.getX(), e.getY());
    				}
    			}
    		});
    		
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(200, 150));
    		
    		pack();
    		
    		setLocationRelativeTo(null);
    	}
    	
    	public static void main(String[] args) throws Exception {
    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // com.sun.java.swing.plaf.windows.WindowsLookAndFeel
    		(new TestMenu()).setVisible(true);
    	}
    }
    Last edited by nowy; 12-05-2009 at 12:46 PM.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    If it is a Windows LAF problem then you will probably have to look at the code which comes with the JDK and then customize the LAF to your requirements:

    com.sun.java.swing.plaf.windows.WindowsMenuItemUI

  11. #11
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Hmmm, I thought that there's an easier way to do it but thanks for help...

  12. #12
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    The indentation of menu items in the sample program is a direct result of
    specifying UIManager.getSystemLookAndFeelClassName().
    The look-and-feel you get is that of Windows
    and Windows l-a-f says there is an indent. Always.

    I thought that there's an easier way ...
    Depends on your definition of easy.

    Choice 1. Abandon the system l-a-f approach. Use metal or nimbus.
    Or just omit UIManager.setLookAndFeel(...)

    Choice 2. Abandon your scruples. (Or at least my scruples.)
    If you pretend to offer the system l-a-f but omit the menu indent,
    you are NOT offering the system l-a-f.
    Your app will differ from other apps, however minutely.

    Having decided you wish to abandon scruples you take advantage
    of my wasted hours. Add two lines to the program
    to set MenuItem.arrowIcon and MenuItem.checkIcon to empty Icon-s.

    The program now looks like this.

    Java Code:
    package Test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TestMenuToo extends JFrame {
    	private static final long serialVersionUID = -6727171074713974942L;
    
    	public TestMenuToo() {
    		super("Test");
    		final JPopupMenu popupMenu = new JPopupMenu();
    		popupMenu.add(new JMenuItem("Test #1"));
    		popupMenu.add(new JMenuItem("Test #2"));
    
    		addMouseListener(new MouseAdapter() {
    			@Override public void mousePressed(MouseEvent e) { pop(e); }
    			@Override public void mouseReleased(MouseEvent e) { pop(e); }
    
    			private void pop(MouseEvent e) {
    				if (e.isPopupTrigger()) {
    					popupMenu.show(e.getComponent(), e.getX(), e.getY());
    				}
    			}
    		});
    
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(200, 150));
    		pack();
    		setLocationRelativeTo(null);
    	}
    
    [COLOR="Blue"]	static public class EmptyIcon implements Icon{
    		private int width = 0, height = 0;
    		public void paintIcon(Component c, Graphics g, int x, int y) { }
    		public int getIconWidth() { return width; }
    		public int getIconHeight() { return height; }
    	}[/COLOR]
    
    	public static void main(String[] args) throws Exception {
    
    [COLOR="Blue"][B]		UIManager.put("MenuItem.arrowIcon", new EmptyIcon());
    		UIManager.put("MenuItem.checkIcon", new EmptyIcon());[/B][/COLOR]
    
    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    				// com.sun.java.swing.plaf.windows.WindowsLookAndFeel
    
    		(new TestMenuToo()).setVisible(true);
    	}
    }

    {{My work here reminds me of an old joke:
    The mechanic looked in the engine and tweaked a widget,
    thus fixing the problem. He charged the lady $10.
    Incensed at the large charge for such a trivial effort,
    she demanded an itemized bill. The mechanic though a bit
    and submitted this:
    • twisting small widget ................. $1
    • knowing which widget to twist .... $9
    • please remit ........................... $10

    }}

  13. #13
    nowy is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks for your reply, now I feel satisfied :) I'm a bit pedantic and that "problem" was kind of important for me to solve :) Your proposition is a good start for something useful for me for using in the future if I wish to be still so strict. Thanks :)

  14. #14
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    For the record, the code that actually lays out the menu items is in
    javax.swing.plaf.basic.BasicMenuItemUI.layoutMenuI tem(...)

Similar Threads

  1. Replies: 7
    Last Post: 06-10-2009, 09:41 AM
  2. Replies: 10
    Last Post: 05-11-2009, 11:56 AM
  3. Replies: 3
    Last Post: 04-22-2009, 08:25 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 14
    Last Post: 06-12-2008, 08:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •