Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Applet to communicate with the Web server

 
David M Fairchild
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code listing below the getFile() function runs when called by init() but not when called by actionPerformed(). I'm strugling trying to figure out how to get an applet to communicate with the web server as the front end of a 3 tier application. Eventually, I will want to query the web server based on some user event and display the returned data.

Why Doesn't the getFile() call work in actionPerformed()? The text "Button Pushed" shows up in the status bar so I know actionPerformed() was called!

import java.io.* ;
import java.net.* ;
import java.applet.* ;
import java.awt.* ;
import java.awt.event.*;

public class HelloServerFile extends Applet implements ActionListener {
Panel pnlCenter = new Panel(new GridLayout(10,1));
Label lblStatus = new Label("Status:");

public void init() {
Panel pnlNorth = new Panel(new FlowLayout());
Panel pnlSouth = new Panel(new FlowLayout());
Button myButton = new Button("Get File");

setLayout( new BorderLayout() );

myButton.addActionListener(this);
pnlNorth.add(new Label("Version 1.07"));
pnlNorth.add(myButton);
pnlNorth.setBackground(Color.CYAN);

lblStatus.setAlignment(Label.LEFT);
pnlSouth.add(lblStatus);
pnlSouth.setBackground(Color.CYAN);

add(pnlNorth, BorderLayout.NORTH);
add(pnlCenter, BorderLayout.CENTER);
add(pnlSouth, BorderLayout.SOUTH);

getFile();
} // close init

private void getFile() {
String line = new String("Button Pressed.");

try{
URL textURL = new URL( getCodeBase() , "test.txt" );
BufferedReader theStream =
new BufferedReader(new InputStreamReader(textURL.openStream()));

while ( ( line = theStream.readLine() ) != null ) {
pnlCenter.add( new Label( line, Label.CENTER ) );
} // close while loop

theStream.close();
} // close try
catch ( Exception e ) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent ae) {
lblStatus.setText("Button Pushed");

getFile();
} // close actionPerformed
} // close applet
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is working, you just can't see the new data. pnlCenter can only hold 10 elements. Everything else is discarded. Try using a TextArea instead of a panel and you'll see data get appended with every button press.
 
David M Fairchild
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer Joe, but I've already tried putting a statment in actionPerformed() to clear the center panel before calling getData(). Nothing gets written back.
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took your code, used a TextArea and it worked fine. Maybe you should try printing out the data as you read it in from the URL.
Making a container dynamic involves a bit more work than "clearing" it. At the very least you have to invoke invalidate() on the container to mark it as needing the layout manager applied, then after altering it invoke validate() to run the layout manager.
 
David M Fairchild
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe,

I appologize for not attempting your suggestion before I replied before.

As you probably have noticed, I'm not a very accomplished Java programmer. From your comment, I take it one should avoid 'writing' to containers. That's not what I actually did, but my previous example might have worked if I had populated the grid with labels then emptied the labels and re-wrote to the label rather than attempt to empty the grid and then filling it with labels again.

It seems this could be handy if you had a multi-form application and wanted to maintain a similar look and feel to all of your forms. You could invalidate the panel(s) then fill them with new labels/text boxes from a list in a database. That way you could alter your form by changing the contents of the database table used to populate the form.... could be handy.

Thanks again.
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David M Fairchild:

As you probably have noticed, I'm not a very accomplished Java programmer.

hey, we're all learning, just at different levels.


From your comment, I take it one should avoid 'writing' to containers.

You CAN do it but there's more to it than you were thinking. If you want to go deeper, the Java Tutorial has two chapters: Creating a GUI with Swing and 2D Graphics which you should run through so you have a better idea as to what process are going on and how things get written to the screen.


It seems this could be handy if you had a multi-form application and wanted to maintain a similar look and feel to all of your forms

Sure. You've figured out two parts of the Model-View-Controller (MVC) paradigm. One separates the responsability of containing the data to the model, displaying the data to the view and working with the data to the controller, then all these pieces collaborate by passing messages back and forth. Swing is based on MVC so it's easier to do what you describe than with AWT. Again, take a look at the tutorials and I think everything will fall into place.
[ July 22, 2005: Message edited by: Joe Ess ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic