Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Simple GUI help and cleanup

    Ok, so first off this code is insanely messy and very very long. So if anyone has any ideas on how I can reduce all the variables and make it just run nicer that would be nice.
    I have been trying many different types of layouts and saving forms and combined it has just clustered my code quite badly, so any suggestions in that area would help VERY much.

    What I need to help with in the source code is this.
    1. Need my Report label to display the text in an organized fashion
    2. Implement a scrollbar function to appear when the report list is longer than 3, excluding the title row.
    3. Make the buttons non expandable, I want them to stay a certain set size.
    4. (optional) Find a way to cut down on the amount of variables I have. If you suggest using "this", PLEASE PLEASE PLEASE explain, as that part of Java greatly

    For the scrollbar, I spent about 2 hours trying to figure it out, and finally just scraped it because I kept getting lost.
    I am not lazy and I do not give up, so when i ask for help, its because I honestly do not understand what I am doing, so all help is greatly appreciated.

    Here is the source code, I posted the three text files below as well.
    To use my program, only the Load and Report buttons work this far. Click LOAD, pick one of my files (I have been using the severalrecords.txt) and then click REPORT.
    I need the text label for report to nicely format the information, title row on the top, then each user's data displayed on the next line below.
    Once it hits 4 rows, I want the Jscrollbar feature to show up as well.

    Again, please EXPLAIN what you all do, because I want to learn.

    Source Code
    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.lang.reflect.Array;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    import javax.swing.*;
    
    public class ProjectEngine {
    
    	/*******************************************************************************************
    	 * @author Austin Anderson This program will display a Calculator
    	 *******************************************************************************************/
    
    	private static JButton buttonLoad;
    	private static JButton buttonReport;
    	private static JButton buttonMinMPG;
    	private static JButton buttonMaxMPG;
    	private static JButton buttonAvgMPG;
    	private static JLabel Fname1 = new JLabel("");
    	private static JLabel Lname1 = new JLabel("");
    	private static JLabel Fares1 = new JLabel("");
    	private static JLabel GasMoney1 = new JLabel("");
    	private static JLabel Miles1 = new JLabel("");
    	private static JLabel Gallons1 = new JLabel("");
    	private static JLabel MPG1 = new JLabel("");
    	private static JLabel Profit1 = new JLabel("");
    	private static JLabel Fname2 = new JLabel("");
    	private static JLabel Lname2 = new JLabel("");
    	private static JLabel Fares2 = new JLabel("");
    	private static JLabel GasMoney2 = new JLabel("");
    	private static JLabel Miles2 = new JLabel("");
    	private static JLabel Gallons2 = new JLabel("");
    	private static JLabel MPG2 = new JLabel("");
    	private static JLabel Profit2 = new JLabel("");
    	private static JLabel Fname3 = new JLabel("");
    	private static JLabel Lname3 = new JLabel("");
    	private static JLabel Fares3 = new JLabel("");
    	private static JLabel GasMoney3 = new JLabel("");
    	private static JLabel Miles3 = new JLabel("");
    	private static JLabel Gallons3 = new JLabel("");
    	private static JLabel MPG3 = new JLabel("");
    	private static JLabel Profit3 = new JLabel("");
    	private static JLabel Fname4 = new JLabel("");
    	private static JLabel Lname4 = new JLabel("");
    	private static JLabel Fares4 = new JLabel("");
    	private static JLabel GasMoney4 = new JLabel("");
    	private static JLabel Miles4 = new JLabel("");
    	private static JLabel Gallons4 = new JLabel("");
    	private static JLabel MPG4 = new JLabel("");
    	private static JLabel Profit4 = new JLabel("");
    	private static JLabel Fname5 = new JLabel("");
    	private static JLabel Lname5 = new JLabel("");
    	private static JLabel Fares5 = new JLabel("");
    	private static JLabel GasMoney5 = new JLabel("");
    	private static JLabel Miles5 = new JLabel("");
    	private static JLabel Gallons5 = new JLabel("");
    	private static JLabel MPG5 = new JLabel("");
    	private static JLabel Profit5 = new JLabel("");
    	private static JLabel Fname6 = new JLabel("");
    	private static JLabel Lname6 = new JLabel("");
    	private static JLabel Fares6 = new JLabel("");
    	private static JLabel GasMoney6 = new JLabel("");
    	private static JLabel Miles6 = new JLabel("");
    	private static JLabel Gallons6 = new JLabel("");
    	private static JLabel MPG6 = new JLabel("");
    	private static JLabel Profit6 = new JLabel("");
    	private static JLabel Fname7 = new JLabel("");
    	private static JLabel Lname7 = new JLabel("");
    	private static JLabel Fares7 = new JLabel("");
    	private static JLabel GasMoney7 = new JLabel("");
    	private static JLabel Miles7 = new JLabel("");
    	private static JLabel Gallons7 = new JLabel("");
    	private static JLabel MPG7 = new JLabel("");
    	private static JLabel Profit7 = new JLabel("");
    	private static JLabel Fname8 = new JLabel("");
    	private static JLabel Lname8 = new JLabel("");
    	private static JLabel Fares8 = new JLabel("");
    	private static JLabel GasMoney8 = new JLabel("");
    	private static JLabel Miles8 = new JLabel("");
    	private static JLabel Gallons8 = new JLabel("");
    	private static JLabel MPG8 = new JLabel("");
    	private static JLabel Profit8 = new JLabel("");
    	private static JLabel start_display = new JLabel("Please load data.");
    	private static JLabel report_display = new JLabel("No report data yet.");
    	private static JLabel min_display = new JLabel("No minimum data yet.");
    	private static JLabel max_display = new JLabel("No maximum data yet.");
    	private static JLabel avg_display = new JLabel("No average data yet.");
    	static JFrame frame = new JFrame("Welcome!");
    	static JPanel panel_startup = new JPanel();
    	static JPanel panel_report = new JPanel();
    	static JPanel panel_MinMPG = new JPanel();
    	static JPanel panel_MaxMPG = new JPanel();
    	static JPanel panel_AvgMPG = new JPanel();
    	static JPanel panel_top = new JPanel();
    	static JPanel panel_middle = new JPanel();
    	static JPanel panel_bottom = new JPanel();
    	static JPanel panel_main = new JPanel();
    	static JPanel panel_upper = new JPanel();
    	static Array[] al;
    	static String report = "";
    	static double mpg1, mpg2, mpg3;
    	static double gal1, gal2, gal3;
    	static double prof1, prof2, prof3;
    	static double mil1, mil2, mil3;
    	static double gasM1, gasM2, gasM3;
    	static double far1, far2, far3;
    
    	CardLayout ourLayout;
    
    	JPanel scrollPane = new JPanel();
    
    	JScrollPane scroller = new JScrollPane(scrollPane);
    
    	static DecimalFormat dmft = new DecimalFormat("0.00");
    
    
    	public static void main(String arg[]) {
    		ProjectEngine t = new ProjectEngine();
    		t.ProjectGUI();
    
    	}
    
    	public void ProjectGUI() {
    
    		panel_startup.setBackground(Color.white);
    		panel_startup.setLayout(new GridLayout(1, 5));
    
    		buttonLoad = new JButton("Load");
    		buttonLoad.setMaximumSize(new Dimension(1, 1));
    		buttonLoad.addActionListener(new e());
    		panel_startup.add(buttonLoad);
    
    		buttonReport = new JButton("Report");
    		buttonReport.setMaximumSize(new Dimension(1, 1));
    		buttonReport.addActionListener(new e());
    		panel_startup.add(buttonReport);
    
    		buttonMinMPG = new JButton("Minimum MPG");
    		buttonMinMPG.setMaximumSize(new Dimension(1, 1));
    		buttonMinMPG.addActionListener(new e());
    		panel_startup.add(buttonMinMPG);
    
    		buttonMaxMPG = new JButton("Maximum MPG");
    		buttonMaxMPG.setMaximumSize(new Dimension(1, 1));
    		buttonMaxMPG.addActionListener(new e());
    		panel_startup.add(buttonMaxMPG);
    
    		buttonAvgMPG = new JButton("Average MPG");
    		buttonAvgMPG.setMaximumSize(new Dimension(1, 1));
    		buttonAvgMPG.addActionListener(new e());
    		panel_startup.add(buttonAvgMPG);
    
    
    		panel_top.setBackground(Color.white);
    		panel_top.setLayout(new GridLayout(1, 8));
    
    
    
    		panel_middle.setBackground(Color.white);
    		panel_middle.setLayout(new GridLayout(8, 8));
    
    		panel_middle.add(Fname1);
    		panel_middle.add(Lname1);
    		panel_middle.add(Fares1);
    		panel_middle.add(GasMoney1);
    		panel_middle.add(Miles1);
    		panel_middle.add(Gallons1);
    		panel_middle.add(MPG1);
    		panel_middle.add(Profit1);
    
    		panel_middle.add(Fname2);
    		panel_middle.add(Lname2);
    		panel_middle.add(Fares2);
    		panel_middle.add(GasMoney2);
    		panel_middle.add(Miles2);
    		panel_middle.add(Gallons2);
    		panel_middle.add(MPG2);
    		panel_middle.add(Profit2);
    
    		panel_middle.add(Fname3);
    		panel_middle.add(Lname3);
    		panel_middle.add(Fares3);
    		panel_middle.add(GasMoney3);
    		panel_middle.add(Miles3);
    		panel_middle.add(Gallons3);
    		panel_middle.add(MPG3);
    		panel_middle.add(Profit3);
    
    		panel_middle.add(Fname4);
    		panel_middle.add(Lname4);
    		panel_middle.add(Fares4);
    		panel_middle.add(GasMoney4);
    		panel_middle.add(Miles4);
    		panel_middle.add(Gallons4);
    		panel_middle.add(MPG4);
    		panel_middle.add(Profit4);
    
    		panel_middle.add(Fname5);
    		panel_middle.add(Lname5);
    		panel_middle.add(Fares5);
    		panel_middle.add(GasMoney5);
    		panel_middle.add(Miles5);
    		panel_middle.add(Gallons5);
    		panel_middle.add(MPG5);
    		panel_middle.add(Profit5);
    
    		panel_middle.add(Fname6);
    		panel_middle.add(Lname6);
    		panel_middle.add(Fares6);
    		panel_middle.add(GasMoney6);
    		panel_middle.add(Miles6);
    		panel_middle.add(Gallons6);
    		panel_middle.add(MPG6);
    		panel_middle.add(Profit6);
    
    		panel_middle.add(Fname7);
    		panel_middle.add(Lname7);
    		panel_middle.add(Fares7);
    		panel_middle.add(GasMoney7);
    		panel_middle.add(Miles7);
    		panel_middle.add(Gallons7);
    		panel_middle.add(MPG7);
    		panel_middle.add(Profit7);
    
    		panel_middle.add(Fname8);
    		panel_middle.add(Lname8);
    		panel_middle.add(Fares8);
    		panel_middle.add(GasMoney8);
    		panel_middle.add(Miles8);
    		panel_middle.add(Gallons8);
    		panel_middle.add(MPG8);
    		panel_middle.add(Profit8);
    
    		panel_report.setLayout(new FlowLayout());
    		panel_report.add(panel_top);
    		panel_report.add(panel_middle);
    		panel_report.add(panel_bottom);
    
    		panel_top.add(start_display);
    		
    		panel_main.setLayout(new GridLayout(2, 1));
    		panel_main.setBackground(Color.white);
    		panel_main.add(panel_top);
    		panel_main.add(panel_startup);
    
    		frame.getContentPane().add(panel_main);
    		frame.setSize(100, 100);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public class e implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			Object source = e.getSource();
    
    			if (source == buttonLoad) {
    				Load();
    				Calculations();
    			}
    
    			if (source == buttonReport) {
    				Report();
    			}
    
    			if (source == buttonMinMPG) {
    
    				Min();
    			}
    
    			if (source == buttonMaxMPG) {
    				Max();
    			}
    
    			if (source == buttonAvgMPG) {
    				Avg();
    			}
    
    		}
    
    	}
    
    private static void ClearAll() {
    	report = "";
    	start_display.setText("");
    	report_display.setText("");
    	min_display.setText("");
    	max_display.setText("");
    	max_display.setText("");
    	mpg1 = 0; 
    	mpg2 = 0; 
    	mpg3 = 0;
    	gal1 = 0;
    	gal2 = 0;
    	gal3 = 0;
    	prof1 = 0;
    	prof2 = 0;
    	prof3 = 0;
    	mil1 = 0;
    	mil2 = 0;
    	mil3 = 0;
    	gasM1 = 0;
    	gasM2 = 0;
    	gasM3 = 0;
    	far1 = 0;
    	far2 = 0;
    	far3 = 0;
    }
    	
    	private static void Load() {
    		ClearAll();
    		JFileChooser chooser = new JFileChooser();
    		Scanner scanner = null;
    		int status = chooser.showOpenDialog(null);
    
    		if (status != JFileChooser.APPROVE_OPTION) {
    			System.out.println("No file was chosen.");
    			return;
    		}
    		try {
    			File file = chooser.getSelectedFile();
    			scanner = new Scanner(file);
    		} catch (IOException ioe) {
    			System.out.println("There was a problem opening the file.");
    		}
    
    		while (scanner.hasNext()) {
    			Fname1.setText(scanner.next());
    			Lname1.setText(scanner.next());
    			Fares1.setText(scanner.next());
    			GasMoney1.setText(scanner.next());
    			Miles1.setText(scanner.next());
    			Gallons1.setText(scanner.next());
    
    			Fname2.setText(scanner.next());
    			Lname2.setText(scanner.next());
    			Fares2.setText(scanner.next());
    			GasMoney2.setText(scanner.next());
    			Miles2.setText(scanner.next());
    			Gallons2.setText(scanner.next());
    
    			Fname3.setText(scanner.next());
    			Lname3.setText(scanner.next());
    			Fares3.setText(scanner.next());
    			GasMoney3.setText(scanner.next());
    			Miles3.setText(scanner.next());
    			Gallons3.setText(scanner.next());
    
    		}
    	}
    
    
    	private static void Calculations() {
    		mil1 = Double.parseDouble(Miles1.getText());
    		gal1 = Double.parseDouble(Gallons1.getText());
    		mpg1 = mil1 / gal1;
    		far1 = Double.parseDouble(Fares1.getText());
    		gasM1 = Double.parseDouble(GasMoney1.getText());
    		prof1 = far1 - gasM1;
    		MPG1.setText(dmft.format(mpg1));
    		Profit1.setText(dmft.format(prof1));
    
    		mil2 = Double.parseDouble(Miles2.getText());
    		gal2 = Double.parseDouble(Gallons2.getText());
    		mpg2 = mil2 / gal2;
    		far2 = Double.parseDouble(Fares2.getText());
    		gasM2 = Double.parseDouble(GasMoney2.getText());
    		prof2 = far2 - gasM2;
    		MPG2.setText(dmft.format(mpg2));
    		Profit2.setText(dmft.format(prof2));
    
    		mil3 = Double.parseDouble(Miles3.getText());
    		gal3 = Double.parseDouble(Gallons3.getText());
    		mpg3 = mil3 / gal3;
    		far3 = Double.parseDouble(Fares3.getText());
    		gasM3 = Double.parseDouble(GasMoney3.getText());
    		prof3 = far3 - gasM3;
    		MPG3.setText(dmft.format(mpg3));
    		Profit3.setText(dmft.format(prof3));
    
    	}
    
    
    	private static void Report() {
    
    		report = "First Name" + " Last Name" + " Fares" + " Gas Money"
    				+ " Miles" + " Gallons" + "  MPG" + " Profit " + "\n"
    				+ Fname1.getText() 
    				+ Lname1.getText()
    				+ Fares1.getText()
    				+ GasMoney1.getText()
    				+ Miles1.getText()
    				+ Gallons1.getText()
    				+ MPG1.getText()
    				+ Profit1.getText() + " "
    				+ "\n"
    				+ Fname2.getText()
    				+ Lname2.getText()
    				+ Fares2.getText()
    				+ GasMoney2.getText()
    				+ Miles2.getText()
    				+ Gallons2.getText()
    				+ MPG2.getText()
    				+ Profit2.getText() + " "
    				+ "\n"
    				+ Fname3.getText()
    				+ Lname3.getText()
    				+ Fares3.getText()
    				+ GasMoney3.getText()
    				+ Miles3.getText()
    				+ Gallons3.getText()
    				+ MPG3.getText()
    				+ Profit3.getText() 
    				+ "\n";
    		start_display.setText(report);
    		
    		System.out.println(report);
    	}
    
    
    	private static void Min() {
    		start_display.setText("" + min_display);
    	}
    
    
    	private static void Max() {
    		start_display.setText("" + max_display);
    	}
    
    
    	private static void Avg() {
    		start_display.setText("" + avg_display);
    	}
    }
    Text files (and yes, the client data is already put on to a newline in the txt file)

    OneRecord.txt
    Code:
    John Doe 100.00 20.00 240.00 8.0
    SeveralRecords.txt
    Code:
    John Doe 100.00 20.00 240.00 8.0
    Roger Ellis 120.00 24.00 250.0 9.0
    Larry Smith 130.00 25.00 250.0 7.0
    8Records.txt
    Code:
    John Doe 100.00 20.00 240.00 8.0
    Roger Ellis 120.00 24.00 250.0 9.0
    Larry Smith 130.00 25.00 250.0 7.0
    A B 200.00 39.0 300.0 13.0
    C D 210.00 40.0 310.0 13.5
    E F 220.00 41.0 320.0 14.0
    G H 230.00 42.0 330.0 14.5
    I J 240.00 43.0 340.0 15.0
    Thank you guys sooooooo much in advance!
    -Austin
    Last edited by aanders5; 12-08-2010 at 04:05 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hey! Now im happy to help - you seem genuinly like you have tried!

    I however dont have the time right now - I shall revisit this thread later on. However I can explain "this".

    the keyword "this" in java refers to the class you are dealing with, or more specifically the instance of the class you are dealing with. If your class had 3 variables one two and three I could do this.one to refer to the local variable.

    People usually use this when you have a situation as follows :

    Code:
    public class Monkey{
    
    private int one;
    private int two;
    
    //constructor
    
    public Monkey(int one, int two){
    this.one = one;
    this.two = two
    }
    }
    That example simply shows how "this" can be used to clearly identify which variable you are referencing at the point. If you took out the this, you would be saving the parameter you passed in "one" = "one" as opposed to my instance variable this.one = the parameter passed in. Its an OO principle.

    As for your layout problems that is just a massive pain in the neck for Java. Hate it! Have you tried using NetBeans IDE. It allows drag and drop layout of GUI elements essentially allowing you to draw the interface you want - I highly recommend it.

    Take the free version:

    http://netbeans.org/downloads/

    There are plugins for Eclipse to do similar things although not so good if that is your poison of choice!

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay thanks, I shall look into that.
    I use Eclipse right now at my university, and they have NetBeans on the machines too, but I am trying to do this one manually, rather with NetBeans.

    Thanks thus far,
    -Austin

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hey - Right im taking a look at this now. Im writing you a quick example of code which will cut down the amount of code and make it look a million times neater!

    Will post when im done! Just throwing it up quickly though , not going to polish, its going to be a proof of concept for you to extend

  • #5
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Right I have developed an example solution. It is a very much cut down version of your original program - cut into separate files and designed more as it should be - a far more scalable better solution.

    What you will see when you run the program is as follows:



    Now.

    If you download the zip from : http://www.sendspace.com/file/6u029u

    You will find an eclipse project. Import it using eclipse and move the SingleRecord.txt file to the C:\ root. "C:\SingleRecord.txt".

    Now run the ExampleSolution.java file and see it run.

    If you like and can work out whats going on I suggest merging this with your previous solution with the buttons to create a much nicer looking app! Much more simple also.

    Basically we have a JTable object - with an InteractiveTableModel - the model stores the information in the table and the Record object represents each line in our SingleRecord.txt file - so if SingleRecord.txt ws extended to have 2 or 3 or x many lines - you could extend the code Ive written to add 2 or 3 or x many entries in the JTable.

    Hope this helps buddy.

    Andy

  • #6
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I shall look at that in an hour so when I am back at school. I did some MASSIVE renovations to my code though, and I am still have troubles with getting my array to totally work, if you can see what I mean.
    (I put a print statement in there and its skipping my calculations for profit and MPG, which were working earlier until i messed something up.)


    I shall post that within the next 1:30 hours too.

    Thanks,
    -Austin

    And will JScroll work with this table stuff?

  • #7
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Pretty sure my code contains a JScroller

  • #8
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by renegadeandy View Post
    Pretty sure my code contains a JScroller
    Ok, I am sorry to bother you again, buttttt
    1. I think the coding you gave me too is advanced for me to do, thus looking like I copied it from somewhere.
    2. I don't fully understand it (I am working on it though, so i am very thankful for it)

    So, I have modified my code drastically, and now I only have two questions left for you.
    1. How do I get my JLabel to sort into rows nicely without using a JTable, but if that's the only way, please explain how it works to me.

    2. I need a JScrollPane for my code, and please explain how you seat it within my code.

    Thank you again, soooo much. This new source code is much shorter, and a ton less variables.
    And if you see anything else I can do to max it look nicer, that'd be great. And thank you sooo muchRenegadeandy, your probably the most hopefully java-related person I've met on any forum.

    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.*;
    
    public class ProjectEngine {
    
    	/*******************************************************************************************
    	 * @author Austin Anderson This program will choose a file,
    	 *         calculate the data then allow the user to see a full
    	 *         report, as well as the minimum, maximum, and average
    	 *         miles per gallon within the report.
    	 *******************************************************************************************/
    
    	private static JButton buttonLoad, buttonReport, buttonMinMPG,
    			buttonMaxMPG, buttonAvgMPG;
    
    	private static JLabel start_display = new JLabel(
    			"Please load data.", JLabel.CENTER);
    
    	static JFrame frame = new JFrame("Welcome!");
    
    	static JPanel panel_startup = new JPanel();
    
    	static JPanel panel_top = new JPanel();
    
    	static JPanel panel_main = new JPanel();
    
    	static ArrayList<String> al = new ArrayList<String>();;
    
    	static String report = "";
    
    	static double mpg1, mpg2, mpg3, mpg4, mpg5, mpg6, mpg7, mpg8, gal1,
    			gal2, gal3, gal4, gal5, gal6, gal7, gal8, prof1, prof2,
    			prof3, prof4, prof5, prof6, prof7, prof8, mil1, mil2, mil3,
    			mil4, mil5, mil6, mil7, mil8, gasM1, gasM2, gasM3, gasM4,
    			gasM5, gasM6, gasM7, gasM8, far1, far2, far3, far4, far5,
    			far6, far7, far8, min, max, avg;
    
    	static int tracker = 0;
    
    	static File file;
    
    	static DecimalFormat dmft = new DecimalFormat("0.00");
    
    
    	public static void main(String arg[]) {
    		ProjectEngine t = new ProjectEngine();
    		t.ProjectGUI();
    
    	}
    
    
    	public void ProjectGUI() {
    
    		panel_startup.setBackground(Color.white);
    		panel_startup.setLayout(new GridLayout(1, 5, 5, 5));
    		panel_startup.setAlignmentX(SwingConstants.CENTER);
    
    		buttonLoad = new JButton("Load");
    		buttonLoad.addActionListener(new e());
    		panel_startup.add(buttonLoad);
    
    		buttonReport = new JButton("Report");
    		buttonReport.addActionListener(new e());
    		panel_startup.add(buttonReport);
    
    		buttonMinMPG = new JButton("Minimum MPG");
    		buttonMinMPG.addActionListener(new e());
    		panel_startup.add(buttonMinMPG);
    
    		buttonMaxMPG = new JButton("Maximum MPG");
    		buttonMaxMPG.addActionListener(new e());
    		panel_startup.add(buttonMaxMPG);
    
    		buttonAvgMPG = new JButton("Average MPG");
    		buttonAvgMPG.addActionListener(new e());
    		panel_startup.add(buttonAvgMPG);
    
    		panel_top.setBackground(Color.white);
    		panel_top.setLayout(new GridLayout(1, 8));
    
    		panel_top.add(start_display);
    
    		panel_main.setLayout(new GridLayout(2, 1));
    		panel_main.setBackground(Color.white);
    		panel_main.add(panel_top);
    		panel_main.add(panel_startup);
    
    		frame.setSize(256, 256);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(panel_main);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public class e implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			Object source = e.getSource();
    
    			if (source == buttonLoad) {
    				Load();
    
    			}
    
    			if (source == buttonReport) {
    				Report();
    			}
    
    			if (source == buttonMinMPG) {
    
    				Min();
    			}
    
    			if (source == buttonMaxMPG) {
    				Max();
    			}
    
    			if (source == buttonAvgMPG) {
    				Avg();
    			}
    
    		}
    
    	}
    
    
    	private static void ClearAll() {
    		report = "";
    		start_display.setText("Loading file...");
    		tracker = 0;
    		al.clear();
    		mpg1 = 0;
    		mpg2 = 0;
    		mpg3 = 0;
    		mpg4 = 0;
    		mpg5 = 0;
    		mpg6 = 0;
    		mpg7 = 0;
    		mpg8 = 0;
    		gal1 = 0;
    		gal2 = 0;
    		gal3 = 0;
    		gal4 = 0;
    		gal5 = 0;
    		gal6 = 0;
    		gal7 = 0;
    		gal8 = 0;
    		prof1 = 0;
    		prof2 = 0;
    		prof3 = 0;
    		prof4 = 0;
    		prof5 = 0;
    		prof6 = 0;
    		prof7 = 0;
    		prof8 = 0;
    		mil1 = 0;
    		mil2 = 0;
    		mil3 = 0;
    		mil4 = 0;
    		mil5 = 0;
    		mil6 = 0;
    		mil7 = 0;
    		mil8 = 0;
    		gasM1 = 0;
    		gasM2 = 0;
    		gasM3 = 0;
    		gasM4 = 0;
    		gasM5 = 0;
    		gasM6 = 0;
    		gasM7 = 0;
    		gasM8 = 0;
    		far1 = 0;
    		far2 = 0;
    		far3 = 0;
    		far4 = 0;
    		far5 = 0;
    		far6 = 0;
    		far7 = 0;
    		far8 = 0;
    	}
    
    
    	private static void Load() {
    
    		ClearAll();
    		Files();
    		Calculations();
    		start_display.setText("File sucessfully loaded.");
    	}
    
    
    	private static void Files() {
    		JFileChooser chooser = new JFileChooser();
    		Scanner scanner = null;
    		int status = chooser.showOpenDialog(null);
    
    		if (status != JFileChooser.APPROVE_OPTION) {
    			System.out.println("No file was chosen.");
    			return;
    		}
    		try {
    			file = chooser.getSelectedFile();
    			scanner = new Scanner(file);
    		} catch (IOException ioe) {
    			System.out.println("There was a problem opening the file.");
    		}
    
    		while (scanner.hasNext()) {
    
    			al.add(scanner.next());
    
    		}
    	}
    
    
    	private static void Calculations() {
    
    		if (al.size() == 6) {
    
    			mil1 = Double.parseDouble(al.get(4));
    			gal1 = Double.parseDouble(al.get(5));
    			mpg1 = mil1 / gal1;
    			far1 = Double.parseDouble(al.get(2));
    			gasM1 = Double.parseDouble(al.get(3));
    			prof1 = far1 - gasM1;
    			al.add(6, dmft.format(mpg1));
    			al.add(7, dmft.format(prof1));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(mpg1));
    			min = Double.parseDouble(dmft.format(mpg1));
    			avg = Double.parseDouble(dmft.format(mpg1));
    		}
    		if (al.size() == 18) {
    			mil1 = Double.parseDouble(al.get(4));
    			gal1 = Double.parseDouble(al.get(5));
    			mpg1 = mil1 / gal1;
    			far1 = Double.parseDouble(al.get(2));
    			gasM1 = Double.parseDouble(al.get(3));
    			prof1 = far1 - gasM1;
    			al.add(6, dmft.format(mpg1));
    			al.add(7, dmft.format(prof1));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(mpg1));
    			min = Double.parseDouble(dmft.format(mpg1));
    			avg = Double.parseDouble(dmft.format(mpg1));
    
    			mil2 = Double.parseDouble(al.get(13));
    			gal2 = Double.parseDouble(al.get(14));
    			mpg2 = mil2 / gal2;
    			far2 = Double.parseDouble(al.get(11));
    			gasM2 = Double.parseDouble(al.get(12));
    			prof2 = far2 - gasM2;
    			al.add(15, dmft.format(mpg2));
    			al.add(16, dmft.format(prof2));
    			al.add(17, "\n");
    
    			if (mpg2 > max) {
    				max = Double.parseDouble(dmft.format(mpg2));
    			}
    			if (mpg2 < min) {
    				min = Double.parseDouble(dmft.format(mpg2));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg2))) / 2;
    
    			mil3 = Double.parseDouble(al.get(22));
    			gal3 = Double.parseDouble(al.get(23));
    			mpg3 = mil3 / gal3;
    			far3 = Double.parseDouble(al.get(20));
    			gasM3 = Double.parseDouble(al.get(21));
    			prof3 = far3 - gasM3;
    			al.add(24, dmft.format(mpg3));
    			al.add(25, dmft.format(prof3));
    			al.add(26, "\n");
    			if (mpg3 > max) {
    				max = Double.parseDouble(dmft.format(mpg3));
    			}
    			if (mpg3 < min) {
    				min = Double.parseDouble(dmft.format(mpg3));
    			}
    			avg = (avg + Double.parseDouble(dmft.format(mpg3))) / 2;
    		}
    		if (al.size() == 48) {
    			mil1 = Double.parseDouble(al.get(4));
    			gal1 = Double.parseDouble(al.get(5));
    			mpg1 = mil1 / gal1;
    			far1 = Double.parseDouble(al.get(2));
    			gasM1 = Double.parseDouble(al.get(3));
    			prof1 = far1 - gasM1;
    			al.add(6, dmft.format(mpg1));
    			al.add(7, dmft.format(prof1));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(mpg1));
    			min = Double.parseDouble(dmft.format(mpg1));
    			avg = Double.parseDouble(dmft.format(mpg1));
    
    			mil2 = Double.parseDouble(al.get(13));
    			gal2 = Double.parseDouble(al.get(14));
    			mpg2 = mil2 / gal2;
    			far2 = Double.parseDouble(al.get(11));
    			gasM2 = Double.parseDouble(al.get(12));
    			prof2 = far2 - gasM2;
    			al.add(15, dmft.format(mpg2));
    			al.add(16, dmft.format(prof2));
    			al.add(17, "\n");
    
    			if (mpg2 > max) {
    				max = Double.parseDouble(dmft.format(mpg2));
    			}
    			if (mpg2 < min) {
    				min = Double.parseDouble(dmft.format(mpg2));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg2))) / 2;
    
    			mil3 = Double.parseDouble(al.get(22));
    			gal3 = Double.parseDouble(al.get(23));
    			mpg3 = mil3 / gal3;
    			far3 = Double.parseDouble(al.get(20));
    			gasM3 = Double.parseDouble(al.get(21));
    			prof3 = far3 - gasM3;
    			al.add(24, dmft.format(mpg3));
    			al.add(25, dmft.format(prof3));
    			al.add(26, "\n");
    			if (mpg3 > max) {
    				max = Double.parseDouble(dmft.format(mpg3));
    			}
    			if (mpg3 < min) {
    				min = Double.parseDouble(dmft.format(mpg3));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg3))) / 2;
    
    			mil4 = Double.parseDouble(al.get(31));
    			gal4 = Double.parseDouble(al.get(32));
    			mpg4 = mil4 / gal4;
    			far4 = Double.parseDouble(al.get(29));
    			gasM4 = Double.parseDouble(al.get(30));
    			prof4 = far4 - gasM4;
    			al.add(33, dmft.format(mpg4));
    			al.add(34, dmft.format(prof4));
    			al.add(35, "\n");
    
    			if (mpg4 > max) {
    				max = Double.parseDouble(dmft.format(mpg4));
    			}
    			if (mpg4 < min) {
    				min = Double.parseDouble(dmft.format(mpg4));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg4))) / 2;
    
    			mil5 = Double.parseDouble(al.get(40));
    			gal5 = Double.parseDouble(al.get(41));
    			mpg5 = mil5 / gal5;
    			far5 = Double.parseDouble(al.get(38));
    			gasM5 = Double.parseDouble(al.get(39));
    			prof5 = far5 - gasM5;
    			al.add(42, dmft.format(mpg5));
    			al.add(43, dmft.format(prof5));
    			al.add(44, "\n");
    			if (mpg5 > max) {
    				max = Double.parseDouble(dmft.format(mpg5));
    			}
    			if (mpg5 < min) {
    				min = Double.parseDouble(dmft.format(mpg5));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg5))) / 2;
    
    			mil6 = Double.parseDouble(al.get(49));
    			gal6 = Double.parseDouble(al.get(50));
    			mpg6 = mil6 / gal6;
    			far6 = Double.parseDouble(al.get(47));
    			gasM6 = Double.parseDouble(al.get(48));
    			prof6 = far6 - gasM6;
    			al.add(51, dmft.format(mpg6));
    			al.add(52, dmft.format(prof6));
    			al.add(53, "\n");
    			if (mpg6 > max) {
    				max = Double.parseDouble(dmft.format(mpg6));
    			}
    			if (mpg6 < min) {
    				min = Double.parseDouble(dmft.format(mpg6));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg6))) / 2;
    
    			mil7 = Double.parseDouble(al.get(58));
    			gal7 = Double.parseDouble(al.get(59));
    			mpg7 = mil7 / gal7;
    			far7 = Double.parseDouble(al.get(56));
    			gasM7 = Double.parseDouble(al.get(57));
    			prof7 = far7 - gasM7;
    			al.add(60, dmft.format(mpg7));
    			al.add(61, dmft.format(prof7));
    			al.add(62, "\n");
    			if (mpg7 > max) {
    				max = Double.parseDouble(dmft.format(mpg7));
    			}
    			if (mpg7 < min) {
    				min = Double.parseDouble(dmft.format(mpg7));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg7))) / 2;
    
    			mil8 = Double.parseDouble(al.get(67));
    			gal8 = Double.parseDouble(al.get(68));
    			mpg8 = mil8 / gal8;
    			far8 = Double.parseDouble(al.get(65));
    			gasM8 = Double.parseDouble(al.get(66));
    			prof8 = far8 - gasM8;
    			al.add(69, dmft.format(mpg8));
    			al.add(70, dmft.format(prof8));
    			al.add(71, "\n");
    			if (mpg8 > max) {
    				max = Double.parseDouble(dmft.format(mpg8));
    			}
    			if (mpg8 < min) {
    				min = Double.parseDouble(dmft.format(mpg8));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(mpg8))) / 2;
    		}
    		al.add(0, "First Name");
    		al.add(1, "Last Name");
    		al.add(2, "Fares");
    		al.add(3, "Gas Money");
    		al.add(4, "Miles");
    		al.add(5, "Gallons");
    		al.add(6, "MPG");
    		al.add(7, "Profit");
    		al.add(8, "\n");
    
    	}
    
    
    	private static void Report() {
    
    		report = al.toString();
    		start_display.setText(report);
    	}
    
    
    	private static void Min() {
    		start_display
    				.setText("The minimum MPG is: " + dmft.format(min));
    	}
    
    
    	private static void Max() {
    		start_display
    				.setText("The maximum MPG is: " + dmft.format(max));
    	}
    
    
    	private static void Avg() {
    		start_display
    				.setText("The average MPG is: " + dmft.format(avg));
    	}
    }
    Thanks again! Please reply soon.
    -Austin

    P.S. Is there a way you think I can put all of the calculation variables in a simple array/arraylist? (I shall be looking into that right now until you reply)
    Last edited by aanders5; 12-09-2010 at 05:10 PM.

  • #9
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hi, Thanks for the praise!

    Ill take a look later on tonight and sort it out for you. You can tabulate label output so separate different fields with tabs for instance which can make it look more structured!

  • #10
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by renegadeandy View Post
    Hi, Thanks for the praise!

    Ill take a look later on tonight and sort it out for you. You can tabulate label output so separate different fields with tabs for instance which can make it look more structured!
    How do you tab? or make new lines with JLabels??

    Also, look at my new revised code! no more extra variables!!
    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.*;
    
    public class ProjectEngine {
    
    	/*******************************************************************************************
    	 * @author Austin Anderson This program will choose a file,
    	 *         calculate the data then allow the user to see a full
    	 *         report, as well as the minimum, maximum, and average
    	 *         miles per gallon within the report.
    	 *******************************************************************************************/
    
    	private static JButton buttonLoad, buttonReport, buttonMinMPG,
    			buttonMaxMPG, buttonAvgMPG;
    
    	private static JLabel start_display = new JLabel(
    			"Please load data.", JLabel.CENTER);
    
    	static JFrame frame = new JFrame("Welcome!");
    
    	static JPanel panel_startup = new JPanel();
    
    	static JPanel panel_top = new JPanel();
    
    	static JPanel panel_main = new JPanel();
    
    	static ArrayList<String> al = new ArrayList<String>();
    
    	static ArrayList<Double> calc = new ArrayList<Double>();
    
    	static String report = "";
    
    	static double min, max, avg;
    
    	static int tracker = 0;
    
    	static File file;
    
    	static DecimalFormat dmft = new DecimalFormat("0.00");
    
    
    	public static void main(String arg[]) {
    		ProjectEngine t = new ProjectEngine();
    		t.ProjectGUI();
    
    	}
    
    
    	public void ProjectGUI() {
    
    		panel_startup.setBackground(Color.white);
    		panel_startup.setLayout(new GridLayout(1, 5, 5, 5));
    		panel_startup.setAlignmentX(SwingConstants.CENTER);
    
    		buttonLoad = new JButton("Load");
    		buttonLoad.addActionListener(new e());
    		panel_startup.add(buttonLoad);
    
    		buttonReport = new JButton("Report");
    		buttonReport.addActionListener(new e());
    		panel_startup.add(buttonReport);
    
    		buttonMinMPG = new JButton("Minimum MPG");
    		buttonMinMPG.addActionListener(new e());
    		panel_startup.add(buttonMinMPG);
    
    		buttonMaxMPG = new JButton("Maximum MPG");
    		buttonMaxMPG.addActionListener(new e());
    		panel_startup.add(buttonMaxMPG);
    
    		buttonAvgMPG = new JButton("Average MPG");
    		buttonAvgMPG.addActionListener(new e());
    		panel_startup.add(buttonAvgMPG);
    
    		panel_top.setBackground(Color.white);
    		panel_top.setLayout(new GridLayout(1, 8));
    
    		panel_top.add(start_display);
    
    		panel_main.setLayout(new GridLayout(2, 1));
    		panel_main.setBackground(Color.white);
    		panel_main.add(panel_top);
    		panel_main.add(panel_startup);
    
    		frame.setSize(256, 256);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(panel_main);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public class e implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			Object source = e.getSource();
    
    			if (source == buttonLoad) {
    				Load();
    
    			}
    
    			if (source == buttonReport) {
    				Report();
    			}
    
    			if (source == buttonMinMPG) {
    
    				Min();
    			}
    
    			if (source == buttonMaxMPG) {
    				Max();
    			}
    
    			if (source == buttonAvgMPG) {
    				Avg();
    			}
    
    		}
    
    	}
    
    
    	private static void ClearAll() {
    		report = "";
    		start_display.setText("Loading file...");
    		tracker = 0;
    		al.clear();
    		calc.clear();
    	}
    
    
    	private static void Load() {
    
    		ClearAll();
    		Files();
    		Calculations();
    		start_display.setText("File sucessfully loaded.");
    	}
    
    
    	private static void Files() {
    		JFileChooser chooser = new JFileChooser();
    		Scanner scanner = null;
    		int status = chooser.showOpenDialog(null);
    
    		if (status != JFileChooser.APPROVE_OPTION) {
    			System.out.println("No file was chosen.");
    			return;
    		}
    		try {
    			file = chooser.getSelectedFile();
    			scanner = new Scanner(file);
    		} catch (IOException ioe) {
    			System.out.println("There was a problem opening the file.");
    		}
    
    		while (scanner.hasNext()) {
    
    			al.add(scanner.next());
    
    		}
    	}
    
    
    	private static void Calculations() {
    
    		if (al.size() == 6) {
    
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    		}
    		if (al.size() == 18) {
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    
    			calc.add(Double.parseDouble(al.get(13)));
    			calc.add(Double.parseDouble(al.get(14)));
    			calc.add(calc.get(6) / calc.get(7));
    			calc.add(Double.parseDouble(al.get(11)));
    			calc.add(Double.parseDouble(al.get(12)));
    			calc.add(calc.get(9) / calc.get(10));
    			al.add(15, dmft.format(calc.get(8)));
    			al.add(16, dmft.format(calc.get(11)));
    			al.add(17, "\n");
    
    			if (calc.get(8) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    			if (calc.get(8) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(8)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(22)));
    			calc.add(Double.parseDouble(al.get(23)));
    			calc.add(calc.get(12) / calc.get(13));
    			calc.add(Double.parseDouble(al.get(20)));
    			calc.add(Double.parseDouble(al.get(21)));
    			calc.add(calc.get(15) / calc.get(16));
    			al.add(24, dmft.format(calc.get(14)));
    			al.add(25, dmft.format(calc.get(17)));
    			al.add(26, "\n");
    			if (calc.get(14) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			if (calc.get(14) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(14)))) / 2;
    		}
    		if (al.size() == 48) {
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    
    			calc.add(Double.parseDouble(al.get(13)));
    			calc.add(Double.parseDouble(al.get(14)));
    			calc.add(calc.get(6) / calc.get(7));
    			calc.add(Double.parseDouble(al.get(11)));
    			calc.add(Double.parseDouble(al.get(12)));
    			calc.add(calc.get(9) / calc.get(10));
    			al.add(15, dmft.format(calc.get(8)));
    			al.add(16, dmft.format(calc.get(11)));
    			al.add(17, "\n");
    
    			if (calc.get(8) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    			if (calc.get(8) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(8)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(22)));
    			calc.add(Double.parseDouble(al.get(23)));
    			calc.add(calc.get(12) / calc.get(13));
    			calc.add(Double.parseDouble(al.get(20)));
    			calc.add(Double.parseDouble(al.get(21)));
    			calc.add(calc.get(15) / calc.get(16));
    			al.add(24, dmft.format(calc.get(14)));
    			al.add(25, dmft.format(calc.get(17)));
    			al.add(26, "\n");
    			if (calc.get(14) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			if (calc.get(14) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(14)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(31)));
    			calc.add(Double.parseDouble(al.get(32)));
    			calc.add(calc.get(18) / calc.get(19));
    			calc.add(Double.parseDouble(al.get(29)));
    			calc.add(Double.parseDouble(al.get(30)));
    			calc.add(calc.get(21) / calc.get(22));
    			al.add(33, dmft.format(calc.get(20)));
    			al.add(34, dmft.format(calc.get(23)));
    			al.add(35, "\n");
    
    			if (calc.get(20) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(20)));
    			}
    			if (calc.get(20) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(20)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(20)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(40)));
    			calc.add(Double.parseDouble(al.get(41)));
    			calc.add(calc.get(24) / calc.get(25));
    			calc.add(Double.parseDouble(al.get(38)));
    			calc.add(Double.parseDouble(al.get(39)));
    			calc.add(calc.get(27) / calc.get(28));
    			al.add(42, dmft.format(calc.get(26)));
    			al.add(43, dmft.format(calc.get(29)));
    			al.add(44, "\n");
    			if (calc.get(26) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(26)));
    			}
    			if (calc.get(26) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(26)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(26)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(49)));
    			calc.add(Double.parseDouble(al.get(50)));
    			calc.add(calc.get(30) / calc.get(31));
    			calc.add(Double.parseDouble(al.get(47)));
    			calc.add(Double.parseDouble(al.get(48)));
    			calc.add(calc.get(33) / calc.get(34));
    			al.add(51, dmft.format(calc.get(32)));
    			al.add(52, dmft.format(calc.get(35)));
    			al.add(53, "\n");
    			if (calc.get(32) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(32)));
    			}
    			if (calc.get(32) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(32)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(32)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(58)));
    			calc.add(Double.parseDouble(al.get(59)));
    			calc.add(calc.get(36) / calc.get(37));
    			calc.add(Double.parseDouble(al.get(56)));
    			calc.add(Double.parseDouble(al.get(57)));
    			calc.add(calc.get(39) / calc.get(40));
    			al.add(60, dmft.format(calc.get(38)));
    			al.add(61, dmft.format(calc.get(41)));
    			al.add(62, "\n");
    			if (calc.get(38) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(38)));
    			}
    			if (calc.get(38) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(38)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(38)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(67)));
    			calc.add(Double.parseDouble(al.get(68)));
    			calc.add(calc.get(42) / calc.get(43));
    			calc.add(Double.parseDouble(al.get(65)));
    			calc.add(Double.parseDouble(al.get(66)));
    			calc.add(calc.get(45) / calc.get(46));
    			al.add(69, dmft.format(calc.get(44)));
    			al.add(70, dmft.format(calc.get(47)));
    			al.add(71, "\n");
    			if (calc.get(44) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(44)));
    			}
    			if (calc.get(44) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(44)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(44)))) / 2;
    		}
    		al.add(0, "First Name");
    		al.add(1, "Last Name");
    		al.add(2, "Fares");
    		al.add(3, "Gas Money");
    		al.add(4, "Miles");
    		al.add(5, "Gallons");
    		al.add(6, "MPG");
    		al.add(7, "Profit");
    		al.add(8, "\n");
    
    	}
    
    
    	private static void Report() {
    
    		report = al.toString().replace(",", "").replace("[", "")
    				.replace("[", "");
    
    		start_display.setText(report);
    	}
    
    
    	private static void Min() {
    		start_display
    				.setText("The minimum MPG is: " + dmft.format(min));
    	}
    
    
    	private static void Max() {
    		start_display
    				.setText("The maximum MPG is: " + dmft.format(max));
    	}
    
    
    	private static void Avg() {
    		start_display
    				.setText("The average MPG is: " + dmft.format(avg));
    	}
    }
    Oh, and when I move it to display on the JLabel, how do I get rid of the comma's and brackets?
    EDIT: I got it to remove brackets and comma's, just need to do a JScroll and have it print on new lines.
    Last edited by aanders5; 12-09-2010 at 05:50 PM.

  • #11
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Well done on your perseverence.


    Just do :

    String output = "Hello\nssecondline\n another new line";

    String output = "hello \t tab2 \t tab3";

    \n = newline
    \t = tab

  • #12
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by renegadeandy View Post
    Well done on your perseverence.


    Just do :

    String output = "Hello\nssecondline\n another new line";

    String output = "hello \t tab2 \t tab3";

    \n = newline
    \t = tab
    If you notice in my code, I have "/n"'s every 8 parts of the array, so when I S.O.P it shows up in a each of its own line. BUT when i convert it to SetText, it becomes one mass line, with spaces where the "/n"'s appeared at. Not sure what to do... Would like a textfield or something work better?

    Here's my most recent, few little changes to GUI.
    Still need JScroll help, and newlines.
    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import javax.swing.*;
    
    public class ProjectEngine {
    
    	/*******************************************************************************************
    	 * @author Austin Anderson This program will choose a file,
    	 *         calculate the data then allow the user to see a full
    	 *         report, as well as the minimum, maximum, and average
    	 *         miles per gallon within the report.
    	 *******************************************************************************************/
    
    	private static JButton buttonLoad, buttonReport, buttonMinMPG,
    			buttonMaxMPG, buttonAvgMPG, buttonExit;
    
    	private static JLabel start_display = new JLabel(
    			"Please load data.", JLabel.CENTER);
    
    	static JFrame frame = new JFrame("Welcome!");
    
    	static JPanel panel_buttons = new JPanel();
    
    	static JPanel panel_top = new JPanel();
    
    	static JPanel panel_main = new JPanel();
    
    	static ArrayList<String> al = new ArrayList<String>();
    
    	static ArrayList<Double> calc = new ArrayList<Double>();
    
    	static String report = "";
    
    	static double min, max, avg;
    
    	static int tracker = 0;
    
    	static File file;
    
    	static DecimalFormat dmft = new DecimalFormat("0.00");
    
    	Dimension d = new Dimension(650, 150);
    
    	Dimension b = new Dimension(600, 50);
    
    
    	public static void main(String arg[]) {
    		ProjectEngine t = new ProjectEngine();
    		t.ProjectGUI();
    
    	}
    
    
    	public void ProjectGUI() {
    
    		panel_buttons.setBackground(Color.white);
    		panel_buttons.setLayout(new GridLayout(2, 5, 3, 3));
    		panel_buttons.setSize(b);
    
    		buttonMaxMPG = new JButton("Maximum MPG");
    		buttonMaxMPG.addActionListener(new e());
    		panel_buttons.add(buttonMaxMPG);
    
    		buttonAvgMPG = new JButton("Average MPG");
    		buttonAvgMPG.addActionListener(new e());
    		panel_buttons.add(buttonAvgMPG);
    
    		buttonMinMPG = new JButton("Minimum MPG");
    		buttonMinMPG.addActionListener(new e());
    		panel_buttons.add(buttonMinMPG);
    
    		buttonLoad = new JButton("Load");
    		buttonLoad.addActionListener(new e());
    		panel_buttons.add(buttonLoad);
    
    		buttonReport = new JButton("Report");
    		buttonReport.addActionListener(new e());
    		panel_buttons.add(buttonReport);
    
    		buttonExit = new JButton("Exit");
    		buttonExit.addActionListener(new e());
    		panel_buttons.add(buttonExit);
    
    		panel_buttons.setBorder(BorderFactory.createLineBorder(
    				Color.black, 1));
    		panel_buttons.setBackground(Color.black);
    
    		panel_top.setBackground(Color.white);
    		panel_top.add(start_display);
    		panel_top
    				.setBorder(BorderFactory.createLineBorder(Color.black));
    
    		panel_main.setLayout(new GridLayout(2, 1));
    		panel_main.setBackground(Color.white);
    		panel_main.add(panel_top);
    		panel_main.add(panel_buttons);
    		panel_main.setBorder(BorderFactory.createLineBorder(
    				Color.black, 2));
    
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(panel_main);
    		frame.pack();
    		frame.setSize(d);
    		frame.setVisible(true);
    	}
    
    	public class e implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			Object source = e.getSource();
    
    			if (source == buttonLoad) {
    				Load();
    
    			}
    
    			if (source == buttonReport) {
    				Report();
    			}
    
    			if (source == buttonMinMPG) {
    
    				Min();
    			}
    
    			if (source == buttonMaxMPG) {
    				Max();
    			}
    
    			if (source == buttonAvgMPG) {
    				Avg();
    			}
    
    			if (source == buttonExit) {
    				System.exit(1);
    			}
    
    		}
    
    	}
    
    
    	private static void ClearAll() {
    		report = "";
    		start_display.setText("Loading file...");
    		tracker = 0;
    		al.clear();
    		calc.clear();
    	}
    
    
    	private static void Load() {
    
    		ClearAll();
    		Files();
    		Calculations();
    		start_display.setText("File sucessfully loaded.");
    	}
    
    
    	private static void Files() {
    		JFileChooser chooser = new JFileChooser();
    		Scanner scanner = null;
    		int status = chooser.showOpenDialog(null);
    
    		if (status != JFileChooser.APPROVE_OPTION) {
    			System.out.println("No file was chosen.");
    			return;
    		}
    		try {
    			file = chooser.getSelectedFile();
    			scanner = new Scanner(file);
    		} catch (IOException ioe) {
    			System.out.println("There was a problem opening the file.");
    		}
    
    		while (scanner.hasNext()) {
    
    			al.add(scanner.next());
    
    		}
    	}
    
    
    	private static void Calculations() {
    
    		if (al.size() == 6) {
    
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    		}
    		if (al.size() == 18) {
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    
    			calc.add(Double.parseDouble(al.get(13)));
    			calc.add(Double.parseDouble(al.get(14)));
    			calc.add(calc.get(6) / calc.get(7));
    			calc.add(Double.parseDouble(al.get(11)));
    			calc.add(Double.parseDouble(al.get(12)));
    			calc.add(calc.get(9) / calc.get(10));
    			al.add(15, dmft.format(calc.get(8)));
    			al.add(16, dmft.format(calc.get(11)));
    			al.add(17, "\n");
    
    			if (calc.get(8) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    			if (calc.get(8) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(8)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(22)));
    			calc.add(Double.parseDouble(al.get(23)));
    			calc.add(calc.get(12) / calc.get(13));
    			calc.add(Double.parseDouble(al.get(20)));
    			calc.add(Double.parseDouble(al.get(21)));
    			calc.add(calc.get(15) / calc.get(16));
    			al.add(24, dmft.format(calc.get(14)));
    			al.add(25, dmft.format(calc.get(17)));
    			al.add(26, "\n");
    			if (calc.get(14) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			if (calc.get(14) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(14)))) / 2;
    		}
    		if (al.size() == 48) {
    			calc.add(Double.parseDouble(al.get(4)));
    			calc.add(Double.parseDouble(al.get(5)));
    			calc.add(calc.get(0) / calc.get(1));
    			calc.add(Double.parseDouble(al.get(2)));
    			calc.add(Double.parseDouble(al.get(3)));
    			calc.add(calc.get(3) - calc.get(4));
    			al.add(6, dmft.format(calc.get(2)));
    			al.add(7, dmft.format(calc.get(5)));
    			al.add(8, "\n");
    
    			max = Double.parseDouble(dmft.format(calc.get(2)));
    			min = Double.parseDouble(dmft.format(calc.get(2)));
    			avg = Double.parseDouble(dmft.format(calc.get(2)));
    
    			calc.add(Double.parseDouble(al.get(13)));
    			calc.add(Double.parseDouble(al.get(14)));
    			calc.add(calc.get(6) / calc.get(7));
    			calc.add(Double.parseDouble(al.get(11)));
    			calc.add(Double.parseDouble(al.get(12)));
    			calc.add(calc.get(9) / calc.get(10));
    			al.add(15, dmft.format(calc.get(8)));
    			al.add(16, dmft.format(calc.get(11)));
    			al.add(17, "\n");
    
    			if (calc.get(8) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    			if (calc.get(8) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(8)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(8)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(22)));
    			calc.add(Double.parseDouble(al.get(23)));
    			calc.add(calc.get(12) / calc.get(13));
    			calc.add(Double.parseDouble(al.get(20)));
    			calc.add(Double.parseDouble(al.get(21)));
    			calc.add(calc.get(15) / calc.get(16));
    			al.add(24, dmft.format(calc.get(14)));
    			al.add(25, dmft.format(calc.get(17)));
    			al.add(26, "\n");
    			if (calc.get(14) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			if (calc.get(14) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(14)));
    			}
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(14)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(31)));
    			calc.add(Double.parseDouble(al.get(32)));
    			calc.add(calc.get(18) / calc.get(19));
    			calc.add(Double.parseDouble(al.get(29)));
    			calc.add(Double.parseDouble(al.get(30)));
    			calc.add(calc.get(21) / calc.get(22));
    			al.add(33, dmft.format(calc.get(20)));
    			al.add(34, dmft.format(calc.get(23)));
    			al.add(35, "\n");
    
    			if (calc.get(20) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(20)));
    			}
    			if (calc.get(20) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(20)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(20)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(40)));
    			calc.add(Double.parseDouble(al.get(41)));
    			calc.add(calc.get(24) / calc.get(25));
    			calc.add(Double.parseDouble(al.get(38)));
    			calc.add(Double.parseDouble(al.get(39)));
    			calc.add(calc.get(27) / calc.get(28));
    			al.add(42, dmft.format(calc.get(26)));
    			al.add(43, dmft.format(calc.get(29)));
    			al.add(44, "\n");
    			if (calc.get(26) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(26)));
    			}
    			if (calc.get(26) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(26)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(26)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(49)));
    			calc.add(Double.parseDouble(al.get(50)));
    			calc.add(calc.get(30) / calc.get(31));
    			calc.add(Double.parseDouble(al.get(47)));
    			calc.add(Double.parseDouble(al.get(48)));
    			calc.add(calc.get(33) / calc.get(34));
    			al.add(51, dmft.format(calc.get(32)));
    			al.add(52, dmft.format(calc.get(35)));
    			al.add(53, "\n");
    			if (calc.get(32) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(32)));
    			}
    			if (calc.get(32) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(32)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(32)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(58)));
    			calc.add(Double.parseDouble(al.get(59)));
    			calc.add(calc.get(36) / calc.get(37));
    			calc.add(Double.parseDouble(al.get(56)));
    			calc.add(Double.parseDouble(al.get(57)));
    			calc.add(calc.get(39) / calc.get(40));
    			al.add(60, dmft.format(calc.get(38)));
    			al.add(61, dmft.format(calc.get(41)));
    			al.add(62, "\n");
    			if (calc.get(38) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(38)));
    			}
    			if (calc.get(38) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(38)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(38)))) / 2;
    
    			calc.add(Double.parseDouble(al.get(67)));
    			calc.add(Double.parseDouble(al.get(68)));
    			calc.add(calc.get(42) / calc.get(43));
    			calc.add(Double.parseDouble(al.get(65)));
    			calc.add(Double.parseDouble(al.get(66)));
    			calc.add(calc.get(45) / calc.get(46));
    			al.add(69, dmft.format(calc.get(44)));
    			al.add(70, dmft.format(calc.get(47)));
    			al.add(71, "\n");
    			if (calc.get(44) > max) {
    				max = Double.parseDouble(dmft.format(calc.get(44)));
    			}
    			if (calc.get(44) < min) {
    				min = Double.parseDouble(dmft.format(calc.get(44)));
    			}
    
    			avg = (avg + Double.parseDouble(dmft.format(calc.get(44)))) / 2;
    		}
    		al.add(0, "First Name");
    		al.add(1, "Last Name");
    		al.add(2, "Fares");
    		al.add(3, "Gas Money");
    		al.add(4, "Miles");
    		al.add(5, "Gallons");
    		al.add(6, "MPG");
    		al.add(7, "Profit");
    		al.add(8, "\n");
    
    	}
    
    
    	private static void Report() {
    
    		if (al.size() > 0) {
    			report = al.toString().replace(",", "").replace("[", "")
    					.replace("[", "");
    
    		} else {
    			report = "No file selected yet";
    		}
    		start_display.setText(report);
    	}
    
    
    	private static void Min() {
    		if (al.size() > 0) {
    
    			start_display.setText("The minimum MPG is: "
    					+ dmft.format(min));
    		} else {
    			start_display.setText("No file selected yet");
    		}
    	}
    
    
    	private static void Max() {
    		if (al.size() > 0) {
    			start_display.setText("The maximum MPG is: "
    					+ dmft.format(max));
    		} else {
    			start_display.setText("No file selected yet");
    		}
    	}
    
    
    	private static void Avg() {
    		if (al.size() > 0) {
    			start_display.setText("The average MPG is: "
    					+ dmft.format(avg));
    		} else {
    			start_display.setText("No file selected yet");
    		}
    	}
    }

  • #13
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Try making the dimensions of the label you are setting really large, its probably wrapping because they are set too small.

    setDimensions(new Point(400,400)); on the label, something like that might help the new lines showing up as spaces etc!

  • #14
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    setDimensions isn't for JLabel, are you thinking of something else?

  • #15
    New Coder
    Join Date
    Oct 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tired using setSize and stuff as well, with a million sizes and nothing is changing. It just center aligns the whole report, and then makes one massive line of it. :/

    Is there a way, to make the text not run out of the JPanel/JLabel, so that it is forced to make newlines?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •