Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Fubarable

Thread: how to make new frame open on the click of a button??

  1. #1
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Question how to make new frame open on the click of a button??

    I did this,but i don't find it more efficient:-
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Swing implements ActionListener {
    
    JButton j,k;
    JLabel l;
    JFrame f,f1;
    public static void main(String args[]){
    
      Swing x = new Swing();
      x.go();
    
    
    }
    public void go(){
    
    f = new JFrame("first frame");
    j = new JButton("click me");
    
    f.getContentPane().add(j);
    
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    f.setSize(300,500);
    f.setVisible(true);
    j.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    f1 = new JFrame();
    k = new JButton("second frame");
    f1.getContentPane().add(k);
    f1.setSize(300,300);
    f1.setVisible(true);
    
    
    }
    }
    Last edited by Norm; 03-11-2012 at 01:16 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: how to make new frame open on the click of a button??

    Is there a question?

  3. #3
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: how to make new frame open on the click of a button??

    is there any other way of opening a new frame on a click beside this ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: how to make new frame open on the click of a button??

    What does doing it on a click have to do with Opening a new frame?

  5. #5
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: how to make new frame open on the click of a button??

    consider a desktop application 'e-diary' which has buttons on it's main window as phone book,notes,etc so when the user clicks on phone book button then a new window will appear giving the functions of adding and searching numbers.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: how to make new frame open on the click of a button??

    My question has to do with where the new frame is created and displayed.
    How is doing it in a listener different from any other place? The one consideration would be not to hold the GUI's thread (EDT) when doing it.
    Should the new frame be modal? Would a Dialog do the job better?
    adiotrox likes this.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: how to make new frame open on the click of a button??

    Then you must first create your main GUI and give it JButtons, and then in an ActionListener that is added to one of the JButtons, add code that would display a modal dialog such as a modal JDialog or JOptionPane (not another JFrame). The Swing tutorials will show you how to do most all of this, and you can find them here: Swing Tutorials
    adiotrox likes this.

Similar Threads

  1. How to open .ods file on button click event
    By Anagha in forum New To Java
    Replies: 15
    Last Post: 07-08-2012, 09:10 PM
  2. Replies: 6
    Last Post: 03-19-2011, 03:24 AM
  3. creating a frame on button click event
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-16-2011, 05:11 AM
  4. add tabbed pane to the frame on a button click
    By Mahaveer in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 09:19 AM
  5. How we can open a file with a click in button
    By kostinio in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 04:46 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
  •