How to add actionlistener to a JAR

Hi and thanks in advance to all people who contribute.

I want to package a gui component so that I can use it within Netbeans. I have managed to do this with the actionlisteners and anction performed methods within the JAR package.

How could I have an actionlistener detect the events occuring within the JAR package?

In order to use this code you will need to download and apply this to your class path:

http://download.java.net/javadesktop/swinglabs/releases/weekly/week-5-2007-01-30/swingx/swingx-2007_01_30-bin.zip

The download page is:

http://swinglabs.org/downloads.jsp

However, you shouldn't have to run the code to know what to do.

There are two seperate programs below. The first is a self contained program which can be executed independly of anyhting else.

The second is what I used to make the JAR, and currently contains the actionlisteners.

Thanks Bemo

/************************First program */

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JPanel;

import org.jdesktop.swingx.JXDatePicker;

import org.jdesktop.swingx.JXHyperlink;

public class MyDateTime extends JFrame implements ActionListener {

private MyJXDatePicker datePicker;

public MyDateTime() {

getContentPane().add(getPanel(),BorderLayout.NORTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setVisible(true);

}

public JPanel getPanel() {

JPanel panel = new JPanel();

datePicker = new MyJXDatePicker();

panel.add(datePicker);

datePicker.addActionListener(this);

return panel;

}

public static void main(String[] args) {

new MyDateTime();

}

public void actionPerformed(ActionEvent arg0) {

System.out.println("The date is: " + datePicker.getDate());

}

}

class MyJXDatePicker extends JXDatePicker {

private String[] dateStr = null;

private DateFormat df;

private SimpleDateFormat df2;

private JPanel newLinkPanel;

public MyJXDatePicker() {

df = DateFormat.getDateInstance(DateFormat.LONG);

df2 = new SimpleDateFormat("dd/MM/yyyy");

dateStr = new String[] { "dd/MM/yyyy" };

LinkPanel();

setFormats(dateStr);

setLinkPanel(newLinkPanel);

setDate(new Date());

}

private void LinkPanel() {

newLinkPanel = new JPanel(new BorderLayout());

JXHyperlink todayLink = new JXHyperlink();

todayLink.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

getEditor().setText(df2.format((new Date())));

setDate(new Date());

getMonthView().getParent().setVisible(false);

fireActionPerformed();

}

});

todayLink.setText("Today: " + df.format(new Date()));

newLinkPanel.add(todayLink, BorderLayout.CENTER);

}

}

/************************Second program */

package datePicker;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JPanel;

import org.jdesktop.swingx.JXDatePicker;

import org.jdesktop.swingx.JXHyperlink;

public class MyDateTime extends JPanel {//extends JFrame implements ActionListener {

private MyJXDatePicker datePicker;

public MyDateTime() {

datePicker = new MyJXDatePicker();

add(datePicker);

datePicker.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

dateChangeActionPerformed(e);

}

});

}

public void dateChangeActionPerformed(java.awt.event.ActionEvent e){

System.out.println("The date is: " + datePicker.getDate());

}

}

class MyJXDatePicker extends JXDatePicker {

private String[] dateStr = null;

private DateFormat df;

private SimpleDateFormat df2;

private JPanel newLinkPanel;

public MyJXDatePicker() {

// TODO Auto-generated constructor stub

df = DateFormat.getDateInstance(DateFormat.LONG);

df2 = new SimpleDateFormat("dd/MM/yyyy");

dateStr = new String[] { "dd/MM/yyyy" };

LinkPanel();

setFormats(dateStr);

setLinkPanel(newLinkPanel);

setDate(new Date());

}

private void LinkPanel() {

newLinkPanel = new JPanel(new BorderLayout());

JXHyperlink todayLink = new JXHyperlink();

todayLink.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

getEditor().setText(df2.format((new Date())));

setDate(new Date());

getMonthView().getParent().setVisible(false);

fireActionPerformed();

}

});

todayLink.setText("Today: " + df.format(new Date()));

newLinkPanel.add(todayLink, BorderLayout.CENTER);

}

}

[5327 byte] By [Bemoa] at [2007-11-26 18:04:09]
# 1

> I want to package a gui component so that I can use

> it within Netbeans. I have managed to do this with

> the actionlisteners and anction performed methods

> within the JAR package.

>

> How could I have an actionlistener detect the events

> occuring within the JAR package?

>

Your question doesn't make sense to me.ActionListeners attach to Components and both of those are objects. Jars do not contain objects - they contain classes. You just need to write code to add ActionListeners to your Components. You do not need to write code that is dependent on whether a class is in a jar or not. You do need to make all of your classes, whether they are in the jar or not, available to your compiler and to the JVM.

atmguya at 2007-7-9 5:34:29 > top of Java-index,Desktop,Deploying...