Results 1 to 12 of 12
  1. #1
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default JMenu bar - adding an unclickable bar section?

    How do you add an unclickable part of the JMenuBar component, so that you can span the bar across the entire screen width or create more distance between multiple sets of JMenuBar menu titles?

    Thanks!
    May the Newtonian physics be with you.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JMenu bar - adding an unclickable bar section?

    Add a Box.createHorizontalGlue()

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    I got it working now, but I used createRigidArea instead to specify length, since the glue didn't allow that. Thanks for pointing me in the right direction, though. :)
    Last edited by DrMadolite; 08-18-2012 at 01:06 AM.
    May the Newtonian physics be with you.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JMenu bar - adding an unclickable bar section?

    Your original question asked how to
    span the bar across the entire screen width
    which was why I suggested glue.

    I used createRigidArea instead
    For a defined horizontal gap, you should be using createHorizontalStrut(...). Not createRigidArea(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DarrylBurke View Post
    Your original question asked how to which was why I suggested glue.
    But how exactly do you use Glue to make the JMenuBar span the entire width? Doesn't seem to work in any way - createHorizontalGlue() doesn't seem to allow me to define a size. So I'm assuming that you're talking about the Glue itself being able to auto-span the width of the screen when it's between that and the last item, or something? But I tried putting it after the last JMenu (as well as between JMenus), but it didn't react at all, it just stayed 0 pixels wide. Does it matter what layout I'm using? (FlowLayout atm)

    Quote Originally Posted by DarrylBurke View Post
    For a defined horizontal gap, you should be using createHorizontalStrut(...). Not createRigidArea(...).
    Yeah, I guess I can understand why too, 1D being a quicker process than 2D, and all. Thanks for tip.
    Last edited by DrMadolite; 08-18-2012 at 05:52 AM.
    May the Newtonian physics be with you.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DrMadolite View Post
    I'm assuming that you're talking about the Glue itself being able to auto-span the width of the screen when it's between that and the last item, or something? But I tried putting it after the last JMenu (as well as between JMenus), but it didn't react at all, it just stayed 0 pixels wide. Does it matter what layout I'm using? (FlowLayout atm).
    Why did you play with the layout? JMenuBar has a default layout that is perfectly suited to its intended use.

    No, I'm not going to tell you which layout that is -- find out for yourself.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DarrylBurke View Post
    Why did you play with the layout? JMenuBar has a default layout that is perfectly suited to its intended use.
    I wasn't aware that JMenuBar was layout-specific (or any other JComponents, for that matter). But judging from your answer, they clearly are. The reason why I'm using FlowLayout is because I based my project on some video I saw on Youtube where FlowLayout was used and I didn't want to deviate too much from it until I knew more. I did try some other layouts though, like BoxLayout (since the Glue class is called Box, I figured), but it didn't seem to work so I reverted.

    Thanks for the tip though, I'll read further up on all the JComponents and try to pinpoint what their default layouts are, as neither Java Tutorials or Oracle Docs seems to mention it (although I've probably just overlooked it). I'm sure it's obvious once I see what the problem actually is. Kinda typical, that one, for newbies hehe. "Oh my gaaawd, was THAT the problem?!!" LOL the debugger's lot in life, I presume.

    Cheers.
    Last edited by DrMadolite; 08-18-2012 at 12:02 PM.
    May the Newtonian physics be with you.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DrMadolite View Post
    I'll read further up on all the JComponents and try to pinpoint what their default layouts are, as neither Java Tutorials or Oracle Docs seems to mention it (although I've probably just overlooked it).
    You haven't overlooked it, and default layouts aren't the only implementation detail you usually don't need to worry about nor know, so are neither mentioned in the JavaDoc nor in the Tutorials. But here's a hint:
    Java Code:
    System.out.println(new JMenuBar().getLayout().getClass().getName());
    Note: JMenuBar happens to use a layout that is part of the public API; there are other components that use a layout manager specific to the (sub)components contained within, layouts that are NOT public classes.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DarrylBurke View Post
    You haven't overlooked it, and default layouts aren't the only implementation detail you usually don't need to worry about nor know, so are neither mentioned in the JavaDoc nor in the Tutorials. But here's a hint:
    Java Code:
    System.out.println(new JMenuBar().getLayout().getClass().getName());
    Note: JMenuBar happens to use a layout that is part of the public API; there are other components that use a layout manager specific to the (sub)components contained within, layouts that are NOT public classes.
    db
    Ok I'll look into it, thanks again.
    May the Newtonian physics be with you.

  10. #10
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    The problem is that, if I stop using a chosen layout for the JMenuBar, it acts all funky. It doesn't align properly to the top border, nor do the createHorizontalGlue have any effect. I used that println too and that mentions DefaultMenuLayout, so I'll try to search the internet for how exactly that default thing works. But it's kinda hard when you don't even know what to look for:

    Java Code:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    
    
    public class SolFrame extends JPanel implements ActionListener {
    	
    	private static final long serialVersionUID = 1L;
    
    	private JMenuBar bar;
    	
    	private JMenu menu1;
    	private JMenuItem m101;
    	private JMenuItem m102;
    	private JMenuItem m103;
    	private JMenuItem m104;
    	private JMenuItem m105;
    	
    	private JMenu menu2;
    	private JMenuItem m201;
    	private JMenuItem m202;
    	private JMenuItem m203;
    	private JMenuItem m204;
    	private JMenuItem m205;
    	
    	
    	
    	public SolFrame() {
    		
    
    		// ---------- this.setLayout(new BoxLayout(bar, BoxLayout.PAGE_AXIS)); Tried this as a test, but only made the JMenuBar invisible.
    		
    
    		bar = new JMenuBar();
    		
    		
    		// ---------- "FILE" DROP-DOWN MENU ----------
    		
    		menu1 = new JMenu("File");
    		m101 = new JMenuItem ("New Game");
    		m102 = new JMenuItem ("Save/Load");
    		m103 = new JMenuItem ("Options");
    		m104 = new JMenuItem ("Credits");
    		m105 = new JMenuItem ("Exit");
    		
    		bar.add(menu1);
    		menu1.add(m101);
    		menu1.addSeparator();
    		menu1.add(m102);
    		menu1.add(m103);
    		menu1.add(m104);
    		menu1.addSeparator();
    		menu1.add(m105);
    		
    		ButtonGroup group1 = new ButtonGroup();
    		group1.add(m101);
    		group1.add(m102);
    		group1.add(m103);
    		group1.add(m104);
    		group1.add(m105);
    		
    		m101.addActionListener(this);
    		m102.addActionListener(this);
    		m103.addActionListener(this);
    		m104.addActionListener(this);
    		m105.addActionListener(this);
    		
    		
    		// ---------- "MENU2" DROP-DOWN MENU ----------
    		
    		menu2 = new JMenu("Menu2");
    		m201 = new JMenuItem ("New Game");
    		m202 = new JMenuItem ("Save/Load");
    		m203 = new JMenuItem ("Options");
    		m204 = new JMenuItem ("Credits");
    		m205 = new JMenuItem ("Exit");
    		
    		bar.add(menu2);
    		menu2.add(m201);
    		menu2.add(m202);
    		menu2.add(m203);
    		menu2.add(m204);
    		menu2.add(m205);
    		
    		ButtonGroup group2 = new ButtonGroup();
    		group2.add(m201);
    		group2.add(m202);
    		group2.add(m203);
    		group2.add(m204);
    		group2.add(m205);
    		
    		m201.addActionListener(this);
    		m202.addActionListener(this);
    		m203.addActionListener(this);
    		m204.addActionListener(this);
    		m205.addActionListener(this);
    		
    		
    		// ---------- Adds the bar itself and extra effects ----------
    		
    		bar.add(Box.createHorizontalGlue());
    		
    		add(bar);
    		
    	}	
    
    	public void actionPerformed(ActionEvent e) {
    		
    		if (e.getSource() == m101) {
    			setBackground(Color.BLACK);
    		} else if (e.getSource() == m102) {
    			setBackground(Color.RED);
    		} else if (e.getSource() == m103) {
    			setBackground(Color.BLUE);
    		} else if (e.getSource() == m104) {
    			setBackground(Color.MAGENTA);
    		} else if (e.getSource() == m105) {
    			setBackground(Color.GREEN);
    		}
    	}
    }
    I'm really just wondering how exactly I add that createHorizontalGlue(), syntaxically. Also, I'm thinking that there might be something wrong with how I use the JMenuBar in conjunction with the JPanel. Should I use JMenuBar outside of the JPanel? Cause I was under the impression that JPanel is the panel that contain all these JComponents. I'll try JMenuBar outside of it and see what happens, maybe my JMenuBar is messed up by the default layout of the parent JPanel.

    Should all JComponents just be added separately to the JFrame, as long as there's no nesting involved? And is JMenuBar something that can't be nested inside a JPanel (or if it is, then proper use requires further parameters)?

    Thanks.
    Last edited by DrMadolite; 08-18-2012 at 02:13 PM.
    May the Newtonian physics be with you.

  11. #11
    DrMadolite is offline Member
    Join Date
    Jul 2012
    Location
    Norway
    Posts
    29
    Rep Power
    0

    Default Re: JMenu bar - adding an unclickable bar section?

    Main class:

    Java Code:
    import javax.swing.JFrame;
    
    public class Main {
    	
    	public static void main(String[] args) {
    		
    		JFrame sol = new JFrame ("Solaxis Dawn");
    		sol.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		sol.setLocation(300, 100);
    		sol.setResizable(false);
    		sol.setSize(1024, 768);
    		sol.setVisible(true);
    		
    		sol.add(new SolFrame());	
    	}
    }
    May the Newtonian physics be with you.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: JMenu bar - adding an unclickable bar section?

    Quote Originally Posted by DrMadolite View Post
    The problem is that, if I stop using a chosen layout for the JMenuBar, it acts all funky. It doesn't align properly to the top border, nor do the createHorizontalGlue have any effect.
    Java Code:
    		add(bar);
    That's not correct. Read the API for JMenuBar or any of the Swing menu components and follow the link to the tutorial on How to Use Menus, where you will find the correct way to set a menu bar in a frame.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JPanel not displaying after adding JMenu logic
    By AMCNovember in forum New To Java
    Replies: 1
    Last Post: 12-18-2011, 05:44 AM
  2. Adding a JPanel to a JMenu - Focus issues
    By snoopygee in forum AWT / Swing
    Replies: 9
    Last Post: 05-06-2011, 09:53 AM
  3. DisplayTag and 508 Section Law
    By khv in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 11-29-2010, 05:32 PM

Posting Permissions

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