Results 1 to 12 of 12
- 08-17-2012, 05:35 AM #1
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
- 08-17-2012, 07:41 AM #2
Re: JMenu bar - adding an unclickable bar section?
Add a Box.createHorizontalGlue()
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-18-2012, 01:00 AM #3
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
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.
- 08-18-2012, 05:16 AM #4
Re: JMenu bar - adding an unclickable bar section?
Your original question asked how to
which was why I suggested glue.span the bar across the entire screen width
For a defined horizontal gap, you should be using createHorizontalStrut(...). Not createRigidArea(...).I used createRigidArea instead
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-18-2012, 05:39 AM #5
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
Re: JMenu bar - adding an unclickable bar section?
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)
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.
- 08-18-2012, 09:49 AM #6
Re: JMenu bar - adding an unclickable bar section?
Why do they call it rush hour when nothing moves? - Robin Williams
- 08-18-2012, 11:50 AM #7
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
Re: JMenu bar - adding an unclickable bar section?
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.
- 08-18-2012, 12:50 PM #8
Re: JMenu bar - adding an unclickable bar section?
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:
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.Java Code:System.out.println(new JMenuBar().getLayout().getClass().getName());
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-18-2012, 12:54 PM #9
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
- 08-18-2012, 01:37 PM #10
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
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:
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.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); } } }
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.
- 08-18-2012, 01:42 PM #11
Member
- Join Date
- Jul 2012
- Location
- Norway
- Posts
- 29
- Rep Power
- 0
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.
- 08-18-2012, 05:04 PM #12
Re: JMenu bar - adding an unclickable bar section?
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
About the Advertising section
By OC-John in forum Suggestions & FeedbackReplies: 0Last Post: 04-17-2012, 09:32 AM -
JPanel not displaying after adding JMenu logic
By AMCNovember in forum New To JavaReplies: 1Last Post: 12-18-2011, 05:44 AM -
Adding a JPanel to a JMenu - Focus issues
By snoopygee in forum AWT / SwingReplies: 9Last Post: 05-06-2011, 09:53 AM -
DisplayTag and 508 Section Law
By khv in forum JavaServer Pages (JSP) and JSTLReplies: 7Last Post: 11-29-2010, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks