M
Mark
Hello Everyone.
I came across something weird while trying to understand event dispatching.
In short, I have designed a little form program in both Swing and AWT
versions where one button's event handler dispatches an event to another
button. The funny thing is that dispatching works fine in the AWT version
but does not in the Swing version. What's even spookier is that I've gotten
events to dispatch in other small programs using a Swing context. I just
don't know what to make of it. Below are the two programs in question.
Please note that I'm using the NetBeans IDE which arranges code in a very
specific way.
Any insight on this matter would be appreciated. Thanks.
Mark
//*******************************************
public class TestFormAWT extends java.awt.Frame {
/** Creates new form TestForm */
public TestFormAWT() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
button1 = new java.awt.Button();
button2 = new java.awt.Button();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
button1.setLabel("Button 1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
add(button1, java.awt.BorderLayout.NORTH);
button2.setLabel("Button 2");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
add(button2, java.awt.BorderLayout.SOUTH);
pack();
}
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 1 event processed.");
button2.dispatchEvent(new java.awt.event.ActionEvent(
button2, java.awt.event.ActionEvent.ACTION_PERFORMED, "Button
2"));
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 2 event processed.");
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TestFormAWT().show();
}
// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.Button button2;
// End of variables declaration
}
//*******************************************
public class TestFormSwing extends javax.swing.JFrame {
/** Creates new form TestFormSwing */
public TestFormSwing() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jButton1.setText("Button 1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, java.awt.BorderLayout.NORTH);
jButton2.setText("Button 2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, java.awt.BorderLayout.SOUTH);
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 2 event processed.");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 1 event processed.");
jButton2.dispatchEvent(new java.awt.event.ActionEvent(
jButton2, java.awt.event.ActionEvent.ACTION_PERFORMED, "Button
2"));
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TestFormSwing().show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
//*******************************************
I came across something weird while trying to understand event dispatching.
In short, I have designed a little form program in both Swing and AWT
versions where one button's event handler dispatches an event to another
button. The funny thing is that dispatching works fine in the AWT version
but does not in the Swing version. What's even spookier is that I've gotten
events to dispatch in other small programs using a Swing context. I just
don't know what to make of it. Below are the two programs in question.
Please note that I'm using the NetBeans IDE which arranges code in a very
specific way.
Any insight on this matter would be appreciated. Thanks.
Mark
//*******************************************
public class TestFormAWT extends java.awt.Frame {
/** Creates new form TestForm */
public TestFormAWT() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
button1 = new java.awt.Button();
button2 = new java.awt.Button();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
button1.setLabel("Button 1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
add(button1, java.awt.BorderLayout.NORTH);
button2.setLabel("Button 2");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button2ActionPerformed(evt);
}
});
add(button2, java.awt.BorderLayout.SOUTH);
pack();
}
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 1 event processed.");
button2.dispatchEvent(new java.awt.event.ActionEvent(
button2, java.awt.event.ActionEvent.ACTION_PERFORMED, "Button
2"));
}
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 2 event processed.");
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TestFormAWT().show();
}
// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.Button button2;
// End of variables declaration
}
//*******************************************
public class TestFormSwing extends javax.swing.JFrame {
/** Creates new form TestFormSwing */
public TestFormSwing() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jButton1.setText("Button 1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, java.awt.BorderLayout.NORTH);
jButton2.setText("Button 2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, java.awt.BorderLayout.SOUTH);
pack();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 2 event processed.");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
System.out.println("Button 1 event processed.");
jButton2.dispatchEvent(new java.awt.event.ActionEvent(
jButton2, java.awt.event.ActionEvent.ACTION_PERFORMED, "Button
2"));
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TestFormSwing().show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
//*******************************************