NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Hello
My names Harry, and im a beginner in Java programming, i have taken a few courses so know some of the terms and lingo and how to build a basic program.
i am currently working on a GUI app, which is why im here, i have managed to make this App button less, so that the information i want to display just does, without having to do anything apart from run it, the information is displayed in 'JTextFields' ontop of a 'JLabel' there are 20 so TextFields, each of these display a line of writing, however the lines of writing are in seperate notepad files, i would like to code it so that it will read the first line of a notepad and then put it in a textfield and then read the 2nd line of writing and display it in a different textfield, and the same for the other 18 or so. i am working my way up to being able to take information from a CSV(comma seperated file).
Is there a way to display information in the same CSV file in different textfields, and if these numbers get changed, how would i go about making it automatically update?
Any help/Soruce code etc., would be very helpful.
================================================== ==============================
Example of my code.(if this helps)
================================================== ==============================
This part of my code creates and puts the textfield in a certain location, and then goes into "MyFile" to read the information and display it into the "TxtCardiff" JTextField:(This works fine, and i have this same code repeated 20 times, and have 20 different notepad files, i need to put all information into one file, and still display it into the different textfields!)
Code Java:
txtCardiff = new JTextField();
try {
FileReader MyFileReader = new FileReader(
"MyFile");
int character = MyFileReader.read();
while (character != -1) {
output += (char) character;
character = MyFileReader.read();
}
MyFileReader.close();
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
txtCardiff.setText(output);
txtCardiff.setText(output);
txtCardiff.setBackground(Color.GREEN);
txtCardiff.setBounds(305, 699, 102, 20);
frame.getContentPane().add(txtCardiff);
txtCardiff.setColumns(10);
================================================== ==============================
Regards,
Harry
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Quote:
display information in the same CSV file in different textfields
There is no restriction on how many different textfields that Strings can be added to.
Quote:
if these numbers get changed, how would i go about making it automatically update?
There are listeners that can be added to the textfields that can be called when there are changes made.
The listeners can do the update.
Please use normal text for posting questions. The ALL caps and large font makes it hard to read the post.
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Sorry about the large text, didnt realise it would be that big.
do you know what listeners i would be using because i have looked into it and the only one i have come across is when you select the textfield and hit the enter key.
i dont think i made myself clear,
i wanted help with how to display single lines of text in different textfeilds from a csv with all the infromation in it.
E.g.
-----------------------------------
CSVfile
cardiff:234567
bristol:546734
manchester:458675
-----------------------------------
and then display each of these locations into 3 different textfields.
Regards
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Quote:
how to display single lines of text in different textfeilds
I don't understand what the problem is. A String can be added to any number of textfields. There are no restrictions.
pseudo code:
read string
add string to tf1
add string to tf2
add string to tf3
etc
or when the strings are to be round robined into different tf:
begin loop
read string
add string to next tf
end loop
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Yes i know there are no restrictions.
but how do i take the strings from the CSV file, and display them in seperate TextFields?
each textfield is going to have different information in it,
i know what to do just not how:
go to file
read first line
display line in tf1
read 2nd line
display line2 in tf2
etc....
but i dont know how to code this
this also needs to be looped every 10-20 seconds to make sure nothing has changed.
Does that make it clear?
sorry im new to this forum stuff.
Regards,
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Quote:
go to file
read first line
display line in tf1
read 2nd line
display line2 in tf2
etc....
but i dont know how to code this
Which of the steps are you having problems with?
How do you decide which tf to add a String to? Can the tf be referenced from an array?
Inside a loop the Strings could be read from the file and added to the next tf from the array.
Quote:
make sure nothing has changed.
Where are the changes being made? To the file's contents or to the textfields?
You can use listeners to detect changes.
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
I am having problems taking data from a CSV file and then
outputting the different lines of information into different textfields
i dont know how to decide it or if it can be referenced thats why i put this question up, so that i can to find out:
- How to extract data from a csv file and put each line of the file into a different TextField.
And changes will be made to the Files contents. and when this happens i want the textfield to update.
i know i can use listeners to do all sorts of things, but i dont know which listeners i need.
Regards,
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Let's work on the problems one at a time. Start with this one:
Quote:
How to extract data from a csv file
The Scanner class is easy to use for reading lines from a file. Define a Scanner object to read from the file.
Write a loop to read lines from the file and print them out.
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
I was able to do this with a single file that had multiple lines of code and display them in one place, however i dont know how to take different lines of this file and display them in different locations.
By the way, thank you for all your help
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Quote:
how to take different lines of this file and display them in different locations.
What determines what location a line is to be displayed in? See post#4 for some considerations.
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
So i will have a Textfield on a Map on the location of Cardiff, the information in the textfield will then say Cardiff:5463965, and one for Bristol, one for london, one for manchester etc,
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
What determines what location a line is to be displayed in?
When a line is read from the file, how will the program decide which tf to add the line to? Is there something in the line that will determine that?
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
I dont have anything to determine this, the only thing that i can use to determine this will be a comma,
Or i could use the fact that each line will have a different location on it, would that work?
Is there a way to make it so the program will look for say the word 'Manchester', and then display the line with the word 'Manchester' on it to a particular tf ?
Re: NEED TO EXTRACT FROM CSV FILE PUT INTO JTEXTFIELDS (JAVA SWING GUI)
Quote:
the only thing that i can use to determine this will be a comma,
If every line has one or more commas, how can a comma be used to select the tf to put the line in?
Quote:
look for say the word 'Manchester',
Look at the methods in the String class. There are some that can be used to check if a String contains another String.
I don't