how can i save file inside it please.
please if you can tell me what to do to let the button "Save" save any file ?
import java.awt.*;
import java.awt.event.*;
publicclass Layoutsextends Frame{
Button btn1;
Label lbl1;
TextField txf1;
Panel pnl1;
Panel pnl2;
Layouts(){
super.setTitle("Nested Layouts");
this.setSize(360, 180);
this.setLayout(new BorderLayout());
pnl1 =new Panel();
pnl1.setLayout(new FlowLayout());
txf1 =new TextField("Edit this text...");
pnl1.add(txf1);
pnl2 =new Panel();
pnl2.setLayout(new FlowLayout());
btn1 =new Button("Save");
pnl2.add(btn1);
this.add(pnl1, BorderLayout.CENTER);
this.add(pnl2, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(1);
}
});
}
publicstaticvoid main(String[] args){
new Layouts().setVisible(true);
}
}
[1999 byte] By [
Alenaa] at [2007-11-27 7:04:35]

Are you sure you want to use the old AWT and not Swing?Odd. In your other thread, you try in Swing...Message was edited by: Hippolyte
Just like in [url=http://forum.java.sun.com/thread.jspa?threadID=5182229]your other thread[/url], you need to do the following:
1. Add a new block to your actionPerformed's if block for the case of btnSave.
2. In that block, call a new method, saveFile(), or something similar.
3 Make a saveFile() (or whatever) method which opens up a BufferedWriter that writes the contents of the outArea out to a text file, then closes the file.
You're unfortunately going to have to resort to reading the online documentation, instead of having code sent to you on a platter. It's a far more elementary thing to open up and write a text file than it is to create the button that does it.
Do yourself a favor and read.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileWriter.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedWriter.html
http://java.sun.com/docs/books/tutorial/essential/io/
Your button needs an ActionListener
thx for help.what i want to do is to create a bgutton save in calculator.and save something with text.so i tried but does not work.i found this one simple and i try to add save but also nothing happen for it.i ll read what this cool person wrote me.thx
Alenaa at 2007-7-12 18:55:53 >

Show us your code that doesn't work, and we'll help out the best we can. We just have to have specific questions about what isn't working out.
How to write to a file: http://www.exampledepot.com/egs/java.io/WriteToFile.html
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math.*;
import java.math.BigInteger;
public class Calculator extends JFrame implements ActionListener{
JPanel lM1;
JLabel lN1, lN2,lN3;
JTextField tN1, tN2,tN3;
JButton btnAdd,btnSub,btnMul,btnDiv,btnClear,btnSave; JTextArea outArea;
JTextField txf1;
Panel pl0=new Panel();
Panel pl12=new Panel();
Panel pl13=new Panel();
Label lb5=new Label();
TextField tx1=new TextField(10);
TextField tx=new TextField(10);
Calculator(){
Box box=Box.createVerticalBox();
// input
lN1=new JLabel("Write first number"); box.add(lN1);
tN1=new JTextField(100); tN1.setEditable(true); box.add(tN1);
lN2=new JLabel("write second number"); box.add(lN2);
tN2=new JTextField(100); tN2.setEditable(true); box.add(tN2);
lN3=new JLabel("Info Label"); box.add(lN3);
//btnSave.addActionListener(new listner());
lb5.setText("write the name of file");
pl0.setLayout(new GridLayout(9,2,0,0));
pl13.add(tx1);
pl13.add(btnSave);
pl13.add(lb5);
pl12.add(pl13,BorderLayout.SOUTH);
this.add(pl12,BorderLayout.SOUTH);
// tN3=new JTextField("write the name of the file");box.add(tN3);
// buttons
btnAdd=new JButton("+"); btnAdd.addActionListener(this); box.add(btnAdd);
// setPreferredSize();
btnSub=new JButton("-"); btnSub.addActionListener(this); box.add(btnSub);
btnMul=new JButton("*"); btnMul.addActionListener(this); box.add(btnMul);
btnDiv=new JButton("/"); btnDiv.addActionListener(this); box.add(btnDiv);
btnClear=new JButton("Clear"); btnClear.addActionListener(this); box.add(btnClear);
btnSave=new JButton("Save"); btnSave.addActionListener(this); box.add(btnSave);
//THEMOST IMPORATNT THING IS HERE TO LET THIS BUTTON WORK AND CAN SAVE TEXT FILE.
// txf1 =new JTextField("Enter the name of the file here");
// lM1.add(txf1);
// Output
String desc="This is a simple integer calculator\n";
outArea=new JTextArea(desc,30,60);//
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(outArea);
box.add(scrollPane);
Container c=getContentPane();
c.add(box);
c.setBackground(Color.green);
setTitle("Calculator");
setSize(600,375);
setVisible(true);
}
private void showStatus(String status) {
lN3.setText(status);
}
public void actionPerformed(ActionEvent actionEvent){
if (actionEvent.getSource() == btnSave) {
// outArea.selectAll(); outArea.Save();
// HERE WHAT I NEED TO ADD ?
}
if(actionEvent.getSource()==btnClear){
outArea.selectAll(); outArea.cut();
} else {
String sN1=tN1.getText(), sN2=tN2.getText(), sOp="";
BigInteger bN1=new BigInteger(sN1);
BigInteger bN2=new BigInteger(sN2);
BigInteger bRes=BigInteger.ZERO, bResDiv[]={BigInteger.ZERO,BigInteger.ZERO};
BigInteger bRemainder=BigInteger.ZERO;
if(actionEvent.getSource()==btnAdd){bRes=bN1.add(bN2); sOp=" + ";}
if(actionEvent.getSource()==btnSub){bRes=bN1.subtract(bN2); sOp=" - ";}
if(actionEvent.getSource()==btnMul){bRes=bN1.multiply(bN2); sOp=" * ";}
if(actionEvent.getSource()==btnDiv){sOp=" / ";
if(bN2.signum()!=0)bResDiv=bN1.divideAndRemainder(bN2);//Math.signum() tells you what sign a number is....
//returns zero if the argument is zero, 1.0 if the argument is greater than zero,
//-1.0 if the argument is less than zero
bRes=bResDiv[0]; bRemainder=bResDiv[1];
}
if(sOp.equals(" / ") && bN2.signum()==0){
showStatus("Cannot divide by zero");
} else {
outArea.append(bN1.toString() + sOp+bN2.toString() + " = " + bRes.toString() + "\n");
if(actionEvent.getSource()==btnDiv)outArea.append("Remainder = "+bRemainder.toString()+"\n");
showStatus("Operation OK.");
}
}
}
public static void main(String[] args) {
new Calculator().setVisible(true);
}
}
Alenaa at 2007-7-12 18:55:53 >

this might be a bit overkill for this, but ive found this hack useful for handling text files. http://www.doesthatevencompile.com/current-projects/code-sniplets/ASCIIFile.htm
I'm not a big fan of giving the JFrame subclass an actionPerformed methodthat has to do triage on a bunch of unrelated buttons. Why not write a SaveListener class?
can you type me small example how it can be this class ?SaveListener class.it must be near the button save or near the public void actionPerformed(ActionEvent actionEvent){ ?
Alenaa at 2007-7-12 18:55:53 >

> can you type me small example how it can be this class ?> SaveListener class.it must be near the button save or> near the public void actionPerformed(ActionEvent> actionEvent){ ?What do you mean by "near"?
i mean if i need to create this class after the button save or after this code
public void actionPerformed(ActionEvent actionEvent){
if (actionEvent.getSource() == btnSave) {
// outArea.selectAll(); outArea.Save();
}
Alenaa at 2007-7-12 18:55:53 >

In Java, there's a lot of latitude in the ordering of code. For example,methods don't have to be listed in a certain order.I think you are actually alluding to nested or inner classes?
Hippolyte look i tried to many things and it does not work correctly .can you help me just how to create this thing Please.
Alenaa at 2007-7-12 18:55:53 >

You define the class in the usual way:
import java.awt.event.*;
import javax.swing.*;
public class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
}
}
To use it, you instantiate the class and call addActionListener:
SaveListener listener = new SaveListener();
btn.addActionListener(listener);
Now, I'm not clear about the details of what you are doing, but you
mentioned something about a text area. If you are saving the text in
a text area, you could make that a field in the listener:
import java.awt.event.*;
import javax.swing.*;
public class SaveListener implements ActionListener {
private JTextArea textArea;
public SaveListener(JTextArea textArea) {
this.textArea = textArea;
}
public void actionPerformed(ActionEvent evt) {
}
}
Hipp,Do you write an action class for every component in your GUIs?I usually write a single action listener class that "triages"all the components in a container.
Hippolyte
thank you for help.
i still have oe thing to ask
look when u runn the program and i press the button save i see in output.the number wich i want calculate.why ?
if i multiply 2 *2 and i press the button "SAVE" i receive 22 !!!
you have any idea why?
if you run the program you will see it,
Alenaa at 2007-7-21 22:12:21 >

> Hipp,
> Do you write an action class for every component in your GUIs?
> I usually write a single action listener class that "triages"
> all the components in a container.
I usually subclass javax.swing.AbstractAction, which means writing
one class per "action". Even if I am directly implementing handler interfaces,
I try to write small classes that are cohesive. Those switch-board actionPerformed
methods make my face twitch. It brings back bad memories of MFC.
> MFC.M'er F'n Charlie, that's what we called him back during the war... but I don't like to talk about it -- not many of us do...*stares off into the distance, face twitching...*
> Hippolyte
> thank you for help.
> i still have oe thing to ask
> look when u runn the program and i press the button
> save i see in output.the number wich i want
> calculate.why ?
> if i multiply 2 *2 and i press the button "SAVE" i
> receive 22 !!!
> you have any idea why?
> if you run the program you will see it,
You have the wrong listener listening to your save button.
You should only have the SaveListener listening to that button.
I want ask when i save the file where can i see it ?like in which directory ?
Alenaa at 2007-7-21 22:12:21 >

> I want ask when i save the file where can i see it ?
> like in which directory ?
If you just used a filename like "testfile.txt", then it's probably in
System.getProperty("user.dir")
which is likely the codebase where you're running the code from.
^^^ What he said.Message was edited by: Hippolyte
> I usually subclass javax.swing.AbstractAction, which
> means writing
> one class per "action". Even if I am directly
> implementing handler interfaces,
> I try to write small classes that are cohesive. Those
> switch-board actionPerformed
> methods make my face twitch. It brings back bad
> memories of MFC.
i looked at AbstractAction, and it didnt give me any insite into how you would subclass this to make a 1 class per action.
i have always hated the switching in the action, so please tell me what you do with this AbstractAction to help you accomplish what you have stated.
please
> i looked at AbstractAction, and it didnt give me any
> insite into how you would subclass this to make a 1
> class per action.
You just do it. Here's a demo:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class Images {
public static final String PATH =
"http://java.sun.com/developer/techDocs/hi/repository/graphicsRepository/toolbarButtonGraphics/general/";
}
class CutAction extends AbstractAction {
public CutAction() {
super("Cut");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
putValue(MNEMONIC_KEY, KeyEvent.VK_C);
putValue(SHORT_DESCRIPTION, "this is the tool tip text for cut");
try {
putValue(SMALL_ICON, new ImageIcon(new URL(Images.PATH + "Cut16.gif")));
putValue(LARGE_ICON_KEY, new ImageIcon(new URL(Images.PATH + "Cut24.gif")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent evt) {
System.out.println("do cut");
}
}
class CopyAction extends AbstractAction {
public CopyAction() {
super("Copy");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
putValue(MNEMONIC_KEY, KeyEvent.VK_O);
putValue(SHORT_DESCRIPTION, "this is the tool tip text for copy");
try {
putValue(SMALL_ICON, new ImageIcon(new URL(Images.PATH + "Copy16.gif")));
putValue(LARGE_ICON_KEY, new ImageIcon(new URL(Images.PATH + "Copy24.gif")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent evt) {
System.out.println("do copy");
}
}
public class ActionExample implements Runnable {
public void run() {
Action cut = new CutAction();
Action copy = new CopyAction();
JMenuBar mb = new JMenuBar();
JMenu edit = new JMenu("Edit");
edit.add(cut);
edit.add(copy);
mb.add(edit);
JToolBar tb = new JToolBar();
tb.add(cut);
tb.add(copy);
JFrame f = new JFrame("ActionExample");
f.setJMenuBar(mb);
f.getContentPane().add(tb, BorderLayout.NORTH);
f.getContentPane().add(new JScrollPane(new JTextArea(20,60)));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new ActionExample());
}
}
>
> i have always hated the switching in the action, so
> please tell me what you do with this AbstractAction
> to help you accomplish what you have stated.
There's nothing special about Action/AbstractAction.
They just have properties like those managed by putValue/getValue
that you can't share across distinct buttons, for example.
But even if you are directly implementing ActionListener, write the
code you want to write -- don't toss everything into one listeners if
you don't want to do that.
