473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Netbeans IDE hell, a problem with my actionlistener

Hello, I am currently attempting to implement a simple actionlistener for a button in a JFrame that was created via "drag and drop" with the Netbeans 5.0 IDE, the code that I am using to implement the actionlistener should work, and is taken from other functional examples ;however, for some reason the compiler kicks out the following error :
Expand|Select|Wrap|Line Numbers
  1. /home/vontux/cis435lab1/src/UserInterface.java:103: <identifier> expected
  2.     btn_roll.addActionListener(
  3. 1 error
It acts as if the button is not yet declared ;however, as you can see in the rest of the code here
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * UserInterface.java
  3.  *
  4.  * Created on March 20, 2007, 8:31 PM
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author  vontux
  10.  */
  11. import java.util.Random;
  12. public class UserInterface extends javax.swing.JDialog {
  13.  
  14.     /** Creates new form UserInterface */
  15.     private Random rand = new Random();
  16.     public UserInterface(java.awt.Frame parent, boolean modal) {
  17.         super(parent, modal);
  18.         initComponents();
  19.     }
  20.  
  21.     /** This method is called from within the constructor to
  22.      * initialize the form.bu
  23.      * WARNING: Do NOT modify this code. The content of this method is
  24.      * always regenerated by the Form Editor.
  25.      */
  26.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  27.     private void initComponents() {
  28.         txt_rand = new javax.swing.JTextField();
  29.         btn_roll = new javax.swing.JButton();
  30.         lbl_status = new javax.swing.JLabel();
  31.  
  32.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  33.         addWindowListener(new java.awt.event.WindowAdapter() {
  34.             public void windowClosed(java.awt.event.WindowEvent evt) {
  35.                 UserInterface.this.windowClosed(evt);
  36.             }
  37.         });
  38.  
  39.         txt_rand.setText("1234");
  40.  
  41.         btn_roll.setText("Roll");
  42.  
  43.         lbl_status.setText("jLabel1");
  44.  
  45.         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  46.         getContentPane().setLayout(layout);
  47.         layout.setHorizontalGroup(
  48.             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  49.             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  50.                 .addContainerGap(127, Short.MAX_VALUE)
  51.                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  52.                     .add(lbl_status)
  53.                     .add(layout.createSequentialGroup()
  54.                         .add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  55.                         .add(22, 22, 22)
  56.                         .add(btn_roll)))
  57.                 .add(159, 159, 159))
  58.         );
  59.         layout.setVerticalGroup(
  60.             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  61.             .add(layout.createSequentialGroup()
  62.                 .add(54, 54, 54)
  63.                 .add(lbl_status)
  64.                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  65.                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  66.                     .add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  67.                     .add(btn_roll))
  68.                 .addContainerGap(67, Short.MAX_VALUE))
  69.         );
  70.         pack();
  71.     }// </editor-fold>
  72.  
  73.  
  74.     private void windowClosed(java.awt.event.WindowEvent evt) {
  75. // TODO add your handling code here:
  76.         System.exit(0);
  77.     }
  78.  
  79.     /*btn_roll.addActionListener(new ActionListener() {
  80.         public void actionPerformed(ActionEvent event)
  81.         {
  82.             randMethod();
  83.         }
  84.     });
  85.      */
  86.     /**
  87.      * @param args the command line arguments
  88.      */
  89.     public static void main(String args[]) {
  90.         java.awt.EventQueue.invokeLater(new Runnable() {
  91.             public void run() {
  92.                 new UserInterface(new javax.swing.JFrame(), true).setVisible(true);
  93.             }
  94.         });
  95.     }
  96.  
  97.     // Variables declaration - do not modify
  98.     private javax.swing.JButton btn_roll;
  99.     private javax.swing.JLabel lbl_status;
  100.     private javax.swing.JTextField txt_rand;
  101.     // End of variables declaration
  102.  
  103.     btn_roll.addActionListener(
  104.             new ActionListener()
  105.             {
  106.                 public void actionPerformed( ActionEvent event )
  107.                 {
  108.                     randMethod();
  109.                 }
  110.             });
  111.     private void randMethod()
  112.     {
  113.         int roll = rand.nextInt(2000);
  114.     }
  115. }
  116.  
