Results 1 to 8 of 8
  1. #1
    ksaketh2 is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default How do i create an object whose name is contained in a String object?

    The problem I have is this.

    I need to instantiate a JButton with the object name as the value of a string contained in a String object. How do I do this?

    Let me explain it clearly...

    Java Code:
    String str="button1";
    JButton ___________ = new JButton("Cilck Here");
    Now, how do I make the JButton object name as "button1" ? What should I fill in the blank to get the work done?

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: How do i create an object whose name is contained in a String object?

    You don't. There's no good reason to want to do that.

    Why do you want to do it?

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: How do i create an object whose name is contained in a String object?

    Names of the variables are not dynamic. This is not PHP. Like Tim said, why?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    ksaketh2 is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: How do i create an object whose name is contained in a String object?

    I am creating a swing application which will have 2 jframes viz. an empty one and a toolbox jframe. the toolbox will have many "tools." when a user drags and drops a tool( for example a jbutton) on to the empty jframe, the mouselistener gets activated and the jbutton object gets added to that empty jframe dynamically. now, for this, I am maintaining a sequence variable to check how many of each tools are being added to the first jframe. suppose a jbutton is being dropped onto the jframe, and if it is the first time it is being added, then the jbutton object should be created with name "button1." so I need to get a solution to my question. any help is appreciated. thanks in advance :)

  5. #5
    ksaketh2 is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: How do i create an object whose name is contained in a String object?

    can the use of
    Java Code:
    Map
    class be of any help?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How do i create an object whose name is contained in a String object?

    Quote Originally Posted by ksaketh2 View Post
    then the jbutton object should be created with name "button1."
    Objects don't have names.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do i create an object whose name is contained in a String object?

    Sounds to me like you want a symbolic reference which Java does not support. You can emulate it using maps as you suggested.

    Java Code:
    Map<String, JButton> buttons = new HashMap();
    buttons.put("button1", new JButton("click here" ));
    But unlike Perl for example, you cannot manipulate the internal symbol table.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How do i create an object whose name is contained in a String object?

    Quote Originally Posted by ksaketh2 View Post
    I am creating a swing application which will have 2 jframes viz. an empty one and a toolbox jframe. the toolbox will have many "tools." when a user drags and drops a tool( for example a jbutton) on to the empty jframe, the mouselistener gets activated and the jbutton object gets added to that empty jframe dynamically. now, for this, I am maintaining a sequence variable to check how many of each tools are being added to the first jframe. suppose a jbutton is being dropped onto the jframe, and if it is the first time it is being added, then the jbutton object should be created with name "button1." so I need to get a solution to my question. any help is appreciated. thanks in advance :)
    A code generator, huh? You seem to be conflating the generated code with the generator code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  2. How to create dynamic string object???
    By Stephen Douglas in forum New To Java
    Replies: 8
    Last Post: 04-12-2010, 02:35 AM
  3. Create a CLOB object with the string value
    By oshoarun in forum JDBC
    Replies: 1
    Last Post: 03-18-2010, 06:36 PM
  4. create object with name from string
    By skandalouz in forum New To Java
    Replies: 7
    Last Post: 12-05-2009, 11:40 PM
  5. Replies: 1
    Last Post: 04-09-2008, 02:15 AM

Tags for this Thread

Posting Permissions

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