The Artima Developer Community
Sponsored Link

Java Answers Forum
please check this code

1 reply on 1 page. Most recent reply: Jul 3, 2003 2:08 PM by mmm

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
lazza

Posts: 3
Nickname: lazza
Registered: Jun, 2003

please check this code Posted: Jul 3, 2003 1:34 AM
Reply to this message Reply
Advertisement
I am trying to write from a textfield to a file. The code I have written appears to be ok with the compiler, but when I try and send it to a file I get an error message in the textfield, can sombody please help
String newdata;

TextField tfn;
Button b1;

private void writedata(String arg) throws IOException
{
DataOutputStream os;
os=new DataOutputStream(new FileOutputStream("c:\\info.txt"));
os.writeBytes(arg);
os.close();
}




public void init()
{
tfn=new TextField(30);
add (tfn);
b1=new Button("Write text");
add(b1);

}
public boolean action(Event evt, Object arg)
{

if(evt.target==b1)
{

newdata=tfn.getText();
try
{
writedata(newdata);
}
catch(java.lang.Exception e) {tfn.setText("error while writing");}
repaint();

}
return true;
}


mmm

Posts: 2
Nickname: m3
Registered: Jul, 2003

Re: please check this code Posted: Jul 3, 2003 2:08 PM
Reply to this message Reply
You might want to add an actionlistener to your button.

So modify your init() to the following:

public void init()
{
tfn=new TextField(30);
add (tfn);
b1=new Button("Write text");

// modification
b1.addActionListener(this);
// modification

add(b1);

}

Flat View: This topic has 1 reply on 1 page
Topic: AdaptiveClassLoader from Apache Previous Topic   Next Topic Topic: create many variably named components?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use