Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: java GUI , reading in files

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java GUI , reading in files

    hello! im working ona program that reads in a file called pot4.asc. my button Read File , is supposed to read in a file when clicked. i already have all the work done in my model class , im just trying to get it to work with a GUI and a button. i cant figure out what im doing wrong.

    my action listener is at the bottom , i called my model class and its readFromFile. shouldnt that work? im thinking maybe i missed something with the textfield?
     
     
     
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Gui {
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("TeaPot");
     
    		JTextField textField = new JTextField("pot4.asc",10);
     
     
     
    		GridLayout applicationLayout = new GridLayout(1,2);
    		frame.setLayout(applicationLayout);
     
    		JPanel rightPanel = new JPanel();
    		JPanel leftPanel = new JPanel();
     
    		rightPanel.add(textField);
     
    		GridLayout canvasLayout = new GridLayout(1,1);
    		leftPanel.setLayout(canvasLayout);
     
    		MyCanvas canvas = new MyCanvas(300,300);
    		leftPanel.add(canvas);
     
    		FlowLayout buttonLayout = new FlowLayout();
    		rightPanel.setLayout(buttonLayout);
     
    		JButton button1 = new JButton("Read In Data");
    		button1.addActionListener(new eventRead());
    		rightPanel.add( button1 );
     
    		JButton button2 = new JButton("Write Out");
    		button2.addActionListener(new eventWrite());
    		rightPanel.add( button2 );
     
    		JButton button3 = new JButton("Statistical Data");
    		button3.addActionListener(new eventPrintStatistics());
    		rightPanel.add( button3 );
     
     
    		frame.add(leftPanel);
    		frame.add(rightPanel);
     
     
    		frame.setSize(800, 400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(
    				JFrame.EXIT_ON_CLOSE);
    	}
    }
     
     
    class eventRead implements ActionListener{
     
    	public void actionPerformed(ActionEvent e) {
    		String inputFilename = "pot4.asc";
    		JLabel label = new JLabel("Data Read");
    		Model m = new Model();
    		try{	    
    			m.readFromFile(inputFilename);
    			label.setText("File Read");
    			System.out.println("File Read");
    		} catch (Exception e1){
    			System.out.println(e1.getMessage());
    			inputFilename="pot4.asc";
    			System.out.println("Using "+ inputFilename+ " instead");
     
    		}
     
    	}
     
     
     
    }

    model class

     
     
     
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class Model {
     
    	ArrayList<Triangle> triangleArray = new ArrayList<Triangle>();
     
    	void readFromFile(String filename) throws Exception {
    		File file = new File(filename);
    		readFromFile(file);
    	}
     
    	void writeDataToFile(File file) {
    		try{
     
    			PrintWriter out = new PrintWriter(file);
    			for(Triangle t: triangleArray){
    				out.println("triangle");
    				out.print( t.toString() );
     
    			}
    			out.close();
    		}catch(Exception e){
     
    		}
    	}
     
     
     
    	void writeInfoToFile(File file) {
    		double area = computeSurfaceArea();
    		double nTriangle = triangleArray.size();
    		double min = computeMin();
    		double max= computeMax();
    		double averageArea = area / nTriangle;
     
    		try{
     
    			PrintWriter out = new PrintWriter(file);
    			out.println("Total number of triangles = "+nTriangle);
    			out.println("Area = "+area);
    			out.println("Min = "+min);
    			out.println("Max = "+max);
    			out.println("Average Area = "+averageArea);
     
     
     
    			out.close();
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
     
    	void readFromFile(File file) throws Exception {
    		try {
     
    			Scanner s2 = new Scanner(file);
    			while (s2.hasNext()) {
    				s2.nextLine(); // tosss the first line
    				String[] threeLines = { //array with 3 lines, reads in 3 lines
    						s2.nextLine(), 
    						s2.nextLine(),
    						s2.nextLine() 
    				};
     
    				Triangle newTriangleObject = new Triangle(threeLines);
    				triangleArray.add( newTriangleObject );
    			}
    		} catch (Exception e) {
    			System.out.println("Model.readFromFile: "+e.getMessage());
    			String bottle = "error: "+e.getMessage();
    			throw new Exception(bottle);
    			//e.printStackTrace();
    		}
    	}
     
    	double computeSurfaceArea() {
    		double v = 0;
    		// loop through each triangle and sum up their areas
    		for ( Triangle t : triangleArray )
    		{
    			double sArea = t.computeArea();
    			v += sArea;
    		}
    		return v;
    	}
     
     
    	double computeMax(){
    		double max= -Double.MAX_VALUE;
    		for(Triangle t: triangleArray){
    			max = (max > t.computeArea() ? max : t.computeArea());
     
    		}
     
    		return max;
     
    	}  
     
    	double computeMin(){  
    		double min= Double.MAX_VALUE;
    		for ( Triangle t : triangleArray) {
     
    			if (min > t.computeArea() ){
    				min =t.computeArea();
     
    			} 
     
    		}
    		return min;
     
    	}  
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    trying to get it to work with a GUI and a button. i cant figure out what im doing wrong.
    Can you explain what the problem is?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java GUI , reading in files

    my problem is that my actionlistener isnt reading in the data right. everytime i hit the read button , it creates a model each time and the variable is only visible in that context making the model not accessible outside. how could i fix that? would making the model outside the actionPerformed method fix that?
     
    class eventRead implements ActionListener{
     
    	public void actionPerformed(ActionEvent e) {
    		String inputFilename = "pot4.asc";
    		JLabel label = new JLabel("Data Read");
    		Model m = new Model();
    		try{	    
    			m.readFromFile(inputFilename);
    			label.setText("File Read");
    			System.out.println("File Read");
    		} catch (Exception e1){
    			System.out.println(e1.getMessage());
    			inputFilename="pot4.asc";
    			System.out.println("Using "+ inputFilename+ " instead");
     
    		}
     
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    actionlistener isnt reading in the data right
    What is wrong with the way the data is read? Is the reading done in the readFromFile() method? What does it do incorrectly?

    the model not accessible outside
    The variable: m defined inside the listener will go away when the listener method exits. If the reference to the Model object that was created is not saved anywhere outside of the listener method, any data in that object will not be accessible to the rest of the program.

    The listener method should not do any tasks that can take very long because the GUI is frozen until the task ends.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java GUI , reading in files

    i see what you mean , about the teh variable m. but i cant figure out how to fix it. would i just create another reference to the model object outside the listener?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    Why are you declaring and creating a Model object inside a method? What code wants to use the data the readFromFile() method gets from the file?
    Can you declare it outside of the method and use it in the method?
    If it were declared as a class variable, then it would stay around and could be accessed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java GUI , reading in files

    im creating the model object inside the method because i was calling the method readFromFile thats in the model class, that already does the work for reading in files. i figured all i had to do was just create the listener and create the model object and call the readFromFile() method to do the work. for it to be class variable it would need to e static right? that would kind of ruin my model class lol.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    A static variable is used when you only want there to be only one variable that all instances of a class can share.
    You don't normally make a class variable static. A variable defined as a class instance variable will have its own value in each instance of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java GUI , reading in files

    argg sorry , but im really spazzing out here lol. i dont quite get what im supposed to do. ok , so ur saying i should declare it outside of the method and use it in the method. so im in my GUI class , which is were the Reading takes place. i should make my object outside of the listener , so i did this. i defined model right at the beginning of the code. then how could i access it inside the listener? i appologize if this is totally wrong lol

     
     
     
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Gui {
     
    	Model m = new Model();
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("TeaPot");
     
    		JTextField textField = new JTextField("pot4.asc",10);
     
     
     
    		GridLayout applicationLayout = new GridLayout(1,2);
    		frame.setLayout(applicationLayout);
     
    		JPanel rightPanel = new JPanel();
    		JPanel leftPanel = new JPanel();
     
    		rightPanel.add(textField);
     
    		GridLayout canvasLayout = new GridLayout(1,1);
    		leftPanel.setLayout(canvasLayout);
     
    		MyCanvas canvas = new MyCanvas(300,300);
    		leftPanel.add(canvas);
     
    		FlowLayout buttonLayout = new FlowLayout();
    		rightPanel.setLayout(buttonLayout);
     
    		JButton button1 = new JButton("Read In Data");
    		button1.addActionListener(new eventRead());
    		rightPanel.add( button1 );
     
    		JButton button2 = new JButton("Write Out");
    		button2.addActionListener(new eventWrite());
    		rightPanel.add( button2 );
     
    		JButton button3 = new JButton("Statistical Data");
    		button3.addActionListener(new eventPrintStatistics());
    		rightPanel.add( button3 );
     
     
    		frame.add(leftPanel);
    		frame.add(rightPanel);
     
     
    		frame.setSize(800, 400);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(
    				JFrame.EXIT_ON_CLOSE);
    	}
    }
     
     
    class eventRead implements ActionListener{
     
     
    	public void actionPerformed(ActionEvent e) {
    		String inputFilename = "pot4.asc";
    		JLabel label = new JLabel("Data Read");
    		try{	    
    			m.readFromFile(inputFilename);
    			label.setText("File Read");
    			System.out.println("File Read");
    		} catch (Exception e1){
    			System.out.println(e1.getMessage());
    			inputFilename="pot4.asc";
    			System.out.println("Using "+ inputFilename+ " instead");
     
    		}
     
    	}
     
     
     
     
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    What happens when you compile and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java GUI , reading in files

    it wont compile , because i cant access model in the actionlistener. i cant call m.readFromFile. it says m cannot be resolved.

    class eventRead implements ActionListener{
     
     
    	public void actionPerformed(ActionEvent e) {
    		String inputFilename = "pot4.asc";
    		JLabel label = new JLabel("Data Read");
    		try{	    
    			m.readFromFile(inputFilename);
    			label.setText("File Read");
    			System.out.println("File Read");
    		} catch (Exception e1){
    			System.out.println(e1.getMessage());
    			inputFilename="pot4.asc";
    			System.out.println("Using "+ inputFilename+ " instead");
     
    		}
     
    	}

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,384
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java GUI , reading in files

    Couple of solutions:
    pass the reference to the model: m to the listener's constructor and save it in the listener's class.
    Make the listener class an inner class
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 2nd, 2012, 11:13 PM
  2. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 11:47 AM
  3. Reading In Then Writing Out Massive Files
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 28th, 2011, 07:00 PM
  4. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 06:16 PM
  5. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM