Results 1 to 8 of 8
  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Hash of hashes and their access?

    Hello, I am trying to make a dynamic structure with hash maps. I have an issue: when I refer to hash element with get:
    Java Code:
    hashElements.put("Button1", new JButton("1"));
    System.out.println(hashElements.get("Button1").getText());
    it gives me: error: cannot find symbol. I suppose that I can`t access methods of buttons. Any good ideas how can I do that?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hash of hashes and their access?

    It depends on the type definition of hashElements; if it is defined as a Map<String, JButton> (or HashMap<String, JButton>) all will be fine; if it is defined as a raw Map (or HashMap), you have to cast the returned value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Re: Hash of hashes and their access?

    The definition is HashMap<String, JComponent>, I suppose that JComponent must have getText function?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hash of hashes and their access?

    Quote Originally Posted by heatblazer View Post
    The definition is HashMap<String, JComponent>, I suppose that JComponent must have getText function?
    Read the API documentation for the JComponent class and check if it has a getText() method. ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Re: Hash of hashes and their access?

    Hmmm... I think that I got where I missed it. Polymorphism did not worked as I expected. But I don`t get one thing:
    if JComponent is a superclass of JButton, I was able to create a button with JComponent jb = new JButton("sth"), but since I don`t have the getText() in JComponent, how can I cast the object? Must I made a factory method?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hash of hashes and their access?

    Think of it this way:

    Java Code:
    JComponent jc= new JPanel();
    
    jc.getText(); // should this work?
    There is an easy way out: do a downcast:

    Java Code:
    JComponent jc= new JButton("foo");
    
    ((JButton)jc).getText(); // this works
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Re: Hash of hashes and their access?

    Thank you. Best regards. That was tricky with the cast and parenthesis. I figured it that first the cast must be done then the get :)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hash of hashes and their access?

    Quote Originally Posted by heatblazer View Post
    Thank you. Best regards. That was tricky with the cast and parenthesis. I figured it that first the cast must be done then the get :)
    Not really; your map is defined to have JComponents as their values; they actually are JButtons but only you and I know that; the compiler only knows that they are JComponents and hence the downcast on the values; ((JButton)map.get("something")).getText().

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 9
    Last Post: 11-12-2012, 11:38 AM
  2. Hash set
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 07-20-2011, 06:58 PM
  3. Hash Set in Java
    By muj in forum New To Java
    Replies: 14
    Last Post: 08-09-2010, 04:11 PM
  4. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  5. Hash Map
    By rekha in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 01:00 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
  •