Results 1 to 7 of 7
  1. #1
    pogz is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Post How to Output in Notepad using JButton

    pls help me with my program, i just use dialog box for output but i want this in notepad. i dont know how, i try to use throws FileNotFoundException but still i cant. pls help.. heres my code:

    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class POSfinals extends JFrame{
    
    		PrintWriter  outFile;
    	   JLabel Items, Qty, Sardines , Noodles, Milk, Coffee, amount, total, cash, change, remain ;
    	   JTextField sardinesTF, noodlesTF, milkTF, coffeeTF, sardinesT, milkT, noodlesT, coffeeT, totalT,cashT,changeT;
    	   JButton calculate,exit, money, printing;
    	   double totalTFT;
    		String str;
    		double sardinesC,sardinesTFT,noodlesC,noodlesTFT,milkC,milkTFT,coffeeC,coffeeTFT,ttotalTFT;
    		double ch, cashF;
    
    	public POSfinals(){
    		setTitle("POS # 1:\t Cashier: Anne");
    		setSize(600,500);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Items= new JLabel("ITEMS:",SwingConstants.CENTER);
    		Qty = new JLabel("QUANTITY:",SwingConstants.CENTER);
    		amount = new JLabel("Amount",SwingConstants.CENTER);
    		Sardines = new JLabel("SARDINES @Php 15:",SwingConstants.LEFT);
    		
    		Noodles = new JLabel("NOODLES @Php 7.50:",SwingConstants.LEFT);
    		
    		Milk = new JLabel("MILK @Php 20:",SwingConstants.LEFT);
    	
    		Coffee = new JLabel("COFFEE @Php 5:",SwingConstants.LEFT);
    		
    		total = new JLabel("Amount Due",SwingConstants.RIGHT);
    		cash = new JLabel("Cash Tendered",SwingConstants.RIGHT);
    		change = new JLabel("Change",SwingConstants.RIGHT);
    		remain = new JLabel("Total Change",SwingConstants.RIGHT);
    	
    	
    		sardinesTF = new JTextField(10);
    		noodlesTF = new JTextField(10);
    		milkTF = new JTextField(10);
    		coffeeTF = new JTextField(10);
    		sardinesT = new JTextField(10);
    		noodlesT = new JTextField(10);
    		milkT = new JTextField(10);
    		coffeeT = new JTextField(10);
    		cashT =  new JTextField(10);
    		changeT =  new JTextField(10);
    		totalT= new JTextField(10);
    		
    		calculate = new JButton("TOTAL");
    		exit = new JButton("EXIT");
    		money = new JButton("PAY");
    		printing = new JButton("Print");
    		
    		totalT.setEditable(false);
    		sardinesT.setEditable(false);
    		noodlesT.setEditable(false);
    		milkT.setEditable(false);
    		coffeeT.setEditable(false);
    		changeT.setEditable(false);
    	
    		
    		
    		ButtonHandler calc=new ButtonHandler();
    		exitHandler h=new exitHandler();
    		Change m = new Change();
    		printit p = new printit();
    		calculate.addActionListener(calc);
    		exit.addActionListener(h);
    		money.addActionListener(m);
    		printing.addActionListener(p);
    		
    		Container pane = getContentPane();
    		pane.setLayout(new GridLayout(9,3));
    		
    		pane.add(Items);
    		pane.add(Qty);
    		pane.add(amount);
    		
    		pane.add(Sardines);
    		pane.add(sardinesTF);
    		pane.add(sardinesT);
    		
    		pane.add(Noodles);
    		pane.add(noodlesTF);
    		pane.add(noodlesT);
    		
    		pane.add(Milk);
    		pane.add(milkTF);
    		pane.add(milkT);
    		
    		pane.add(Coffee);
    		pane.add(coffeeTF);
    		pane.add(coffeeT);
    		
    		
    		pane.add(calculate);
    		pane.add(total);
    	
    		pane.add(totalT);
    		pane.add(money);
    		pane.add(cash);
    		pane.add(cashT);
    		
    		pane.add(exit);
    		pane.add(remain);
    		pane.add(changeT);
    		pane.add(printing);
    	
    		
    		setVisible(true);
    	}
    	class ButtonHandler implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			
    			double sardinesC,sardinesTFT,noodlesC,noodlesTFT,milkC,milkTFT,coffeeC,coffeeTFT,totalTFT;
    			sardinesC=Double.parseDouble(sardinesTF.getText());
    			noodlesC=Double.parseDouble(noodlesTF.getText());
    			milkC=Double.parseDouble(milkTF.getText());
    			coffeeC=Double.parseDouble(coffeeTF.getText());
    			
    			sardinesTFT=((sardinesC)*15);
    			sardinesT.setText(""+sardinesTFT);
    			
    			noodlesTFT = noodlesC*7.50;
    			noodlesT.setText(""+noodlesTFT);
    			
    			milkTFT = milkC*20;
    			milkT.setText(""+milkTFT);
    			
    			coffeeTFT = coffeeC*5;
    			coffeeT.setText(""+coffeeTFT);
    			
    			totalTFT=(sardinesTFT+noodlesTFT+milkTFT+coffeeTFT);
    			totalT.setText(""+totalTFT);
    		}
    		
    		
    	}
    		class Change implements ActionListener {
    			public void actionPerformed(ActionEvent e){
    				double ch, cashF;
    				ch=Double.parseDouble(totalT.getText());
    				cashF=Double.parseDouble(cashT.getText());
    				
    				changeT.setText(""+(cashF-ch));
    		}
    	}
    			class exitHandler implements ActionListener{
    				public void actionPerformed(ActionEvent e){
    					
    				
    				System.exit(0);
    	
    }
    				}
    			class printit implements ActionListener{
    				public void actionPerformed(ActionEvent e){
    					 
    					POSfinals() throws FileNotFoundException{
    						PrintWriter outFile = new PrintWriter();
    					}
    
    					ch=Double.parseDouble(totalT.getText());
    					cashF=Double.parseDouble(cashT.getText());
    					
    					sardinesC=Double.parseDouble(sardinesTF.getText());
    					noodlesC=Double.parseDouble(noodlesTF.getText());
    					milkC=Double.parseDouble(milkTF.getText());
    					coffeeC=Double.parseDouble(coffeeTF.getText());
    					
    					sardinesTFT=((sardinesC)*15);
    					noodlesTFT = noodlesC*7.50;
    					milkTFT = milkC*20;
    					coffeeTFT = coffeeC*5;
    					ttotalTFT=(sardinesTFT+noodlesTFT+milkTFT+coffeeTFT);
    					
    		str=("sardines"+"  "+"@"+sardinesC+"      "+"Php"+sardinesTFT+"\n");
    		str+=("noodles"+"   "+"@"+noodlesC+"      "+"Php"+noodlesTFT+"\n");
    		str+=("milk"+"   "+"   @"+milkC+"      "+"Php"+milkTFT+"\n");
    		str+=("coffee"+"   "+" @"+coffeeC+"      "+"Php"+coffeeTFT+"\n");
    		str+=("\n");
    		str+=("Total Sales     "+""+"    Php"+ttotalTFT+"\n");
    		str+=("Cash           "+"     Php"+cashF+"\n");
    		str+=("Change         "+"     Php"+(cashF-ch)+"\n");
    		str+=("Vat 12%             "+(ttotalTFT*0.12)+"\n");
    
    	
    		
    		JOptionPane.showMessageDialog(null,str, "Official Receipt",JOptionPane.PLAIN_MESSAGE);
    }
    }
    			public static void main(String []args) throws FileNotFoundException{
    				POSfinals a = new POSfinals();
    				
    				
    	}
    	}
    Last edited by Fubarable; 05-14-2011 at 05:16 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. Could you please provide the necessary details that we need in order to be able to help you. For instance, what problems are you currently having with this code? Are there any error messages on compilation or exceptions thrown on running? And what specifically are you trying to do with this code? Anything you can tell us will help us better understand your problem.

    Also, I have edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    [code]
    Java Code:
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Best of luck, and again, welcome!

  3. #3
    pogz is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    sir, tanx for the welcome... that code is running well. but i wanna know instead appearing the output on a dialog box it will goes to a notepad.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pogz View Post
    sir, tanx for the welcome... that code is running well. but i wanna know instead appearing the output on a dialog box it will goes to a notepad.
    What do you man by "it will goes [sic] to a notepad"? Do you mean that you wish to write the text into a text file on disk? Or to have it display in an already running NotePad program?

  5. #5
    pogz is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    yes sir that is what i mean. it will write into a text file on disk.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yeah I see. This won't work:
    Java Code:
    PrintWriter outFile = new PrintWriter();
    and in fact it shouldn't even compile. Please check out the API for the PrintWriter class here: PrintWriter (Java Platform SE 6)

    It will tell you what constructors are valid (hint, create a File or even just a String for the file name and pass it into the constructor parameter).

  7. #7
    pogz is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    thank you so much for your effort sir, i really appreciate it. thanks again

Similar Threads

  1. modules in notepad
    By joypik in forum New To Java
    Replies: 6
    Last Post: 11-22-2010, 06:47 PM
  2. Notepad program
    By sridevisala in forum Java Software
    Replies: 1
    Last Post: 04-26-2010, 10:56 AM
  3. problem with notepad
    By munish in forum Advanced Java
    Replies: 7
    Last Post: 08-30-2009, 12:53 PM
  4. NotePad
    By whosadork in forum New To Java
    Replies: 10
    Last Post: 10-03-2008, 08:44 PM
  5. Notepad or IDE?
    By Eranga in forum Other IDEs
    Replies: 30
    Last Post: 09-22-2008, 05:20 AM

Posting Permissions

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