• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with the code........  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

i have been stuck with this prototype for 2 weeks.........i have 2 java files.....the first code is the interface code....and the second code is a code that reads the contents of a file.

the thing is ...i want to make the contents of the file appear in the TextBox of the interface.........i dont know how to do that..?......how i can link the 2 files together....how can i direct the file contents to appear in the TextBox in my interface........

HERE IS MY CODES..

1....INTERFACE CODE......

import javax.swing.*;
import javax.swing.border.*;

public class sanesense
{








public static void main (String[] args)
{





// creating the components
// Paragraph TextField + Question TextField + Answer TextField + ASK Button



JLabel Paragraph = new JLabel("Please Enter a Text..");
JTextArea ParagraphText = new JTextArea(6, 30);


ParagraphText.setEditable(true); // thi function disallow the user from changing the information in the text area.




JLabel Question = new JLabel("Your Question");
JTextArea QuestionText = new JTextArea(2,30);



JLabel Answer = new JLabel("The Answer");
JTextArea AnswerText = new JTextArea(5,30);

JButton button = new JButton ("ASK");
button.setLocation(100,200);








// creating the panel

JPanel panel = new JPanel();
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "");
panel.setBorder(titled);


panel.add(Paragraph);
panel.add(ParagraphText);
panel.add(Question);
panel.add(QuestionText);
panel.add(Answer);
panel.add(AnswerText);
panel.add(button);



// creating the frame


JFrame frame = new JFrame();
frame.setTitle("THE SANESENSE PROJECT");
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,450);
frame.setResizable(true);


frame.setVisible(true);
frame.show();

}
}






2...CODE TO READ FROM A FILE....

import java.io.*;

class FileReadTest {



public static void main (String[] args)
{
FileReadTest t = new FileReadTest();
t.readMyFile();
}




void readMyFile()
{

String record = null;
int recCount = 0;

try {

FileReader fr = new FileReader("mydata");
BufferedReader br = new BufferedReader(fr);

record = new String();
while ((record = br.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}

} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("an IOException error!");
e.printStackTrace();
}

}

}


i will be very grateful for any comments or help.........even my bect coffee didnt help me to figure this out


Thank you very much

 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, your readMyFile() method should probably return a String. Then within sanesense, you just create an instance of FileReadTest and call that method in conjunction with setText...

FileReadTest t = new FileReadTest();
QuestionText.setText(t.readMyFile());

However, your readMyFile() method has a couple of other problems. First, you'll need a file extension on "mydata." Something like "mydata.dat" or "mydata.txt". Second, you'll need to modify the while loop so that the lines you're reading are actually added to the String that you're going to return after you're done reading lines. (You might consider a StringBuffer for this purpose.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!