Results 1 to 8 of 8
  1. #1
    skandalouz is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default create object with name from string

    Hi,
    for my school project i'm trying to create a GUI with JFrame, JButtons etc.
    But..is it possible to create a button,or any object for that matter, with a name selected from a string? So that i can refer to an object with the name of the string?
    So if i have
    Java Code:
    String s = "abc";
    Object s = new Object();
    abc.whateverMethod();
    it actually got the method in the Object..

    Thanks,
    Skandalouz

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

  3. #3
    skandalouz is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Hmm, fair enough.
    My project is some game-related simple database.
    we have a text file, which contains Objects(Characters in my case) we need to read, display and modify, in the following format
    Java Code:
    2
    Name1
    Att1 = Value1
    Att2 = Value2
    .
    Att6 = Value6
    Name2
    Att1 = Value1
    Att2 = Value2
    .
    Att6 = Value6
    where the first number(2) tells how many Characters are in the file, each Character has 6 attributes, in random order. I have a read method which creates Objects and puts them in an ArrayList. Now i have to read from the ArayList, which i have done with
    Java Code:
    for(Character c: list)
    {
    c.print()
    }
    and print():
    Java Code:
    JLabel name = new JLabel(c.getName());
    JLabel get exp = new JLabel(c.getExp());
    etc..
    
    name.setBounds(0,i*100,100,50);
    exp.setBounds(0,(i*100)+20,100,50);
    etc..
    i++
    
    frame.add(name);
    frame.add(exp);
    so this way all the characters are displayed nicely beneath eachother, no matter how much i get. But the point is we have to be able to modify the content. My idea was to add buttons with names depending on the i so that all the buttons are easy to refer to. My problem here is I've got multiple instances of the labels so i dont know where to put some actionlistener to select the attributes from that Character and go to my modify screen.

    Any thoughts?

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

    Default

    You might want to consider using a JTable and display the data within that. Then the data is findable, sortable and editable. Otherwise, you could use OOPs technique of encapsulation and make a class that creates a JPanel which holds the attribute JLabels for one and only one Character as well as a related JButton, and have that button's actionlistener display a dialog that allows editing of the attributes of the Character displayed in that JPanel.

    Also, you could give the JButton any text you wish, get the text in its actionlistner via the ActionEvent's getActionCommand() method and then use this to compare with whatever index or character attribute is displayed on the button.

    Also, whenever anyone mentions using variables as object names, I reflexively think of using HashMaps as this often solves this type of problem, though I'm not sure if it is applicable in your situation.

    There are lots of ways to skin this fish.

    As an aside, I strongly urge you to avoid using null layout and absolute positioning. You're much better off in the long run letting the layout managers do the heavy layout lifting for you. Best of luck.
    Last edited by Fubarable; 12-06-2009 at 12:02 AM.

  5. #5
    skandalouz is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the advice.
    when I first tried layouts, I had some difficulties getting everything decently displayed and didn't like the way everything got stretched, but i'll look into that again.
    Now, i like the idea of a JTable, but do I use this instead of a JFrame, or inside a JFrame?
    And won't I get to the same problems using my
    for(Character c: list)
    code? Or will i be able to refer to the cell i click in?
    Thanks

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

    Default

    The JTable, as with any Swing component, needs to be in a root container of some sort in order to be displayed which in your case could be a JFrame. If you are interested in using a JTable, a great place to learn about it, and all of Swing coding is the Sun Swing tutorials. There's a decent section on the layout managers (though it weighs too heavily in favor of the GridBagLayout for my likings). And yes, you'd be able to refer to the data held in a row or cell based on which cell gets clicked.

  7. #7
    skandalouz is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Great!
    I'll have a look at this, and I think this might be my solution.
    Thanks for your help, and if I run into problems again, you'll hear from me:)
    Skandalouz

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

Similar Threads

  1. String to an object?
    By bobbyboyy in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 08:43 PM
  2. How to create object dinamically and call methods.
    By Gelembjuk in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 10:28 AM
  3. how to create an object at runtime
    By sireesha in forum New To Java
    Replies: 8
    Last Post: 10-01-2008, 01:52 PM
  4. Replies: 1
    Last Post: 04-09-2008, 03:15 AM
  5. Automation server can't create object
    By katie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-06-2007, 11:53 PM

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
  •