Create custom GUI JAR component and add actionlistener and action performed
807606Feb 10 2007 — edited Feb 10 2007Hi and thanks in advance.
I have a JFrame already made in Netbeans which is very similar to Eclipse.
I want to import just the date selector into netbeans. I have been able to do this with action listeners catching any clicks on the date selection within the JAR package. This works fine, and I can select the date, and have the action listener detect the selection and have it printed onto the console window. However, I want to receive this date value in the Netbeans program.
The best solution I think would be to add the action listener and catch the listener within the Netbeans program. I need help on how to do this.
Below are two different pieces of code. The first contains a main and is run independently of anything else. The second has no main, and is used to make a JAR file to import into Eclipse. This version contains the action listeners and action performed methods within the code. I don�t think I will be able to do this, as I want to return the date value to the Eclipse program.
/*********************************** First code example */
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() {
// TODO Auto-generated constructor stub
//JButton button = new JButton("Print Me");
//button.addActionListener(this);
getContentPane().add(getPanel(),BorderLayout.NORTH);
//getContentPane().add(button,BorderLayout.SOUTH);
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() {
// 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);
}
}
/*********************************** Second code example */
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);
}
}