Results 1 to 10 of 10
  1. #1
    Wesley Chin is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    Wesley Chin is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    Thanks :) Under what heading is it to create GUI's with Swing and without NetBeans code generation?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Wesley Chin View Post
    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

  5. #5
    Wesley Chin is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    OK great, I will take a look at it. Thanks very much for helping me. Much appreciated.

  6. #6
    Wesley Chin is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Wesley Chin View Post
    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
    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.

  8. #8
    Wesley Chin is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    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);
    }

  9. #9
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Again, anything that needs to be used in another method needs to be declared in the class, not the method.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    For instance:

    Java Code:
    public class Fu2 {
       int declaredInClass = 2; // variable visible throughout class
       
       public void myMethod() {
          int declaredInMethod = 3; // variable visible only in method
       }
    }
    Also, check out the forum FAQ about using code tags.

Similar Threads

  1. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  2. Mouse events, are they best or only way to go?
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-10-2009, 04:34 PM
  3. Need Help with events
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 09-23-2008, 03:18 AM
  4. swing events
    By chitra in forum AWT / Swing
    Replies: 3
    Last Post: 09-20-2008, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •