start a new JFrame from an existing one, and when old JFrame closes new one does not

Discussion in 'Java' started by jakester, Apr 2, 2007.

  1. jakester

    jakester Guest

    I have the need to create a new JFrame from an existing one. The code
    below show how I am accomplishing this task. However, when the
    original JFrame closes, all JFrames created from the original JFrame
    closes. Could someone please help me how to create a new JFrame so
    that it runs outside the thread of the original? Thanks.

    public class MyGuiForm extends JFrame implements ActionListener {

    private JButton _btnNew;

    public MyGuiForm() {
    _btnNew = new JButton("New");
    _btnNew.addActionListener(this);

    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(_btnNew);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    this.show();
    }

    public void actionPerformed(ActionEvent ae) {
    Object source = ae.getSource();
    if(null == source) return;

    if(_btnNew == source) {
    new MyGuiForm();
    }
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    new MyGuiForm();
    }
    });
    }
    }
    jakester, Apr 2, 2007
    #1
    1. Advertising

  2. Re: start a new JFrame from an existing one, and when old JFramecloses new one does not

    jakester wrote:
    > I have the need to create a new JFrame from an existing one. The code
    > below show how I am accomplishing this task. However, when the
    > original JFrame closes, all JFrames created from the original JFrame
    > closes. Could someone please help me how to create a new JFrame so
    > that it runs outside the thread of the original? Thanks.
    >
    > public class MyGuiForm extends JFrame implements ActionListener {
    >
    > private JButton _btnNew;
    >
    > public MyGuiForm() {
    > _btnNew = new JButton("New");
    > _btnNew.addActionListener(this);
    >
    > this.getContentPane().setLayout(new BorderLayout());
    > this.getContentPane().add(_btnNew);
    >
    > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > this.pack();
    > this.show();
    > }
    >
    > public void actionPerformed(ActionEvent ae) {
    > Object source = ae.getSource();
    > if(null == source) return;
    >
    > if(_btnNew == source) {
    > new MyGuiForm();
    > }
    > }
    >
    > /**
    > * @param args
    > */
    > public static void main(String[] args) {
    > SwingUtilities.invokeLater(new Runnable() {
    > public void run() {
    > JFrame.setDefaultLookAndFeelDecorated(true);
    > new MyGuiForm();
    > }
    > });
    > }
    > }
    >


    Your code is very difficult to read without any indentation.

    You tell your program to exit when this frame is closed with this line:

    > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    Take that line out and it won't do that any more. Also you don't need
    all of the 'this.' in front of methods that are part of 'this'.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 2, 2007
    #2
    1. Advertising

  3. jakester

    SadRed Guest

    On Apr 3, 7:29 am, "jakester" <> wrote:
    > I have the need to create a new JFrame from an existing one. The code
    > below show how I am accomplishing this task. However, when the
    > original JFrame closes, all JFrames created from the original JFrame
    > closes. Could someone please help me how to create a new JFrame so
    > that it runs outside the thread of the original? Thanks.
    >
    > public class MyGuiForm extends JFrame implements ActionListener {
    >
    > private JButton _btnNew;
    >
    > public MyGuiForm() {
    > _btnNew = new JButton("New");
    > _btnNew.addActionListener(this);
    >
    > this.getContentPane().setLayout(new BorderLayout());
    > this.getContentPane().add(_btnNew);
    >
    > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > this.pack();
    > this.show();
    >
    > }
    >
    > public void actionPerformed(ActionEvent ae) {
    > Object source = ae.getSource();
    > if(null == source) return;
    >
    > if(_btnNew == source) {
    > new MyGuiForm();
    >
    > }
    > }
    >
    > /**
    > * @param args
    > */
    > public static void main(String[] args) {
    > SwingUtilities.invokeLater(new Runnable() {
    > public void run() {
    > JFrame.setDefaultLookAndFeelDecorated(true);
    > new MyGuiForm();
    >
    > }
    > });
    > }
    > }


    > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Use other value. See API doc.
    SadRed, Apr 2, 2007
    #3
  4. jakester

    jakester Guest

    If I comment out the code
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); then I get the
    intended behaviour. The problem is now the java thread does not
    terminate.

    I looked at the JavaDocs for the options on setDefaultCloseOperation
    and it turns out JFrame.DISPOSE_ON_CLOSE would probably help me out.
    So I uncommented the line above and did
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); and everything
    works!

    Thank you both.

    On Apr 2, 6:50 pm, Knute Johnson <>
    wrote:
    > jakester wrote:
    > > I have the need to create a new JFrame from an existing one. The code
    > > below show how I am accomplishing this task. However, when the
    > > original JFrame closes, all JFrames created from the original JFrame
    > > closes. Could someone please help me how to create a new JFrame so
    > > that it runs outside the thread of the original? Thanks.

    >
    > > public class MyGuiForm extends JFrame implements ActionListener {

    >
    > > private JButton _btnNew;

    >
    > > public MyGuiForm() {
    > > _btnNew = new JButton("New");
    > > _btnNew.addActionListener(this);

    >
    > > this.getContentPane().setLayout(new BorderLayout());
    > > this.getContentPane().add(_btnNew);

    >
    > > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    > > this.pack();
    > > this.show();
    > > }

    >
    > > public void actionPerformed(ActionEvent ae) {
    > > Object source = ae.getSource();
    > > if(null == source) return;

    >
    > > if(_btnNew == source) {
    > > new MyGuiForm();
    > > }
    > > }

    >
    > > /**
    > > * @param args
    > > */
    > > public static void main(String[] args) {
    > > SwingUtilities.invokeLater(new Runnable() {
    > > public void run() {
    > > JFrame.setDefaultLookAndFeelDecorated(true);
    > > new MyGuiForm();
    > > }
    > > });
    > > }
    > > }

    >
    > Your code is very difficult to read without any indentation.
    >
    > You tell your program to exit when this frame is closed with this line:
    >
    > > this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    >
    > Take that line out and it won't do that any more. Also you don't need
    > all of the 'this.' in front of methods that are part of 'this'.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
    jakester, Apr 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    4
    Views:
    3,661
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Nov 15, 2004
  2. Replies:
    4
    Views:
    2,600
    Oliver Wong
    Sep 29, 2005
  3. ChrisC
    Replies:
    11
    Views:
    2,904
    ChrisC
    Feb 7, 2007
  4. John Henry
    Replies:
    24
    Views:
    1,006
    alex23
    May 30, 2008
  5. Replies:
    2
    Views:
    409
    markspace
    Aug 27, 2012
Loading...

Share This Page