The Deep End

go to! email!

Tuesday, March 08, 2005

Java Apps: Events, Button-Clicks, and How to Make Your Program Work

Although adding buttons is fun and looks cool, it's a whole lot better when you can click on them and have things actually happen.

When the user clicks on a button (or moves a mouse or presses a key) it's called an Event. The general terminology is that an event gets "fired" (when you click that button) and to "catch" the event, you'll need to do a lot of stuff.

Firstly, your program will have to implement the ActionListener interface--this means that you have to have an actionPerformed() method somewhere in the class that is your app. The parameter to actionPerformed is an ActionEvent which is basically the event that's just occurred.

If you just have one button, then the actionPerformed method is all you'll need. Just type in the code that you want "performed" when the event fires--like
System.out.println("I've just been pressed!");

However, there are a few more things that you have to do:

1. Don't forget to put implements ActionListener next to your class name, and import the package java.awt.event.*.
2. When you create a button that you want actionPerformed to trap the events for, write this:
JButton btn = new JButton("Click Me, I'm Fatal");
This makes sense because you want to be "listening" for an event, and because this (which is your class) implements ActionListener, it is an ActionListener. It's also where the actionPerformed method is; it's all tied together!

If you have more than one button that you want to handle events for, use addActionListener(this) as usual, but in the actionPerformed method you'll need to write something like:
public void actionPerformed(ActionEvent e){
String action = e.getActionCommand();
if(action.equals("Click Me, I'm Fatal")){
else if(action.equals("I'm Another Button!")){
getActionCommand() returns the name of the Component.

There are lots of other events other than ActionEvent--like KeyEvent or MouseEvent, but they're pretty easy to understand once you've gotten ActionEvent. You'll have to add KeyListeners, MouseListeners, etc. and implement the MouseListener/KeyListener interfaces, but there are Java Tutorials for that.

Thursday, March 03, 2005

Swing Java Apps Tutorial

Java applications (not Java applets, which are a lot different) begin with a JFrame--although the best place to begin is here, at the Java Tutorial. In the tutorial, they use threads, which may make the program safe but is also very confusing and not fun if you have to hard-code the entire thing anyways.

frame.pack() shrinks the app down to the size of the components--so you don't have a lot of extra space. setVisible(true) makes it visible...

The JFrame is the main window--this kind of makes sense because it's like a picture frame into which you add contents You do this using frame.getContentPane().add(your Component) You can read more about JFrame here.

After you've got a JFrame, you can add all sorts of things. Reading the Swing Tutorial would be a lot better than me putting up everything I know (which isn't a lot) but a few spiffy items to get started with are:


Organizing Your App

To organize your gui (graphical user interface), if you're hard-coding everything, then you'll need to use JPanels. JPanels are like mini-frames in that they start out empty and you can add stuff (Components) like JTextFields and JButtons to them. This makes your app more organized in that you can have inputPanel, which contains all the input Components (like JTextFields and JButtons), and labelPanel which would be the labels that you'll place above the input Components.

Positioning Stuff

To do things like place one JPanel above another, you'll need to use a Layout Manager. A great Java Tutorial is located here. By default, every Container (like a JPanel, JFrame, or anything that has an add() method) uses FlowLayout, which, as stated in the Java Tutorial, basically just adds things in a row.

A lot of the other layouts are really confusing, so the main one that I use is BorderLayout. For how to use BorderLayout, refer to the Java Tutorial segment "How to Use BorderLayout".

Hope that helps!

Tuesday, March 01, 2005

Thunderbird Extensions: nsIMsgComposeParams and nsIMsgCompFields

nsIMsgComposeParams specifies the formatting of the Compose window that is opened when you call

OpenComposeWindowWithParams(null, params); //params is a nsIMsgComposeParams

In addition to type and format, which are explained in other posts, the other field that I saw used was composeFields, which is a nsIMsgCompFields. nsIMsgCompFields is basically the headers of the new message, plus the body. Just use the self-explanatory properties.

Thunderbird Extensions: nsIMsgCompType and nsIMsgCompFormat

I didn't include this in the post "How to Open a Compose Window" because I'm not 100% sure what these components do. I have played around with them some, so the theory is this:

nsIMsgCompType dictates which type of compose window is going to be opened. How exactly this will change the look of the message, I don't know.

nsIMsgCompFormat formats, I think, the text that will make up the body of your message. For example, if you use PlainText on text that has HTML in it, you'll get tags.

In Relation to Opening a Compose Window

The component nsIMsgComposeParams has type and format properties--use nsIMsgCompType and nsIMsgCompFormat.

Obviously, if you write

var params = Components.classes[';1'].
var msgComposeFormat = Components.interfaces.nsIMsgCompFormat;
params.format = msgComposeFormat.PlainText;

and later set body to text that uses HTML, then you'll see tags.