new to applets.. how do I do this?
Hello everyone.
I'm a student studying programming. So far I've done a lot of small java applications but we haven't yet learned how to deploy them or make web applets / .jar files.
I think I know how to make .jar files now but I was wondering if there is such a thing as a .jar frontend that makes it easier and more user-friendly ?
Also.. the main question I wanted to ask is.. how do I make something like what we see on this website.
http://www.chessanytime.com/playGuest.php
basically.. it's just a small applet that launches a window when you press on connection. I would like to reproduce this effect. I am making an applet that I don't want to run IN a webpage.. I want for the user to have to click 'launch' and then a window launches that can be resized and moved around.. with menus and all that bang.
is there a tutorial somewhere that explains how to make this?
and lastly.. Right now I'm making my application as I normally do.. meaning that all my swing classes extend JFrame. Is this going to work for a web applet ?
I know these questions must sound awfully noobish to you.. but I am learning as I go along and I won't ask the same thing twice I promise ;-)
thanks for the help guys
[1269 byte] By [
vesper8a] at [2007-10-3 4:11:24]

I want for the user to have to click 'launch' and then a window launches that can be resized and moved around.. with menus and all that bang.
You can have menus and everything directly in an spplet. Or you could implement an applet which just contains a button, which launches a JFrame.
Right now I'm making my application as I normally do.. meaning that all my swing classes extend JFrame. Is this going to work for a web applet ?
It'll work as normal but it's very bad practice (though seemingly very common) to subclass JFrame. If you really need custom components (and you don't need them as often as you'd think) it's generally best to use JComponent or even JPanel - the most generic of components. Why? Because if you've got a specific JFrame you can't display that inside anything else. If the root of your UI is a JComponent you can display it in a JFrame, a JApplet, a Window, even within a whole other application.
But you can and should build UIs from generic components - your app-specific functionality should be more in model classes than in UI classes - the only classes I write which extend Swing components are generic ones, it's very rare to need an app-specific one.
Do the tutorial book examples put functionality in JFrame subclasses? It seems to be a common beginners thing on these forums and it's a terrible way of going about writing applications. After one particularly alarming post a week or so ago I'm getting a bit concerned about whether half the books out there are just teaching everyone bad practice.
well.. let me be the first to say that if the teachers teach us bad practice.. it certainly is refreshing and amazing! for lack of a better word, to hear it right from the words of those who've learned.. the right way shall we say ;-)
in other words.. thanks for that awesome insightful reply! I will not only never extend a jframe again unless i absolutely need to, because i just understood the practicality of making your apps ready to be used.. anywhere. but you've also made me think about the way i divide my work into classes. As it is.. I do use a lot of jpanel classes but i do tend to code large bulks of my app code inside of them too.. perhaps I should rethink this in the future and leave the gui out of the app
but back to my dilema now though. I would indeed like to make a small applet that launches a jframe.. just like on that chess website. I could experiment with the applet thing too but the movable on-top window just seems more practical in my mind at the moment.
could you be so kind to point me in the right direction on how to do that then? an helpful link would be as fine as a few more minutes of your well learned insights ;-)
thanks!
Ok, here is how I would structure it. Let's take the example of a game.
First we'll take the button and launching a frame from it. This is pretty straightforward. The 'basic' way of doing it is to use an ActionListener but it's good to get into the habit of using Actions, for a number of reasons. Firstly they associate details like the label and/or icon, making widget construction quicker and meaning changing those details is much easier. I use my own Action class which does some neat stuff with threading and resource loading but we don't need to worry about that right now. So, this will launch your new frame...
public class Launcher extends JApplet
{
public void init()
{
add(new JButton(new Launch()));
}
private final class Launch extends AbstractAction
{
public Launch()
{
putValue(Action.NAME, "Launch");
}
public void actionPerformed(ActionEvent e)
{
// Disable the action here to avoid multiple instances -
// note that because we constructed the button with
// this action, the button is disabled too.
setEnabled(false);
// Create and show the frame, with a label in it for now.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane.add(new JLabel("Hello World", JLabel.CENTER));
frame.pack();
frame.setLoactionRelativeTo(null);
frame.setVisible(true);
}
}
}
Simple enough so far. Now let's put something in the frame - the UI for a game. Now, going back to what I said before, it's best not to cook the 'business' logic into the UI components. So, ideally we'd create our game separately and then create a UI which uses it.
public class Launcher extends JApplet
{
private final Game game = new Game();
public void init()
{
add(new JButton(new Launch()));
}
private JComponent createUI(Game game)
{
// Create and return the UI here.
}
private final class Launch extends AbstractAction
{
public Launch()
{
putValue(Action.NAME, "Launch");
}
public void actionPerformed(ActionEvent e)
{
setEnabled(false);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(createUI(game));
frame.pack();
frame.setLoactionRelativeTo(null);
frame.setVisible(true);
}
}
}
You could do that slightly differently - you could have a specific GameUI class which extends JComponent, you could have a GameUIFactory class with a createUI() method, you could even have a getUI() method on the Game itself (some would argue that's worse practice, others would argue it's better practice - depends on context really).
thanks once again for your reply.
With it I managed to get my jframe to launch from the launcher class. And I've tested it using Jdeveloper's applet viewer and that much works.
Now the next step would be to test it on an actual webpage. Currently it just launches it using the .java files.
I guess the next part is to put all my project into a jar file ?
and once the .jar file is made.. what would my applet launch code look like in the html ?
currently it's :
<APPLET CODE="package_admin.Launcher" ARCHIVE="mysql-connector-java-3.1.13-bin.jar,jdev-rt.jar" HEIGHT="200" WIDTH="200" ALIGN="bottom">This browser does not support Applets.</APPLET>
can you please confirm if all I have to do now is .jar up my project, and then change the applet code. I assume I must put the project .jar in the ARCHIVE= and the CODE= won't be neeed anymore. Is that right ?