views:

324

answers:

4

Hello,

I'm facing a frustrating issue. I have an application where the scroll wheel doesn't work in a JDialog window (but works in a JFrame).

Here's the code:

import javax.swing.*;
import java.awt.event.*;

public class Failtest extends JFrame {

 public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
       @Override
       public void run() {
          new Failtest();             
       }
    });

 }

 public Failtest() {
  super();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  setTitle("FRAME");
  JScrollPane sp1 = new   JScrollPane(getNewList());
  add(sp1);
  setSize(150, 150);
        setVisible(true);


  JDialog d = new JDialog(this, false);// NOT WORKING
        //JDialog d = new JDialog((JFrame)null, false); // NOT WORKING
        //JDialog d = new JDialog((JDialog)null, false);// WORKING - WHY? 

  d.setTitle("DIALOG");
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  JScrollPane sp = new JScrollPane(getNewList());

  d.add(sp);
  d.setSize(150, 150);
  d.setVisible(true);
 }

 public JList getNewList() {
  String objs[] = new String[30];
  for(int i=0; i<objs.length; i++) {
  objs[i] = "Item "+i;
 }
 JList l = new JList(objs);
  return l;
 }
}

I found a solution which is present as a comment in the java code - the constructor receiving a (JDialog)null parameter.

Can someone enlighten me? My opinion is that this is a java bug.

Tested on Windows XP-SP3 with 1 JDK and 2 JREs:

D:\Program Files\Java\jdk1.6.0_17\bin>javac -version
javac 1.6.0_17

D:\Program Files\Java\jdk1.6.0_17\bin>java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

D:\Program Files\Java\jdk1.6.0_17\bin>cd ..

D:\Program Files\Java\jdk1.6.0_17>java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)

Thank you in advance,

Iulian Şerbănoiu

PS: The problem is not new - the code is taken from a forum (here) where this problem was also mentioned - but no solutions to it (yet)

LATER EDIT: The problem persists with jre/jdk_1.6.0_10, 1.6.0_16 also

LATER EDIT 2: Back home, tested on linux (Ubuntu - lucid/lynx) - both with openjdk and sun-java from distribution repo and it works (I used the .class file compiled on Windows) !!! - so I believe I'm facing a JRE bug that happens on some Windows configurations.

+1  A: 

Its not a bug, when using:

 JDialog d = new JDialog(this, false);
 JDialog d = new JDialog((JFrame)null, false);

You are calling the constructor with Frame in the first parameter

But when using

 JDialog d = new JDialog((JDialog)null, false);

You are calling the constructor with JDialog in the first parameter

Might be confusing because both are null but even though, you should specify which constructor exactly you want.

UPDATE
After your comment, i just realized whats the problem :), im unable to reproduce it though. The mouse scrolls in both the Dialog and the Frame. I'm using Java 1.6 on Snow Leopard

medopal
Then why the scroll works only in the "(JDialog)null" case? This is the real question. I actually need an explanation - this solution for me is *just* a hack
Iulian Şerbănoiu
A: 

There is something weird with your java install : your default version is not the 1.6.0_17 you show us, but an even more recent 1.6.0_18.

Anyway, using my (just a little older) 1.6.0_16 version on Windows XP, I cannot reproduce your issue, letting me think it may be a bug in J

Riduidel
Look at the command line. For development I use JDK 1.6.0_17(which contains also JRE with the same version) but the system (when I moved out of bin directory) has a newer JRE (1.6.0_18)
Iulian Şerbănoiu
Yes, of course, but I personnally tend to develop using the JDK that the systelm uses as default, for the sake of coherence. Anyway, I was not able to reproduce your behaviour in my JDK 1.6.0_16, and THAT is weird.
Riduidel
Indeed - I'm downloading the JDK 1.6.0_16 and will check the results. Thanks for the hint
Iulian Şerbănoiu
I edited the answer - the problem persists even with jdk 1.6.0_16
Iulian Şerbănoiu
Are we talinkg about the same thing ? From testing it on my box, I believed it worked with my version, but you say it don't. So, here is my test : I took your code, compiled it, then run it. Two windows showed, one titled 'FRAME' and the other titled 'DIALOG'. On each of them, without clicking on it, when i scroll the wheel, nothing happens. But when I click the window title bar, and scroll, the list moves correctly. So, what is the defective behaviour ?
Riduidel
The scroll works in the Frame but doesn't work in the Dialog (only with the hack present in the code as comment). This is the *real* problem. I tested on linux - it works there ... so it's a problem with my Windows configuration (it is very possible that this is a JRE bug, triggered by some system configuration)
Iulian Şerbănoiu
+1  A: 

I haven't been able to reproduce this on my system (Java 6u18 on Windows 7). However, I suspect that this is a focus issue, where the mouse events aren't being received by the scrollpane when you use the JFrame version of the JDialog constructor and yet they are being received when you use the JDialog version.

Several things you can try here:

  1. You can try clicking on the dialog and/or the dialog's scrollbar and see if mouse scrolling affects the scrollbar after you have clicked on it.
  2. You can programmatically request focus for the scrollpane by calling requestFocusInWindow().
  3. If requestFocusInWindow() fails, try requestFocus(). (requestFocus() is discouraged because of platform-specific variations in its behavior, but you appear to have a platform-specific problem.)
  4. You can add a FocusListener to your scrollpane to track when it gains and loses focus, which should help you confirm whether this problem has anything to do with focus at all.
  5. You can add a MouseWheelListener to your scrollpane to see if each MouseWheelEvent is being received by the scrollpane or not.

Like I said, I couldn't reproduce this problem on my system, but these are the things I would try if I were troubleshooting a problem like the one you describe.

Joe Carnahan
+1  A: 

I know it's not likely the answer, but can you please entertain us and FIX your buggy code such that the frame is created on the EDT (as per Sun's specifications)??

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Failtest();             
        }
    });
}

Until you've done, I'm not sure you can rule out anything.

I've got a "works for me" using Windows Vista on JRE 1.6.0_20.

taftster
You are right - it's not the answer, but I did modify the code according to your suggestion. +1 for that. Thanks
Iulian Şerbănoiu