Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By PhHein

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

  1. #1
    gpmax is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

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

    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 :
    Java Code:
    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:

    Java Code:
    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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    DarrylBurke likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

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

    I'm going to hazard a guess here, based on the dispose API, and say it was 1.5 that was wrong.
    "
    The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show.
    "
    That implies that dispose only frees up the contents of a Window, it does not remove the Window.
    So a call to getOwnedWindows ought to supply these disposed but not deleted windows, as they are recoverable.
    You need to use the isDisplayable flag as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Difference between jdk1.5 and jdk1.6
    By pradnya in forum New To Java
    Replies: 10
    Last Post: 12-08-2010, 11:52 AM
  2. Replies: 2
    Last Post: 10-22-2010, 07:54 PM
  3. EJB invokation failing in jdk1.5.0_23 but works in jdk1.6.0
    By randle169 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 01-28-2010, 09:09 AM
  4. How to uninstall jdk1.4 or jdk1.5
    By ran_sushmi in forum Advanced Java
    Replies: 0
    Last Post: 07-20-2009, 12:45 PM
  5. JNLP - Swing application compiled in jdk1.5 not working in jdk1.6
    By mahendra.athneria in forum AWT / Swing
    Replies: 4
    Last Post: 01-20-2009, 08:27 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
  •