aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with assignment and JFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with assignment and JFileChooser" Watch "Help with assignment and JFileChooser" New topic
Author

Help with assignment and JFileChooser

Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
I have an upcoming assignment Here is the List of things to accomplish which is difficult to understand in the first place.

Here is one way to say it.

have two buttons in the SOUTH frame. One says Get Student Names, which when clicked will read in the Student Names File and display the lines in the textArea; and one that says "Get Student Grades" that when clicked will display the student Id, names, and average grade in the text area. Include the student object from P_Supplemental_9 with an arraylist for the grades.

Requirements:
1 frame with BorderLayout
2 buttons on Panel
1 Panel with GridLayout to put the two buttons on in the SOUTH region
1 FileChooser in a new frame
1 TextArea in the CENTRAL region
1 Label telling what the program does in the NORTH region



here is another

for P_Supplemental_11. When the first button is pressed, it should open up the FileChooser that's explained on page 329 in the book. With this, choose the studentNames.txt file and read it into the JTextArea.
When the second button is pressed, it should have you browse to the studentScores.txt file, import it, do the magic, and display the same output as P_Supplemental_9 in the JTextArea.


Here is my entire code:






So my first question is how do I get The two buttons to display next to eachother in the SOUTH of the frame in the BorderLayout


And second how do attach the JFile chooser to the buttons?






Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

So my first question is how do I get The two buttons to display next to eachother in the SOUTH of the frame in the BorderLayout

You already have the answer to that one here.

And second how do attach the JFile chooser to the buttons?

Read How to Use File Choosers, there are some great examples that suit your needs.


The quieter you are, the more you are able to hear.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Austin Trower wrote:
1 FileChooser in a new frame

Are you sure you got this one right?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19


Austin Trower wrote:
1 FileChooser in a new frame


Are you sure you got this one right?


No that why I asked the Question how do I set that to open on a button click and why it is currently commented out
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
You already have the answer to that one here



Im glad to see Im not the only one in my class having problems Thanks for the links
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
K first problem solved
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
My next Question is how do i get the JFileChooser to display the selected files text inside the original GUI box instead of to the Console?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

If you've read the tutorial I gave you on how to use JFileChooser (and seems like you did read some of it), then you know how to choose a file, open it and read its content (that code you already have).

The only thing you have to do now is to replace the code that prints lines to the console:

with appropriate method that would append each line to JTextArea in your GUI. Check the API for JTextArea#append(String str) method and that would solve your problem.
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
All right got that done i will have to work on making the out put fit in the frame better but got it to print inside the GUI



Now Im going to need advice on this; When the second button is pressed, it should have you browse to the studentScores.txt file, import it, do the magic, and display the same output as P_Supplemental_9 in the JTextArea.

Instructor also clarified thusly; When the first file is read in, you will want to not just display it but add it to the student arraylist at that time. When the second file is read in, you will run the collect grades method (for each student) and add that to the corresponding student object.

This kind of splits up the code from P_Supplemental_9 but should help you understand the differences between working with the console and the GUI.

Seeing as how I don't know any magic I cant even pull my thumb off properly. Im going to keep reading but If their is any advice you guys could give to keep nudging me in the right direction it would be appreciated.




These are the baby steps I have taken so far Thanks Kemal

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

And what is P_Supplemental_9?

From the description you gave, you should extend the code you already have. Since I don't know what are the formats of files you are reading, I will make an assumption.

Suppose the first file (containing data about students) is formatted as:

In this case your Student class should have these three fields. You would read the file just as you did in the code you already have, but in addition you would have to parse each line you read to extract values for each of these fields (see java.util.StringTokenizer). Once you have extracted all three values from the current line you read, you would instantiate a Student class with those values.

Until you provide more details about how files are structured, I can't give you any more hints, but what I wrote above would be some basic idea on how to handle that task.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Surely you would use String#split rather than a Tokenizer, which is legacy code.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Surely you would use String#split rather than a Tokenizer, which is legacy code.

I missed that, what was I thinking?
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
Sorry I have been away from a computer for a while I hope you guys are out there i will try and clarify, and take your input that you already given into consideration this is P_Supplemental_9







What i think i need to do is clickmy first button in my GUI that pops up and look up this file which is in a notepad format that has student names and ID numbers

3305 Smith Henry
5555 Eddy Olivia
8915 Johnson Luke








then i need the code to do the afore mention "magic" so When I click the other button in my GUI and select the file that looks like this:

3305 92.0
5555 95.5
8915 98.5
3305 89.0
5555 90.5
8915 95.5
3305 78.5
5555 85.0
8915 82.0

and is list in the above code here



It sort of adds the two files together and prints out the student names and Average grades which is what the code in P_Supplemental_9 does, I just need to make it so it does the same action by clicking the two buttons in the GUI from the code that i originally posted. So in the end the print out looks like this.
3305 Smith Henry 86.50 avg,
5555 Eddy Olivia 90.33 avg,
8915 Johnson Luke 92.00 avg,

Inside the GUI text box

I hope that makes some sense.



Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
How do i change this so it is all a String?





all night
Austin Trower
Greenhorn

Joined: Nov 06, 2012
Posts: 19
Where and how do I use StringBuilder in my code? I know that that is what I need to do and I think it is the last step. here is where i need to use it for the jtxtAfileContents.setText line



and I know it it needs to be set like this



but I just dont know where or how to set the method for it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help with assignment and JFileChooser