Results 1 to 10 of 10
Thread: How to add events dynamically
- 11-12-2010, 09:23 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
How to add events dynamically
Hi everyone,
I had to create some JFrame components dynamically using code and not the palette to my Swing application because they need to be hidden and displayed when needed.
What I wanted to know is how do I add events to these components dynamically using code as well? I only know how to do it with the palette. I would to click the button I added and search the database. I am using NetBeans IDE 6.9.1.
Thanks,
Wesley
-
You need to read the tutorials on how to create GUI's with Swing and without NetBeans code generation. You may wish to start here: Using Swing Components
Best of luck!
- 11-12-2010, 09:29 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
Thanks :) Under what heading is it to create GUI's with Swing and without NetBeans code generation?
-
I've already linked to the subheading in the tutorial on how to create GUI without NetBeans code generation. For instance, to add listeners to JButtons (perhaps what you meant by "add events dynamically") and other components, you'd look at this section: Writing Event Listeners
- 11-12-2010, 09:34 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
OK great, I will take a look at it. Thanks very much for helping me. Much appreciated.
- 11-13-2010, 05:13 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
Thanks Fubarable, it is working. Just another question, how do I use those variables that I created for the dynamically created components outside of its method? I am still a beginner :D
- 11-13-2010, 05:18 AM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
A variable must be visible to be used and so if you want to be able to use a variable in more than one method of a class, it needs to be declared in the class itself, not a method so that its scope encompasses the entire class. Note that it does not necessarily have to be initialized in the class, and it is fine to declare it in the class and initialize it in your method, but just be sure that it is initialized before it is used.
- 11-13-2010, 05:22 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 14
- Rep Power
- 0
This is my code if it will make more sense. How can I use these components in another method?:
public void displayRegisterIcons() {
// Adding all the labels
JLabel registerLbl = new JLabel("Please register below to gain access:");
add(registerLbl);
registerLbl.setSize(250, 14);
registerLbl.setLocation(new Point(48, 59));
JLabel fNameLbl = new JLabel("First name:");
add(fNameLbl);
fNameLbl.setSize(70, 14);
fNameLbl.setLocation(new Point(48, 93));
JLabel lNameLbl = new JLabel("Last name:");
add(lNameLbl);
lNameLbl.setSize(70, 14);
lNameLbl.setLocation(new Point(48, 133));
JLabel ageLbl = new JLabel("Age:");
add(ageLbl);
ageLbl.setSize(70, 14);
ageLbl.setLocation(new Point(48, 173));
JLabel countryLbl = new JLabel("Country:");
add(countryLbl);
countryLbl.setSize(70, 14);
countryLbl.setLocation(new Point(48, 213));
JLabel usernameLbl = new JLabel("Username:");
add(usernameLbl);
usernameLbl.setSize(80, 14);
usernameLbl.setLocation(new Point(48, 253));
JLabel passwordLbl = new JLabel("Password:");
add(passwordLbl);
passwordLbl.setSize(80, 14);
passwordLbl.setLocation(new Point(48, 293));
// Adding all the text fields
JTextField fNameTF = new JTextField("");
add(fNameTF);
fNameTF.setSize(200, 20);
fNameTF.setLocation(129, 93);
JTextField lNameTF = new JTextField("");
add(lNameTF);
lNameTF.setSize(200, 20);
lNameTF.setLocation(129, 133);
JTextField ageTF = new JTextField("");
add(ageTF);
ageTF.setSize(200, 20);
ageTF.setLocation(129, 173);
JTextField countryTF = new JTextField("");
add(countryTF);
countryTF.setSize(200, 20);
countryTF.setLocation(129, 213);
JTextField usernameTF = new JTextField("");
add(usernameTF);
usernameTF.setSize(200, 20);
usernameTF.setLocation(129, 253);
// Adding the password field
JPasswordField passwordTF = new JPasswordField("");
add(passwordTF);
passwordTF.setSize(200, 20);
passwordTF.setLocation(129, 293);
// Adding the register button
JButton registerBtn = new JButton("Register");
add(registerBtn);
registerBtn.setSize(90, 20);
registerBtn.setLocation(149, 333);
registerBtn.addActionListener(this);
}
- 11-13-2010, 05:23 AM #9
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Again, anything that needs to be used in another method needs to be declared in the class, not the method.
- 11-13-2010, 05:26 AM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Similar Threads
-
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 12:22 PM -
Mouse events, are they best or only way to go?
By dbashby in forum New To JavaReplies: 2Last Post: 04-10-2009, 04:34 PM -
Need Help with events
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 09-23-2008, 03:18 AM -
swing events
By chitra in forum AWT / SwingReplies: 3Last Post: 09-20-2008, 04:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks