Results 1 to 7 of 7
- 05-14-2011, 04:15 PM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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 04:16 PM. Reason: code tags added
-
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]
[/code]Java Code:// your code goes here // notice how the top and bottom tags are different
Best of luck, and again, welcome!
- 05-14-2011, 04:45 PM #3
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
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.
-
- 05-14-2011, 04:53 PM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
yes sir that is what i mean. it will write into a text file on disk.
-
Yeah I see. This won't work:
and in fact it shouldn't even compile. Please check out the API for the PrintWriter class here: PrintWriter (Java Platform SE 6)Java Code:PrintWriter outFile = new PrintWriter();
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).
- 05-14-2011, 05:15 PM #7
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
modules in notepad
By joypik in forum New To JavaReplies: 6Last Post: 11-22-2010, 05:47 PM -
Notepad program
By sridevisala in forum Java SoftwareReplies: 1Last Post: 04-26-2010, 09:56 AM -
problem with notepad
By munish in forum Advanced JavaReplies: 7Last Post: 08-30-2009, 11:53 AM -
NotePad
By whosadork in forum New To JavaReplies: 10Last Post: 10-03-2008, 07:44 PM -
Notepad or IDE?
By Eranga in forum Other IDEsReplies: 30Last Post: 09-22-2008, 04:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks