Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: frame are exiting at the same time

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default frame are exiting at the same time

    i need some quick help here please..

    i have a class names PopUpFrame class
    public class PopUpFrame extends JFrame {
     
        private JPanel popUpPanel;
     
        private JLabel errorMessage;
     
        private Image icon;
     
        private ImageIcon background;
     
        private JButton okButton;
     
        private String errorNotificationFrameTitle;
        private String errorNotificationLabel;
     
        private int popUpWindowWidth;
        private int buttonXPosition;
     
        public PopUpFrame() {
     
            icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
            background = new ImageIcon("c:/pics/mainbck2.jpg");
            okButton = new JButton("Ok");
            errorMessage = new JLabel();
            errorNotificationFrameTitle = "";
            errorNotificationLabel = ""; 
            popUpWindowWidth = 300;
            buttonXPosition = 110;
        }
     
        public void setComponents(Container cont) {
     
            popUpPanel = new JPanel() {
     
                @Override
                public void paintComponent(Graphics g) {
     
                    // Scale image to size of component
                    Dimension d = getSize();
     
                    g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
     
                    super.paintComponent(g);
                }
            };
            popUpPanel.setOpaque(false);
     
            // set this panel's layout to null for absolute positioning of
            // components
            popUpPanel.setLayout(null);
     
            // set the properties and actions of this button
            Action closeAction = new AbstractAction() {
     
                public void actionPerformed(ActionEvent ae) {
     
                    int x = JOptionPane.CLOSED_OPTION;
     
                    if (x == 1) {
     
                        System.exit(0);
                    }
                }
            };
            okButton.addActionListener(closeAction);
            okButton.setBounds(buttonXPosition, 35, 75, 20);
            okButton.setFont(new Font("Monospaced", Font.BOLD, 11));
            okButton.setBackground(Color.LIGHT_GRAY);
     
     
            // set the properties of this label
            errorMessage.setText(errorNotificationLabel);
            errorMessage.setBounds(20, 5, 500, 30);
            errorMessage.setFont(new Font("SansSerif", Font.BOLD, 11));
     
            popUpPanel.add(errorMessage);
            popUpPanel.add(okButton);
            // add the panel to the container
            cont.add(popUpPanel);
        }
     
        /**
         * Sets the error title for this pop-up frame.
         * 
         * @param frameTitle title for the pop-up frame
         */
        public void setErrorNotificationFrameTitle(String frameTitle) {
     
            errorNotificationFrameTitle = frameTitle;
        }
     
        /**
         * Sets the error label message for this pop-up frame.
         *
         * If the message is too long, extend the width of the pop-up frame, and
         * make the button in the center by calculating its x coordinate according
         * to the width of the frame.
         * 
         * @param labelMessage error message for this pop-up frame
         */
        public void setErrorNotificationLabel(String labelMessage) {
     
     
            if (labelMessage.length() > 50) {
     
                popUpWindowWidth = popUpWindowWidth + 300;
                buttonXPosition = buttonXPosition + 150;
            }
            else if (labelMessage.length() > 40) {
     
                popUpWindowWidth = popUpWindowWidth + 250;
                buttonXPosition = buttonXPosition + 120;
            }
            else if (labelMessage.length() > 30) {
     
                popUpWindowWidth = (popUpWindowWidth * 2) - 200;
                buttonXPosition = (buttonXPosition * 2) - 60;
            }
     
     
            errorNotificationLabel = labelMessage;
        }
     
        public void createPopUpFrame() {
     
            this.setDefaultCloseOperation(PopUpFrame.EXIT_ON_CLOSE);
            this.setComponents(this.getContentPane());
            this.setTitle(errorNotificationFrameTitle);
            this.setIconImage(icon);
            this.setSize(popUpWindowWidth, 100);
            this.setLocationRelativeTo(null);
            this.setResizable(false);
            this.setVisible(true);
        }
     
    }

    and i have a simple main class that extends the JFrame class
    public class NewMain {
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame();
     
            PopUpFrame p = new PopUpFrame();
     
            f.setSize(400, 500);
            f.setVisible(true);
            f.setLocationRelativeTo(null);
     
            p.createPopUpFrame();
     
        }
    }

    my problem here is.. when i run the main class, the popUpFrame object will pop up..

    but the problem is it DOESNT ACT like THE JOptionPane...

    when i press the exit button.. all the frames are exiting.... how can i resolve this one...


    i need this customize frame for my program...


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: frame are exiting at the same time

    I believe calling System.exit terminates everything.

  3. The Following User Says Thank You to Shambolic For This Useful Post:

    chronoz13 (January 25th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: frame are exiting at the same time

    tnx dude but i got the error... just a little stupidity i didnt notice this part

    this.setDefaultCloseOperation(PopUpFrame.EXIT_ON_CLOSE);

    damn.... im getting frustrated .. because i need to finish this ASAP.. thats why im not able to notice some of
    my codings... darn... i dont even know how to use debugger until now.....

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: frame are exiting at the same time

    now i want to ask how to.. make this.. please help...

    i want to make an action for this button... that if i press ok it will just act like a JOption button
    that will JUST EXIT the DIALOG not the whole program...

    this is only the comman i know how to terminate
    System.exit(0)

    but if i use this.. all the windows that are running will terminate..

          Action closeAction = new AbstractAction() {
     
                public void actionPerformed(ActionEvent ae) {
     
                             // an action that will only command this button to exit ONLY itself not the whole windows that are running 
                }
            };
    Last edited by chronoz13; January 25th, 2010 at 06:42 AM.

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: frame are exiting at the same time

    frame.dispose();

    Where 'frame' is a reference to your JFrame. You can also set the default close operation to DISPOSE_ON_CLOSE, or if you wish to re-use the window just set its visibility to false (setVisible(false)).

  7. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (January 25th, 2010)

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: frame are exiting at the same time

    damn! i love this forum!! THANKS SIR COPEG!!!! hahahaha
    Last edited by chronoz13; January 25th, 2010 at 10:36 AM.

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: frame are exiting at the same time

    last problem.. i tried to deal with this one.. but i cant figure out the flow ..

    the PopUp Class
    public class PopUpFrame {
     
        private JFrame f;
        private JPanel popUpPanel;
     
        private JLabel errorMessage;
     
        private Image icon;
     
        private ImageIcon background;
     
        private JButton okButton;
     
        private String errorNotificationFrameTitle;
        private String errorNotificationLabel;
     
        private int popUpWindowWidth;
        private int buttonXPosition;
     
        public PopUpFrame() {
     
            f = new JFrame();
            icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
            background = new ImageIcon("c:/pics/mainbck2.jpg");
            okButton = new JButton("Ok");
            errorMessage = new JLabel();
            errorNotificationFrameTitle = "";
            errorNotificationLabel = ""; 
            popUpWindowWidth = 300;
            buttonXPosition = 110;
        }
     
        public void setComponents(Container cont) {
     
            popUpPanel = new JPanel() {
     
                @Override
                public void paintComponent(Graphics g) {
     
                    // Scale image to size of component
                    Dimension d = getSize();
     
                    g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
     
                    super.paintComponent(g);
                }
            };
            popUpPanel.setOpaque(false);
     
            // set this panel's layout to null for absolute positioning of
            // components
            popUpPanel.setLayout(null);
     
            // set the properties and actions of this button
            Action closeAction = new AbstractAction() {
     
                public void actionPerformed(ActionEvent ae) {
     
                    f.dispose();
     
     
                }
            };
            okButton.addActionListener(closeAction);
            okButton.setBounds(buttonXPosition, 35, 75, 20);
            okButton.setFont(new Font("Monospaced", Font.BOLD, 11));
            okButton.setBackground(Color.LIGHT_GRAY);
            okButton.setMnemonic(KeyEvent.VK_ENTER); // UNSUPPORTED Hot Key
     
     
            // set the properties of this label
            errorMessage.setText(errorNotificationLabel);
            errorMessage.setBounds(20, 5, 500, 30);
            errorMessage.setFont(new Font("SansSerif", Font.BOLD, 11));
     
            popUpPanel.add(errorMessage);
            popUpPanel.add(okButton);
            // add the panel to the container
            cont.add(popUpPanel);
        }
     
        /**
         * Sets the error title for this pop-up frame.
         * 
         * @param frameTitle title for the pop-up frame
         */
        public void setErrorNotificationFrameTitle(String frameTitle) {
     
            errorNotificationFrameTitle = frameTitle;
        }
     
        /**
         * Sets the error label message for this pop-up frame.
         *
         * If the message is too long, extend the width of the pop-up frame, and
         * make the button in the center by calculating its x coordinate according
         * to the width of the frame.
         * 
         * @param labelMessage error message for this pop-up frame
         */
        public void setErrorNotificationLabel(String labelMessage) {
     
     
            if (labelMessage.length() > 50) {
     
                popUpWindowWidth = popUpWindowWidth + 300;
                buttonXPosition = buttonXPosition + 150;
            }
            else if (labelMessage.length() > 40) {
     
                popUpWindowWidth = popUpWindowWidth + 250;
                buttonXPosition = buttonXPosition + 120;
            }
            else if (labelMessage.length() > 30) {
     
                popUpWindowWidth = (popUpWindowWidth * 2) - 200;
                buttonXPosition = (buttonXPosition * 2) - 60;
            }
     
            errorNotificationLabel = labelMessage;
        }
     
        public void createPopUpFrame() {
     
            setComponents(f.getContentPane());
            f.setTitle(errorNotificationFrameTitle);
            f.setIconImage(icon);
            f.setSize(popUpWindowWidth, 100);
            f.setFocusableWindowState(false);
            f.setLocationRelativeTo(null);
            f.setResizable(false);
            f.setVisible(true);
        }
     
        public static void popTheMessage() {
     
            SwingUtilities.invokeLater(new Runnable() {
     
                PopUpFrame pU = new PopUpFrame();
     
                public void run() {
     
                    pU.createPopUpFrame();
                }
            });
        }
    }

    main
    public class NewMain {
     
        public static void main(String[] args) {
     
            PopUpFrame pU = new PopUpFrame();
     
            pU.setErrorNotificationFrameTitle("Error Title");
            pU.setErrorNotificationLabel("Label");
     
            PopUpFrame.popTheMessage();
        }
    }

    i want this class to be as flexible as possible.. but i cant figure out why is that i cant generate the title and the label?

    i tried to put a string label and a string title in the PopUp class.. and it runs perfectly.. but when i use the methods .setErrorNotificationFrameTitle(String frameTitle); and setErrorNotificationLabel(String labelMessage); it doesnt generate the desired output...

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: frame are exiting at the same time

    A quick look-over and it looks like you're just setting a string inside the class, not the actual title of the frame. Use setTitle() to make sure the title gets set.
    frame.setTitle("My Frame");

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (January 25th, 2010)

  12. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: frame are exiting at the same time

    im still getting the same error...

Similar Threads

  1. how to set my frame in the middle of the screen
    By chronoz13 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 10th, 2012, 08:13 AM
  2. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  3. Unmovable frame (window) -- setFocus
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 7th, 2009, 04:00 AM
  4. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM
  5. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM