Closed Thread
Results 1 to 10 of 10

Thread: help for the return type ..~

  1. #1
    R3.RyozKidz Guest

    help for the return type ..~

    if a method that is returning an interface , what should i do so that i can get the return value ..?

    instance : in ItemEvent class , there is a method named getItemSelectable() with ItemSelectable as a return value .

    can i just declare Object type reference to get the return value ..?

    Object source = ItemEvent.getItemSelectable();

  2. CODECALL Circuit advertisement

     
  3. #2
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: help for the return type ..~

    2.6 The Checkbox and CheckboxGroup classes

    You need to either check its type, compare it against know objects or typecast it if you want to access values.

    Just lookup "java getItemSelectable" on google and you will find a bunch of examples.
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  4. #3
    R3.RyozKidz Guest

    Re: help for the return type ..~

    the problem is i dunno how to check the return type of getItemSelectable(since it return a interface type value)

  5. #4
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: help for the return type ..~

    Same as a normal class. Use getClass and either compare to a class or use getClass().getName() and compare to a string.

    Code:
    interface MyInterface
    {
    }
    
    MyInterface myObject;
    if (myObject.getClass() == MyInterface.class)
    {
    }
    You can also just typecast and if its not correct, Java will spit the dummy (ie: throw an exception), which you can catch. It depends on how you prefer to code.
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  6. #5
    R3.RyozKidz Guest

    Re: help for the return type ..~

    i get what u r trying to say ..~
    if that a possibility to downcast the object class to string class..?
    like this example ..~
    Code:
    static private String selectedString(ItemSelectable is) {
        Object selected[] = is.getSelectedObjects();
        return ((selected.length == 0) ? "null" : (String)selected[0]);
    i try to search getSelectedObject method in ItemEvent , but i cant find it...~

  7. #6
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: help for the return type ..~

    Um. Short answer, um no. Long answer, maybe, if the object that is selected was derived from a String, which Im not even sure is possible.

    Why would you think the ItemSelectable is a string? Check its ClassName if you want to know what it is.

    Code:
    System.out.print(this.getClass().getName());
    You can only typecast an object to itself or a class it inherited from. So, find out what the object is and typecast it in order to access any variables/methods.
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  8. #7
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: help for the return type ..~

    btw, getSelectedObject() is from ItemSelectable, not ItemEvent

    ItemSelectable (Java 2 Platform SE v1.4.2)
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  9. #8
    R3.RyozKidz Guest

    Re: help for the return type ..~

    the "this" is refer to the Object type ?
    Code:
    System.out.print(this.getClass().getName());
    this code is from yours
    Code:
    interface MyInterface
    {
    }
    
    MyInterface myObject;
    if (myObject.getClass() == MyInterface.class)
    {
    }
    so i can compare the class by using myObject.getClass() == MyInterface.class ?

    the .class is from where..?

  10. #9
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: help for the return type ..~

    O god. Just go do something else!

    You are either playing this forum, or completely stupid. As i dont beleive you are stupid (based on your english), I beleive you are just playing this forum.

    Do you get paid for doing this? What is your goal?

    No human can be as stupid as you, therefore, you are a fake.

    Why? Are you paid by this site?
    Buzz PHP Class Library - Web Components Made Easy!
    http://www.buzzphp.com/

  11. #10
    R3.RyozKidz Guest

    Re: help for the return type ..~

    this is my assignment title
    Code:
    A common memory matching game played by young children is to start with a deck of cards that contain identical pairs.
     For example, given a six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. 
    The cards are shuffled and placed face down on the table.
     A player then selects two cards that are face down, turns the face up, and if the cards match they are left face up. 
    If the two cards do not match, they are returned to their original face down position. 
    The game continues until all the cards are face up.
    this is an incomplete code
    Code:
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import javax.swing.JOptionPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.util.Random;
    import java.awt.GridLayout;
    
    public class Assignment1 extends JPanel
    {
        private JFrame frame;
        private JButton buttons[];
        private GridLayout layout;
        private Random $rand;
        private String label_name[];
        private int shuffled
        
        public Assignment1()
        {
            frame = new JFrame("Xtreme Game");
            layout = new GridLayout(4 , 4 , 5 , 5);
            super(layout);
            buttons = new JButton[8];
            $rand = new Random();
            
            label_name = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8" };
            shuffled_int = new int[16];
            
            shuffling();
            createButton();
            add_buttons_to_layout();
            
        }
        
        private void shuffling()
        {
            for(int i = 0 ; i < label_name.length ; i++)
            {
                int x = $rand.nextInt(16);
                String temp = label_name[i];
                label_name[i] = label_name[x];
                
                shuffled[i] = Integer.parseInt(label_name[i]);
            }
        }
        
        private void createButton()
        {
            for(int i = 0 ; i < buttons.length ; i++)
            {
                buttons[i] = new JButton("*");
            }
        }
        
        private void add_buttons_to_layout()
        {
            for(int i = 0 ; i < 16 ; i++)
            {
                this.add(buttons[shuffled_int[i]-1]);
            }
        }
    this is the algorithm i have written
    Code:
    /*
    Class extends from JPanel
    JFrame object created
    8 objects of JButton will be created
    every buttons are labelled with "*"
    
    for(int i = 0 ; i < length ; i++)
    {
        buttons[i] = new JButton("*");
    }
    
    String label_name[] = {"1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8" };
    int shuffled[] = { };
    
    for( int i = 0 ; i < length ; i++)
    {
        int x = random.nextInt(16);
        String temp = label_name[i];
        label_name[i] = label_name[x];
        label_name[x] = temp;
        shuffled[i] = Integer.parseInt(shuffled[i]);
    }
    
    add every button into the layout
    
    for(int i = 0 ; i < length ; i++)
    {
        JPanel.add(buttons[shuffled[i]-1]);
    }
    
    add the layout to the content pane
    
    JFrame.setContentPane(JPanel's reference);
    
    create a event handler
    
    EventInstance reference = new EventInstance();
    
    register with the event
    
    for(int i = 0 ; i < length ; i++)
    {
        buttons[i].addItemListener(reference);
    }
    
    create an inner class for the EventInstance with extending the ItemListener
    
    private class EventInstance extends ItemListener
    {
        public void itemStateChange(ItemEvent event)
        {
            if(toggle_button == 1)
            {
                Object source = event.getItemSelectable();
                
                for(int i = 0 ; i < length ; i++)
                {
                    if(source == buttons[i])
                    {
                        buttons[i].setLabel((string)shuffled[i]);
                        x = i;
                    }
                }
                toggle_button++;
            }
            
            if(toggle_button == 2)
            {
                Object source = event.getItemSelectable();
                
                for(int i = 0 ; i < length ; i++)
                {
                    if(source == buttons[i])
                    {
                        buttons[i].setLabel((string)shuffled[i]);
                        y = i;
                    }
                }
                
                toggle_button = 1;
                
                if(buttons[x] == buttons[y])
                {
                    buttons[x].setEnabled(false);
                    buttons[y].setEnabled(false);
                    pair_matched++
                }
                else
                {
                    buttons[x].setLabel("*");
                    buttons[y].setLabel("*");
                }
                
                if(pair_matched == 8)
                {
                    JOptionPane(You have won);
                }
            }
        }
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 10-27-2011, 08:10 PM
  2. Difference between reference type and object type
    By scottbomb in forum Java Help
    Replies: 11
    Last Post: 04-26-2011, 09:52 PM
  3. Why not more than one return value?
    By mebob in forum C and C++
    Replies: 20
    Last Post: 08-16-2010, 03:41 PM
  4. Replies: 2
    Last Post: 05-31-2010, 02:32 PM
  5. 'Could not convert variant of type (String) into type (Double)'
    By Ilikestring in forum Pascal and Delphi
    Replies: 3
    Last Post: 03-26-2010, 05:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

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