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 : - /home/vontux/cis435lab1/src/UserInterface.java:103: <identifier> expected
-
btn_roll.addActionListener(
-
1 error
It acts as if the button is not yet declared ;however, as you can see in the rest of the code here - /*
-
* UserInterface.java
-
*
-
* Created on March 20, 2007, 8:31 PM
-
*/
-
-
/**
-
*
-
* @author vontux
-
*/
-
import java.util.Random;
-
public class UserInterface extends javax.swing.JDialog {
-
-
/** Creates new form UserInterface */
-
private Random rand = new Random();
-
public UserInterface(java.awt.Frame parent, boolean modal) {
-
super(parent, modal);
-
initComponents();
-
}
-
-
/** This method is called from within the constructor to
-
* initialize the form.bu
-
* WARNING: Do NOT modify this code. The content of this method is
-
* always regenerated by the Form Editor.
-
*/
-
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
-
private void initComponents() {
-
txt_rand = new javax.swing.JTextField();
-
btn_roll = new javax.swing.JButton();
-
lbl_status = new javax.swing.JLabel();
-
-
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
-
addWindowListener(new java.awt.event.WindowAdapter() {
-
public void windowClosed(java.awt.event.WindowEvent evt) {
-
UserInterface.this.windowClosed(evt);
-
}
-
});
-
-
txt_rand.setText("1234");
-
-
btn_roll.setText("Roll");
-
-
lbl_status.setText("jLabel1");
-
-
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
-
getContentPane().setLayout(layout);
-
layout.setHorizontalGroup(
-
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
-
.addContainerGap(127, Short.MAX_VALUE)
-
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(lbl_status)
-
.add(layout.createSequentialGroup()
-
.add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-
.add(22, 22, 22)
-
.add(btn_roll)))
-
.add(159, 159, 159))
-
);
-
layout.setVerticalGroup(
-
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(layout.createSequentialGroup()
-
.add(54, 54, 54)
-
.add(lbl_status)
-
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
-
.add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-
.add(btn_roll))
-
.addContainerGap(67, Short.MAX_VALUE))
-
);
-
pack();
-
}// </editor-fold>
-
-
-
private void windowClosed(java.awt.event.WindowEvent evt) {
-
// TODO add your handling code here:
-
System.exit(0);
-
}
-
-
/*btn_roll.addActionListener(new ActionListener() {
-
public void actionPerformed(ActionEvent event)
-
{
-
randMethod();
-
}
-
});
-
*/
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String args[]) {
-
java.awt.EventQueue.invokeLater(new Runnable() {
-
public void run() {
-
new UserInterface(new javax.swing.JFrame(), true).setVisible(true);
-
}
-
});
-
}
-
-
// Variables declaration - do not modify
-
private javax.swing.JButton btn_roll;
-
private javax.swing.JLabel lbl_status;
-
private javax.swing.JTextField txt_rand;
-
// End of variables declaration
-
-
btn_roll.addActionListener(
-
new ActionListener()
-
{
-
public void actionPerformed( ActionEvent event )
-
{
-
randMethod();
-
}
-
});
-
private void randMethod()
-
{
-
int roll = rand.nextInt(2000);
-
}
-
}
-
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.
1 3076
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 -
-
/*
-
* UserInterface.java
-
*
-
* Created on March 20, 2007, 8:31 PM
-
*/
-
/**
-
*
-
* @author vontux
-
*/
-
import java.awt.event.*;
-
import java.util.Random;
-
public class UserInterface extends javax.swing.JDialog {
-
/** Creates new form UserInterface */
-
private Random rand = new Random();
-
public UserInterface(java.awt.Frame parent, boolean modal) {
-
super(parent, modal);
-
initComponents();
-
}
-
/** This method is called from within the constructor to
-
* initialize the form.bu
-
* WARNING: Do NOT modify this code. The content of this method is
-
* always regenerated by the Form Editor.
-
*/
-
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
-
private void initComponents() {
-
txt_rand = new javax.swing.JTextField();
-
btn_roll = new javax.swing.JButton();
-
lbl_status = new javax.swing.JLabel();
-
-
btn_roll.addActionListener(
-
new ActionListener()
-
{
-
public void actionPerformed( ActionEvent event )
-
{
-
randMethod();
-
}
-
});
-
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
-
addWindowListener(new java.awt.event.WindowAdapter() {
-
public void windowClosed(java.awt.event.WindowEvent evt) {
-
UserInterface.this.windowClosed(evt);
-
}
-
});
-
txt_rand.setText("1234");
-
btn_roll.setText("Roll");
-
lbl_status.setText("jLabel1");
-
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
-
getContentPane().setLayout(layout);
-
layout.setHorizontalGroup(
-
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
-
.addContainerGap(127, Short.MAX_VALUE)
-
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(lbl_status)
-
.add(layout.createSequentialGroup()
-
.add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-
.add(22, 22, 22)
-
.add(btn_roll)))
-
.add(159, 159, 159))
-
);
-
layout.setVerticalGroup(
-
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-
.add(layout.createSequentialGroup()
-
.add(54, 54, 54)
-
.add(lbl_status)
-
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
-
.add(txt_rand, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-
.add(btn_roll))
-
.addContainerGap(67, Short.MAX_VALUE))
-
);
-
pack();
-
}// </editor-fold>
-
-
private void windowClosed(java.awt.event.WindowEvent evt) {
-
// TODO add your handling code here:
-
System.exit(0);
-
}
-
/*btn_roll.addActionListener(new ActionListener() {
-
public void actionPerformed(ActionEvent event)
-
{
-
randMethod();
-
}
-
});
-
*/
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String args[]) {
-
java.awt.EventQueue.invokeLater(new Runnable() {
-
public void run() {
-
new UserInterface(new javax.swing.JFrame(), true).setVisible(true);
-
}
-
});
-
}
-
// Variables declaration - do not modify
-
private javax.swing.JButton btn_roll;
-
private javax.swing.JLabel lbl_status;
-
private javax.swing.JTextField txt_rand;
-
// End of variables declaration
-
-
private void randMethod()
-
{
-
int roll = rand.nextInt(2000);
-
}
-
-
}
-
Notice also that I have added the import for awt.event which is required since you are using ActionListener.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
|
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 (...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| | |