the button is indeed declared before the actionlistener method is created. So, what am I doing wrong here, and what about the code generated by the ide (which is what I suspect is causing the problem with my code that should normally work (or at least kick out a different compiler error) is causing problems? Any assistance especially within the next day or two would be greatly appreciated.
Mar 21 '07 #1
1 3076
r035198x
13,262 8TB
The code for adding the actionListener should be put after the button is instantiated not after it has been declared. There fore you must put it in the initComponents method like this


Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * UserInterface.java
  4.  *
  5.  * Created on March 20, 2007, 8:31 PM
  6.  */
  7. /**
  8.  *
  9.  * @author  vontux
  10.  */
  11.  import java.awt.event.*;
  12. import java.util.Random;
  13. public class UserInterface extends javax.swing.JDialog {
  14.     /** Creates new form UserInterface */
  15.     private Random rand = new Random();
  16.     public UserInterface(java.awt.Frame parent, boolean modal) {
  17.         super(parent, modal);
  18.         initComponents();
  19.     }
  20.     /** This method is called from within the constructor to
  21.      * initialize the form.bu
  22.      * WARNING: Do NOT modify this code. The content of this method is
  23.      * always regenerated by the Form Editor.
  24.      */
  25.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  26.     private void initComponents() {
  27.         txt_rand = new javax.swing.JTextField();
  28.         btn_roll = new javax.swing.JButton();
  29.         lbl_status = new javax.swing.JLabel();
  30.  
  31.          btn_roll.addActionListener(
  32.               new ActionListener()
  33.               {
  34.                   public void actionPerformed( ActionEvent event )
  35.                   {
  36.                       randMethod();
  37.                   }
  38.             });
  39.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  40.         addWindowListener(new java.awt.event.WindowAdapter() {
  41.             public void windowClosed(java.awt.event.WindowEvent evt) {
  42.                 UserInterface.this.windowClosed(evt);
  43.             }
  44.         });
  45.         txt_rand.setText("1234");
  46.         btn_roll.setText("Roll");
  47.         lbl_status.setText("jLabel1");
  48.         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  49.         getContentPane().setLayout(layout);
  50.         layout.setHorizontalGroup(
  51.             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  52.             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  53.                 .addContainerGap(127, Short.MAX_VALUE)
  54.                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  55.                     .add(lbl_status)
  56.                     .add(layout.createSequentialGroup()
  57.                         .add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  58.                         .add(22, 22, 22)
  59.                         .add(btn_roll)))
  60.                 .add(159, 159, 159))
  61.         );
  62.         layout.setVerticalGroup(
  63.             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  64.             .add(layout.createSequentialGroup()
  65.                 .add(54, 54, 54)
  66.                 .add(lbl_status)
  67.                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  68.                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  69.                     .add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  70.                     .add(btn_roll))
  71.                 .addContainerGap(67, Short.MAX_VALUE))
  72.         );
  73.         pack();
  74.     }// </editor-fold>
  75.  
  76.     private void windowClosed(java.awt.event.WindowEvent evt) {
  77. // TODO add your handling code here:
  78.         System.exit(0);
  79.     }
  80.     /*btn_roll.addActionListener(new ActionListener() {
  81.         public void actionPerformed(ActionEvent event)
  82.         {
  83.             randMethod();
  84.         }
  85.     });
  86.      */
  87.     /**
  88.      * @param args the command line arguments
  89.      */
  90.     public static void main(String args[]) {
  91.         java.awt.EventQueue.invokeLater(new Runnable() {
  92.             public void run() {
  93.                 new UserInterface(new javax.swing.JFrame(), true).setVisible(true);
  94.             }
  95.         });
  96.     }
  97.  // Variables declaration - do not modify
  98.  private javax.swing.JButton btn_roll;
  99.  private javax.swing.JLabel lbl_status;
  100.  private javax.swing.JTextField txt_rand;
  101.     // End of variables declaration
  102.  
  103.     private void randMethod()
  104.     {
  105.         int roll = rand.nextInt(2000);
  106.     }
  107.  
  108. }
  109.  
Notice also that I have added the import for awt.event which is required since you are using ActionListener.
Mar 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Federico | last post by:
Hi all, I don't know if this topic is perhaps a little bit off-topic, anyway I have a strange problem in transforming an XML file in an HTML file using XSLT form a Java program written with...
8
oll3i
by: oll3i | last post by:
package zad41; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.BorderFactory;
1
by: cozsmin | last post by:
hello, my program is a simple banking application, i need it for my resumee, but i have a little problem i got stuck trying to maek some TextField -s visible , it seems that setting the layout (...
1
by: dishal | last post by:
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: uhdam | last post by:
HI... In netbeans, whenever i launch the app it loads up and then i get an empty pane where the gui would be. Initially i used Netbeans 6.5. Later i used Netbeans 5.0 and had the prob. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.