Results 1 to 2 of 2
  1. #1
    javanewbex is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Exclamation New to java , little help

    Hello Guys,

    Im new to Java,

    I am checking how to do following task.
    01. pickup the selected text file and read the line by line and output the text in to visual text pane.

    what i did:.

    01. I wrote code with the help of google and youtube that read the text file and output in to jave console/ also some of the interface.

    the code read txt file:

    Java Code:
    String fileName = "C:/Users/lakshan/Desktop/lawyer.txt";
    	File textFile = new File(fileName);
    	Scanner  in = new Scanner (textFile);
    	while(in.hasNextLine()){
    		
    			String line =in.nextLine();
    			System.out.println (line);
    	}
    	
    	
    	in.close();
    	
    	}
    and it works:)

    02. also i create interface with jswing and it lets me to select the file from any location of the computer and make the path in to text (label).

    Java Code:
    JFileChooser fileChooser = new JFileChooser();fc = new JFrame();
    		fc.setBounds(100, 100, 800, 500);
    		fc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//fileChooser.setBounds(5, 11, 753, 243);
    		fc.getContentPane().add(fileChooser);
    		fc.getContentPane().setLayout(null);
    		JLabel lblFileName = new JLabel("New label");
    		lblFileName.setFont(new Font("Tahoma", Font.PLAIN, 17));
    		lblFileName.setBounds(10, 25, 764, 30);
    		fc.getContentPane().add(lblFileName);
    		
    		JLabel lblFilePath = new JLabel("File Path");
    		lblFilePath.setBounds(10, 11, 764, 14);
    		fc.getContentPane().add(lblFilePath);
    		
    		
    		final JFileChooser fc= new JFileChooser();
    		int response = fc.showOpenDialog(fc);
    		
    		
    		
    		
    		if (response == JFileChooser.APPROVE_OPTION) {
    		lblFileName.setText(fc.getSelectedFile().toString());
    		//fileName = fc.getSelectedFile().toString();
    		
    
    	
    		}
    		
    		
    		in.close();
    		
    		}
    		else {
    			lblFileName.setText("You havent Select any file Yet");
    		//	fileName = "Thsi File didnt selecteed yet!!!!";
    			
    		}



    all i need to do



    String fileName = "C:/Users/lakshan/Desktop/lawyer.txt"; mapped with lblFileName.setText(fc.getSelectedFile().toString( )); so it will read any text file dynamically and output to the text pane in interface.



    I think scanner code must be execute after the select the file from the browser and set the scanned result in to variable. then later out put the var as string in some jswing component? which component name should it be

    again im not sure

    Can you help me please ? thank you for your time.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: New to java , little help

    Please post a Short, Self Contained, Correct Example that demonstrates the problem. Don't just post unrelated snippets as they are hard to follow. And please ensure your code is properly indented for readability.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Posting Permissions

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