Results 1 to 7 of 7
  1. #1
    Foxtrot is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default Getting code to run from the beginning in a jFrame form

    Hi guys I have a pretty urgent question.

    I am using the latest Netbeans and I need some code to run as a certain jFrame has started so that it fills pre-determined areas of it with data from a text file. The problem is that you get a whole lot of static - non static errors when you try run the code without the use of a button.

    Heres the code for the jFrame without the non-editable bits:
    Java Code:
    package Main;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    
    public class Question1 extends javax.swing.JFrame {
    
        /** Creates new form Question1 */
        public Question1() {
            initComponents();
        }
    
        Student stu = new Student();
    
        /** 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")
    
    
    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
            new Question2().setVisible(true);
            this.dispose();
        }                                       
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Question1().setVisible(true);
                }
            });
    
        }
        
        
    
        // Variables declaration - do not modify
        private javax.swing.JButton btnNext;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JInternalFrame jInternalFrame1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JLabel picture;
        private javax.swing.JRadioButton txtAnswer1;
        private javax.swing.JRadioButton txtAnswer2;
        private javax.swing.JRadioButton txtAnswer3;
        private javax.swing.JRadioButton txtAnswer4;
        private javax.swing.JTextArea txtQuestion;
        // End of variables declaration
    
    }
    and here is the code I need to run when it starts
    Java Code:
    try
            {
           
                Scanner input = new Scanner(new FileInputStream("Teacher.txt"));
                while(input.hasNext())
                {
                    line = input.nextLine();
    
                    Scanner del = new Scanner(line).useDelimiter(";");
                    username = del.next();
                    password = del.next();
    
                   //New object
                }
                input.close();
    
                txtUsername.setText(username); //Etc, etc
    
            }
            catch(IOException exc)
            {
                JOptionPane.showMessageDialog(null,"Error Connecting to File");
            }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Foxtrot is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    the problem is when I have to use the global varibles such as that of text boxes etc. it gives me an error saying you cannot reference a non-static varible from a static context

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Foxtrot is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    ok will do

  6. #6
    vetri03 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: Getting code to run from the beginning in a jFrame form

    'm a beginner. i need some basic codings in netbeans(esp in jframes) and jdbc connection to sql. i know nothing, so please guide me

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

    Default Re: Getting code to run from the beginning in a jFrame form

    Quote Originally Posted by vetri03 View Post
    'm a beginner. i need some basic codings in netbeans(esp in jframes) and jdbc connection to sql. i know nothing, so please guide me
    Please don't hijack someone else's question for your completely unrelated question. If it is important, ask it again in a new thread. Go to the new to Java section and click on New Thread. Also you'll want to ask an answerable specific question by telling us the specifics of just what confuses you. Based on what you've posted above, about all anyone can do is to suggest that you study some of the tutorials.

    This old thread will be locked. Again, I invite you to start a new thread for your question.

Similar Threads

  1. opening a Jframe form in a top of a new form
    By Nubkadiya in forum New To Java
    Replies: 7
    Last Post: 06-01-2010, 09:21 PM
  2. Replies: 1
    Last Post: 05-27-2010, 10:48 PM
  3. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 07:53 AM
  4. Jtree link to JFrame Form
    By daniel50096230 in forum NetBeans
    Replies: 4
    Last Post: 09-16-2008, 07:53 AM
  5. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •