PDA

View Full Version : Needing Help in my code


NeedHelp11
02-20-2009, 11:51 AM
Hi everybody :),

I wrote this code to read data from a file and then display it in textfield in GUI.
But the problem was in displaying the GUI. It is run the application first and then GUI appear with last number that read from the file.
I want the GUI appear first and then the application read from the file and update the values until it finished.

This is the code
import java.awt.BorderLayout;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JLabel jLabel1;
public static JTextField text;
private static String s;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
File file = new File("C:\\MyFile.txt");

DataInputStream dis = null;

try {

dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
//System.out.println(dis.available());
if(dis.available() != 0){
while (dis.available() != 0) {
try{
s = dis.readLine();
//System.out.println(s);
text.setText(s);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText("");


}

}


else {
text.setText("0.0");


}
dis.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}


}
});
}

public NewJFrame() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("The current value of R");
jLabel1.setBounds(27, 81, 155, 31);
}
{

text = new JTextField();
getContentPane().add(text);
text.setBounds(166, 85, 112, 21);
text.setText("");




}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}

}




Thanks in Advance for helping

brad211987
02-20-2009, 01:14 PM
Move you data collection code into a seperate method, then call it in your constructor after your initGUI call. You will also need to move your setText call so that it is done after the data collection as well. Your main method could look something like this:


public static void main(String[] args)
{
new NewJFrame();
}


And you constructor something similar to:


public NewJFrame()
{
super();
initGUI();
collectData();
}

NeedHelp11
02-22-2009, 12:41 PM
It is worked
but I want the program to read from the file while it is running.
I want the value in the textfield to change (updated) while the program is running
like if we have data that change and to be update each time.
The program now is reading from the file and display the last number in text file.

I hope you understand me.

NeedHelp11
02-28-2009, 05:34 PM
LoooL !!!

Noboday has solution .. ??