1

I was trying to add two JMenus to a parent JMenu, when I saw a strange problem :

StateListener stL = new StateListener();            
SpeedListener spL = new VitesseListener();

animation.add(speed);
animation.add(state);

 //Add of JRadiobuttons (st1, st2 and st3) to JMenu : state
 state.add(st1); 
 state.add(st2);
 state.add(st3);

  //Add of JRadiobuttons to ButtonGroup : bG1
  bG1.add(st1);
  bG1.add(st2);
  bG1.add(st3);

   //Setting a default selected button
   st2.setSelected(true);

    //Add of an action listener to JRadioButtons (StateListener : stL)
    st1.addActionListener(stL);
    st2.addActionListener(stL);
    st3.addActionListener(stL);

 //Same code, with speed this time
 speed.add(sp1);
 speed.add(sp2); [...]

This code works great, but when I switch the adding of speed and state to animation, as I do in the code below, the code has a kind of "bug". Indeed, when I pass my mouse over the "speed menu", everything is stuck and I can't do anything (close the program in clean way included) :

[...] animation.add(stae);
animation.add(speed); [...]  

Thanks for your help and sorry for my shitty (I don't even know if I can use this word without being vulgar ^^) english.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
I8u
  • 11
  • 2
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please translate code examples posted to English speaking forums into English as best you can. – Andrew Thompson Aug 28 '12 at 22:52
  • 2
    I tried to make a SSCCE, and strangly, my problem disappeared. Thereby, I'm actually trying to find where is the problem in my entire code (it seems to be a typo or something like that, so I think I can do it alone). I'll post the solution, if I find it. Thanks for your help anyway ^^ – I8u Aug 29 '12 at 09:48
  • First try removing any mouse listeners (from the 'entire code') & see if the problem is still there. – Andrew Thompson Aug 29 '12 at 09:58
  • I'd already tried, and the problem is still here – I8u Aug 29 '12 at 10:28

2 Answers2

1

For me i would use

this.setPreferredSize(new Dimension(800,245));

If u are using any of layout managers. See setSize() vs. setPreferredSize()

Community
  • 1
  • 1
java_xof
  • 439
  • 4
  • 16
  • Hum I'm using BorderLayout, but not directly on my JFrame. That's why I used "setSize" instead of "setPreferredSize". Does I'm wrong while doing this ? – I8u Aug 29 '12 at 10:49
  • See also [Should I **avoid** the use of set{Preferred|Maximum|Minimum}Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – Andrew Thompson Aug 29 '12 at 10:51
  • hehe -as all things JFrame has the other side of the coin as well ;) - personally if i use some JPanels on JFrame with GridBagLayout I prefer using setPreferredSize(), because of the window resizing behavior – java_xof Aug 29 '12 at 11:04
  • Ok so If I use JPanels with GridLayout on resizable JFrame, you advice me to use a setPreferredSize(), that's it ? Because Andrew's link is saying the opposite (to don't use setPreferredSize() on swings at least ^^). Thanks anyway for your help and your two links wich complements each other, I'll remember these tips ! ;) – I8u Aug 29 '12 at 11:20
  • it's more complicated but to make long sentence short - use setSize() when u want your components to be same size at all time, when u want them to be more flexible use setPreferredSize() – java_xof Aug 29 '12 at 11:33
0

I found what was my problem : speed's JMenu was containing 8 JRadioButton. My eighth button was out of my window (which had a too small height), and that's what was rotting my program. The fact that my program was working great with inversed JMenus (speed firstly and state secondly) is because of state's JMenu size. Indeed, it was shorter (containing 3 JRadioButtons instead of 8 for speed), and when I was putting it under speed's JMenu, this one was upper, and had something like 20px more, enough for one more JRadioButton.

I solved this problem with a eight changment :

this.setSize(800,245);

instead of

this.setSize(800,230);

Thank you for your help andrew, do a SSCCE had changed my point of view and I fastly saw what was my problem ;)

I8u
  • 11
  • 2