#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Launch a JFrame from main ?


    I am attempting to launch a JFrame from my main.java.

    The only way I know how is to in my JFrame1 class is to enter
    Code:
    public class JFrame1 extends JPanel.


    BUT, that negates what I need to run the frame, which is:
    Code:
    public class JFrame1 extends javax.swing.JFrame


    Any ideas and or insight would be greatly appreciated.

    Thanks,
    Danecom

    Code:
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
         public static void main(String[] args) throws FileNotFoundException {
            // TODO code application logic here
             JFrame frame = new JFrame ();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
             JFrame1 panel = new JFrame1();
             frame.getContentPane().add(panel);
     
             frame.pack();
             frame.setVisible(true);
         }
    }


    Code:
    public class JFrame1 extends javax.swing.JFrame {
     
        /** Creates new form JFrame1 */
        public JFrame1() {
            initComponents();
        }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    attempting to launch a JFrame
    Does "launch" mean to create a new instance, add some components to it and to set it visible?

    Your code labeled "original code" looks like it does just that.

    Can you explain what you are trying to do that is not being done in that code?

    You second code section looks like you would try to add a JFrame to a JFrame. (where JFrame1 extends JFrame)
    Code:
    JFrame1 panel = new JFrame1(); 
    frame.getContentPane().add(panel);
  4. #3
  5. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    OUT-OF-POINT:

    Swing components are not thread safe you should run swing threads, in event dispatching thread not in main thread of your application...Because all events in swing are handled on event dispatching thread not in main thread, running swing thread on main thread can cause potential deadlock...

    Refer this Article

    i Agree what @Nomr posted
    Originally Posted by NormR
    You second code section looks like you would try to add a JFrame to a JFrame. (where JFrame1 extends JFrame)
    try extending JFrame1 to JPanel in that case

    Comments on this post

    • danecom agrees
    no one can become perfect by merely ceasing the act
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    import javax.swing.*;
    import java.util.Scanner;

    public class ClassProject {

    public static void main (String[] args){
    JFrame theGUI = new JFrame();
    Scanner reader = new Scanner(System.in);

    theGUI.setSize(200, 300);
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theGUI.setTitle("GUI Window");
    theGUI.setVisible(true);

    }

    }
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    Please correct me if I'm wrong
    It'd be faster and more complete if you compiled the code and let the compiler tell you if the code is ok. And then execute it and see if there are any problems and if it does what you want it to do.
    When building a program, a good technique is to make small changes, compile it, execute it and see if everything is ok before adding more changes.
    Take what you have, wrap it up to a complete program and give it a go. Then add more.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    Originally Posted by NormR
    It'd be faster and more complete if you compiled the code and let the compiler tell you if the code is ok. And then execute it and see if there are any problems and if it does what you want it to do.
    When building a program, a good technique is to make small changes, compile it, execute it and see if everything is ok before adding more changes.
    Take what you have, wrap it up to a complete program and give it a go. Then add more.
    What are you even talking about.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    What are you even talking about.
    Writing programs.

IMN logo majestic logo threadwatch logo seochat tools logo