Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Inner Class


  • Please log in to reply
16 replies to this topic

#1 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 04:27 AM

Hello,

Is the first time where trying to use inner class, so occurring some obstacles. I have an inner class that extends JButton and has getters and setters for X and Y (coord.) . So how and where, can I in my outer class create and initialise an array of inner class?

Thank you
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 07:44 AM


class Outer { 

    Inner[] inners = new Inners[4];

    class Inner { 

        ...

     }

}

  • 0

#3 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 08:11 AM

For a reason didn't accept this.! Re-open eclipse and accept it :S .. Thank you lethalwire. Another simple question, need to work with setActionCommand() and addActionListener(listener) for those buttons in the array. To achieve this should have different name for each one, ex. inners[0][0].setActionCommand("text1"); inners[0][1].setActionCommand("text2"); inners[0][3].setActionCommand("text3"); etc.. and addActionListener() for each button?? Or there is a simpler way?
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 10:05 AM

Yes, loop through each array object and initialize the button.
for(int row = 0 ...
      for (int col = 0 ....
           inners[row][col].setActionCommand( actionCommands[row][col] );
           inners[row][col].addActionListener( etc );
       }
}

actionCommands will have to be an array that holds the action commands for each button.
  • 0

#5 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 11:15 AM

Thanks, just could you please give me more details about actionCommands[row][col] ? Type, role etc. please

Thank you
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 11:39 AM

It depends on I guess the program, and how many buttons you have.
For instance, if let's say I had a program that's going to book hotel room, add customers, add phone numbers, add address, add credit card numbers, etc. All of these actions would need different commands.
In your case, you're going to have a 2D array of buttons, so I might naturally create a 2D array of action commands.

I can't say this design is elegant, but it gets my point across.
 MyJButton[][] buttons = new MyJButtons[2][2]; // 2 rows of buttons, 2 cols, totaling 4 buttons
 String[][] actionCommands = { 
                                               {"Add Customer", "Add Phone Number"}
                                               {"Add CC Number", "Add Check-in Date"}
                                         };

 ...
 for(int rows ...
     for(int cols ...
          buttons[rows][cols] = new MyJButton( actionCommands[rows][cols] );
          buttons[rows][cols].setActionCommand( actionCommands[rows][cols] );
      }
 }
This should result in 4 MyJButtons each with a title that matches the actionCommand name.

You can also, instead of using Strings, create a corresponding 2D array of ActionListener's.
MyJButton[][] buttons = new MyJButtons[2][2]; // 2 rows of buttons, 2 cols, totaling 4 buttons
ActionListener[][] listeners = { 
                                           {new AddPersonListener(), new RemovePersonListener()}
                                           {new AddCCListener(), new AddCheckinDateListener() }
                                         };
 for(int rows ...
     for(int cols ...
          buttons[rows][cols] = new MyJButton( actionCommands[rows][cols] );
          buttons[rows][cols].addActionListener( listeners[rows][cols] );
      }
 }


The 2nd set of code could be more suitable if you're creating inner classes of Listeners. Like I said, it's hard to say without knowing the kind of actions the buttons will be firing off, how you want your actions setup, etc.
  • 0

#7 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 11:45 AM

Ok you are right. My program contains 16 buttons 4x4. All buttons have the same behaviour, depends on how you initialise their Icon, all buttons have one image or another. Now my goal is when a button clicked change it's image and also the image of upper, down right and left buttons from clicked button. Also, I have one listener that checks all buttons, so how to initialise every button's actionCommand and used by the same listener?
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#8 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 11:54 AM

Are all of the icons for each button different? Are there 2, 3, 4, or more icons per button?

In your listener, can you not loop through each button, test whether it has been pushed, then change the icons accordingly?
Example:
actionPerformed(ActionEvent e ) {
    MyJButton button = (MyJButton)e.getSource();
    for(int rows ... 
       for(int cols ... 
            if( button[rows][cols] == button ) {
                 button[rows][cols].changeIcon( ... );
                 button[rows+1][cols].changeIcon( ... );
                 button[rows][col+1].changeIcon( ... );
                 etc.changeIcon( .. );
             }
          }
     }

  • 0

#9 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 12:02 PM

Buttons can have just two icons, imagine like coin, two sides. Click on a button and should change side, all around it should change too. Also, my actionPerformed is on different class. if(e.getActionCommand().equals("something")){....}"something" is the name that I gave to a specific button before this
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#10 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 01:25 PM

The actionPerformed, is it in an inner-class, or a completely separate class?

If I were doing this, I'd create an inner-class that implements ActionListener so you can have access to your 2D array of buttons.
  • 0

#11 toto_7

toto_7

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 12 November 2011 - 01:35 PM

Ok let me make it clear to you. My program is like mvc model, so I have one class that extends JFrame and initialise a GUI with 16 buttons. Those buttons are CoordButton type, CoordButton is my inner class where store X,Y for each button when initialise them in outer class(to know who is who). I have another class that stores some data and update the class who extends JFrame. Finally I have a third class that extends ActionListener and when something change(button clicked), this class activated, find out what button pressed, and inform appropriately the DataClass.In addition of 16 CoordButton buttons, I have and 4 JButton buttons. Each of those doing a different job, for example undo. So, undo button has undo.setActionCommand("undo") and undo.addActionListener(listener). "listener" is an object of class that implements ActionListener. Inside this(which implements ActionListener) class doing something like...
public void actionPerformed(ActionEvent e){
if(e.getActionCommand.equals("undo")){
//code
}
}


I hope helped you to understand where I'm stuck. With 16 button because have the same behaviour can't give them 16 different names. Or, can do this but this is maddens.
  • 0

"Programming is like **. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#12 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 12 November 2011 - 02:32 PM

If you want to do everything in a separate class, pass a reference of the object containing the JButtons, into the separate class.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download