This discussion is archived
1 Reply Latest reply: Feb 13, 2013 3:45 AM by 800268 RSS

Is this a bug in Swing in JDK1.6/JDK1.7 but not in JDK1.5?

847570 Newbie
Currently Being Moderated
I have an application for which GUI was developed in Java Swing JDK1.5.I am planning to upgrade the JDK to JDK1.6 but doing so produces problem for me.

Problem Statement : If I open few dialogs(say 10) and dispose them and than call method 'getOwnedWindows()' , it returns 0 in JDK1.5 but returns 10 in JDK1.6. As in JDK1.6 it returns 10, my algorithm to set focus is not working correctly as it is able to find invlaid/disposed dialogs and try to set the focus on it but not on the correct and valid dialog or component because algorithm uses getOwnedWindows() to get the valid and currently open dialog.

Can anyone suggest me the workaround to avoid this problem in JDK1.6?

Following piece of code can demonstrate the problem.

Custom Dialog Class :
import javax.swing.JDialog; 
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

public class CustomDialog extends JDialog implements ActionListener {
    private JPanel myPanel = null;
    private JButton yesButton = null;
    private JButton noButton = null;
    private boolean answer = false;
    public boolean getAnswer() { return answer; }

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {
     super(frame, modal);
     myPanel = new JPanel();
     getContentPane().add(myPanel);
     myPanel.add(new JLabel(myMessage));
     yesButton = new JButton("Yes");
     yesButton.addActionListener(this);
     myPanel.add(yesButton);     
     noButton = new JButton("No");
     noButton.addActionListener(this);
     myPanel.add(noButton);     
     pack();
     setLocationRelativeTo(frame);
     setVisible(true);
     //System.out.println("Constrtuctor ends");
    }

    public void actionPerformed(ActionEvent e) {
     if(yesButton == e.getSource()) {
         System.err.println("User chose yes.");
         answer = true;
         //setVisible(false);
     }
     else if(noButton == e.getSource()) {
         System.err.println("User chose no.");
         answer = false;
         //setVisible(false);
     }
    }
    
    public void customFinalize() {
         try {
               finalize();
          } catch (Throwable e) {
               e.printStackTrace();
          }
    }
}
Main Class:
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Window;

public class TestTheDialog implements ActionListener {
    JFrame mainFrame = null;
    JButton myButton = null;
    JButton myButton_2 = null;

    public TestTheDialog() {
        mainFrame = new JFrame("TestTheDialog Tester");
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
        myButton = new JButton("Test the dialog!");
        myButton_2 = new JButton("Print no. of owned Windows");
        myButton.addActionListener(this);
        myButton_2.addActionListener(this);
        mainFrame.setLocationRelativeTo(null);
        
        FlowLayout flayout = new FlowLayout();
        mainFrame.setLayout(flayout);
        
        mainFrame.getContentPane().add(myButton);
        mainFrame.getContentPane().add(myButton_2);

        mainFrame.pack();
        mainFrame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        if(myButton == e.getSource()) {
               System.out.println("getOwnedWindows 1 " + mainFrame.getOwnedWindows().length);
             createMultipleDialogs();

            int i = 0;
               for (Window singleWindow : mainFrame.getOwnedWindows()) {
                    System.out.println("getOwnedWindows " + i++ + " "
                              + singleWindow.isShowing() + " "
                              + singleWindow.isVisible() + " " + singleWindow);
               }
               System.out.println("getOwnedWindows 2 " + mainFrame.getOwnedWindows().length);
            //System.gc();
               System.out.println("getOwnedWindows 3 " + mainFrame.getOwnedWindows().length);
            //System.gc();
               System.out.println("getOwnedWindows 4 " + mainFrame.getOwnedWindows().length);
        } else if (myButton_2 == e.getSource()) {
               System.out.println("getOwnedWindows now: " + mainFrame.getOwnedWindows().length);
        }
    }
        
     public void createMultipleDialogs() {
          for (int a = 0; a < 10; a++) {
               CustomDialog myDialog = new CustomDialog(mainFrame, false,
                         "Do you like Java?");
               myDialog.dispose();
               myDialog.customFinalize();
          }
    }

    public static void main(String argv[]) {

        TestTheDialog tester = new TestTheDialog();
    }
}
Running the above code gives different output for JDK1.5 and JDK1.6

I would appreciate your help in this regards.

Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points