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:
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
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");
}
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
Re: Getting code to run from the beginning in a jFrame form
Quote:
Originally Posted by
vetri03
'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.