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 7 of 7

Thread: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    Im trying to write a painfully simple program in java that involves instantiating the class that holds my JFrame as a new object.

    Here is the class: Main() that is supposed to instantiate the class that contains this window
    __________________________________________________ _________________________________________
    package TextEditor2;
     
     
    public class Main {
     
        public static void main(String[] args) {
            // TODO code application logic here
     
            System.out.println("amendment to be");
            TextEditor2.NewJFrame njf = new TextEditor2.NewJFrame();
     
        }
     
    }
    _____
    __________________________________________________ ____________________________________
    I have also tried creating the new object without accessing it as an extension of it's package name
    NewJFrame njf = new NewJFrame();
    NewJFrame() is simply the code generated from NetBeans' JFrame form.

    __________________________________________________ ________________________________________
    In both these cases the compiler produces the following output.
    ----------------------------------------------------------------------------------------------------------------------------------------

    > Task :compileJava UP-TO-DATE
    > Task :processResources NO-SOURCE
    > Task :classes UP-TO-DATE

    > Task :runSingle
    amendment to be
    ----------------------------------------------------------------------------------------------------------------------------------------

    The compiler then proceeds to stall indefinitely without rendering the class:NewJFrame() which compiles independently without error.
    __________________________________________________ _________________________________________

    Im aware that there is probably a painfully simple way to fix this but It's difficult to track down reliable sources for programming explanations.

    Would someone please tell me what to do?
    Last edited by TheTortusEatsItsOwnTail; April 18th, 2021 at 09:18 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    What happens when you try to compile and execute the program?
    Please copy the full text of any error messages and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    What environment are you working in? What programs are you using to compile and execute your code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    As I mentioned earlier the compiler produces the following output.
    ----------------------------------------------------------------------------------------------------------------------------------------

    > Task :compileJava UP-TO-DATE
    > Task :processResources NO-SOURCE
    > Task :classes UP-TO-DATE

    > Task :runSingle
    amendment to be

    At that point compilation stalls indefinitely and simply does not render the JFrame.

    My Development Environment is NetBeans IDE.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    Sorry, I have never seen that output from the javac compiler program. What program/app produces that output?
    I have only worked with the javac compiler.

    What is the code in the NewJFrame class? What is the code in that class's constructor expected to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    The code in the constructor for new JFrame is expected to initialize the objects to their default values
    in the case of java swing those objects are known as components, they are models that are rendered into views.

    Here is the code genreated by NetBeans IDE


     
     
    public class NewJFrame extends javax.swing.JFrame {
     
        /**
         * Creates new form NewJFrame
         */
        public NewJFrame() {
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextPane1 = new javax.swing.JTextPane();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jScrollBar1 = new javax.swing.JScrollBar();
            jSlider1 = new javax.swing.JSlider();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jTextPane1.setBackground(new java.awt.Color(153, 153, 153));
            jTextPane1.setCaretColor(new java.awt.Color(153, 255, 51));
            jScrollPane1.setViewportView(jTextPane1);
     
            jButton1.setIcon(new javax.swing.ImageIcon("AbsolutePathname")); // NOI18N
            jButton1.setText("jButton1");
     
            jButton2.setIcon(new javax.swing.ImageIcon("AbsolutePathname")); // NOI18N
            jButton2.setText("jButton2");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGap(129, 129, 129))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 1543, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1484, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(31, 31, 31)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        }                                        
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JScrollBar jScrollBar1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JSlider jSlider1;
        private javax.swing.JTextPane jTextPane1;
        // End of variables declaration                   
    }

    Again this is literally just the code that NetBeansIDE generates when a user constructs a JFrameForm.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    The JFrame needs for the setVisible method to be called so that it is displayed on the screen.
    The NewJFrame class has a main method that calls the setVisible method. If you were to start execution of the NewJFrame class, its main method would be called and the class would display on the screen. The NewJFrame class's constructor does not call the setVisible method.
    Your code in the Main class's main method needs to call the setVisible method for the instance of the NewJFrame class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Java Programmer Needs Help Understanding What He Did Wrong With His Instantiation.

    Thank you so much for the help, your help fixed the issue.

Similar Threads

  1. Replies: 1
    Last Post: September 25th, 2013, 06:11 PM
  2. Replies: 7
    Last Post: August 17th, 2013, 07:55 PM
  3. Need Help Understanding Where I went wrong
    By basedoverlord12 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 1st, 2011, 06:40 PM
  4. ActionServlet Instantiation
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 06:12 AM
  5. Instantiation from within Methods
    By John Davies in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 04:39 PM