Archived

This topic is now archived and is closed to further replies.

kappa

two questions about java

Recommended Posts

The first thing that I have not seem to find is how I would get the state of a button (I whant something to happen when I click on it)
Label name	 	= new Label("Tuturial: ");
	TextField web	= new TextField(25);
	Label address	= new Label("Address: ");
	TextField adr	= new TextField(25);
	Button OK       = new Button("  OK  "); 

	
	public void init()
	{
		add(name);
		add(web);
		add(address);
		add(adr); 
		add(OK);
	}
Something like
if(OK.thisButtonIsPressedByUser() == true)
{
  //...
}
 
The second question I have is how I would add text in the middle of a text file. I have this code:
FileWriter     fileIn;
FileReader     fileOut;
BufferedReader buff;

while(!eof)
				{
					sLine = buff.readLine();
					if(sLine == null)
					{

						fileIn.write(sLink, 0, sLink.length());

						fileIn.write(sLink, 0, sLink.length());

						eof = true;
					}
				}

This would add it att the end of the file,but is there a way that does the samething as in c++ where you can change the pointer and type from there? Thank you for your time. help with this would be helpful

Share this post


Link to post
Share on other sites
In regards to your first question, read up on Listener''s, essentially you set a gui object to call a listener when an event happens (i.e. mouse click/keyboard etc), and the listener (essentially a method) does something in response to the event.

The second question is rather vague...
when you say the middle of a text file, what do you mean?

elaborate further, and someone will help.

Share this post


Link to post
Share on other sites
Java calls clicking on buttons (and scrolling, and check a check box, etc.) events. So you need event listeners that are registered to a certain GUI component.

Sun keeps up a good library of java tutorials:
http://java.sun.com/docs/books/tutorial/

And more specifically what you're looking for:
http://java.sun.com/docs/books/tutorial/uiswing/index.html
Check out the basic GUI applications section for good button clicking fun.

Also, I highly recommomend you use Swing instead of the Abstract Windowing Toolkit. Both are still used, but the AWT was found difficult to work with, and Swing is the "improved version." The Swing classes usually are named just like the AWT ones, but with a prefixed 'J.'

Button is JButton, TextField is JTextField, and Label is JLabel. Instead of importing java.awt.*, you'd import javax.swing.*.

quote:
Original post by kappa
  
FileWriter fileIn;
FileReader fileOut;
BufferedReader buff;

while(!eof) {
sLine = buff.readLine();
if(sLine == null)
{
fileIn.write(sLink, 0, sLink.length());
fileIn.write(sLink, 0, sLink.length());
eof = true;
}
}



A few things about this code: BufferedReaders are usually easier to work with than FileReaders, so one essentially wraps a FileReader in a Buffered Reader. Also, as a matter of tradition, fileIn would refer to a FileReader (hence, inputting data) and fileOut to the writer. It's a good idea if you've got both in & out file IO, that you use a BufferedWriter with the FileWriter as well. This would change your code to this:


import java.io.*;
...
BufferedReader buff = new BufferedReader( new FileReader (infile) );
BufferedWriter writ = new BufferedWriter( new FileWriter (outfile) );


As I'm not sure what sLink is in your program, I can't tell you if there's an easier way to write that out. But check out the Java API at java.sun.com. They've got good information with each of the classes on how to use them best and what the related classes are.

Good Luck,
Katy

[edited by - bypedd on April 20, 2004 12:07:14 PM]

Share this post


Link to post
Share on other sites
ok thank you, I have stumbled on another problem though. I get this error when compiling:
Kind Status Priority Description Resource In Folder Location
Error Class must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) WebApp.java WebApplication line 22

it''s on the public class WebApp extends etc. line and I cannot figure out what is causing it.

here is the complete source to my program in development:

public class WebApp extends JFrame implements ActionListener
{
JFrame border = new JFrame();
JLabel name = new JLabel("Tuturial: ");
JTextField web = new JTextField(25);
JLabel address = new JLabel("Address: ");
JTextField adr = new JTextField(25);
JButton OK = new JButton("OK");


public WebApp()
{
OK.addActionListener(this);
JPanel panel = new JPanel();
panel.add(name);
panel.add(web);
panel.add(address);
panel.add(adr);
panel.add(OK);
}

public static void main(String [] argv)
{

try
{
FileWriter fileIn;
FileReader fileOut;
BufferedReader buff;

String sLink = "www.wwiionine.com";
String sName = "null";
String sLine;

fileIn = new FileWriter("tuturial.html", true);
fileOut = new FileReader("tuturial.html");
buff = new BufferedReader(fileOut);

boolean eof = false;


//sLink = adr.getText();

//sName = web.getText();

while(!eof)
{
sLine = buff.readLine();
if(sLine == null)
{
fileIn.write("\n");
fileIn.write("<a href=\"",0 , 8);
fileIn.write(sLink, 0, sLink.length());
fileIn.write(''>'');
fileIn.write(sLink, 0, sLink.length());
fileIn.write("
</a>");
eof = true;
}
}




fileIn.close();
}catch(IOException e){}
}


}


Share this post


Link to post
Share on other sites
Since you have an action listener you need to write the code to handle the action that gets performed. Like this


public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="OK")
{
...do button stuff...
}
}


There are other ways to do this but this is how I did it in my last Java project.

"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

Share this post


Link to post
Share on other sites
ahh ok, so stupid question how would I now make it so that it can be runned as an applet on a website?

I cannot seem to extend both JFrame and java.applet.Applet at once.

[edited by - kappa on April 20, 2004 4:59:37 PM]

Share this post


Link to post
Share on other sites