0

I am cleaning my code. I read that I putting the ActionListener is another class is better. So that's what I did.

But in my ActionListener, everything works except at some point in the code, I got a setSize(xx,xx). I worked before because it was in the same class. But not anymore. I've tried multiple solutions but I couldn't figure it out.

ActionListener:

public class ActionFrame implements ActionListener{

public void actionPerformed(ActionEvent e){
    Object src = e.getSource();

    if(src == Frame.Console_Bouton){
        System.out.println("Bouton console");
        if(getSize().getWidth() >= 750){
            /** If True (Retirer) */
            for(int i = 1090; i > 689; i--){
                setSize(i, 490);
                System.out.println("Rétractation du Frame");
            }
        }else{
            /** If False (Etirer) */
            for(int i = 689; i < 1090; i++){
                setSize(i, 490);
                System.out.println("Etirage du Frame");
            }
        }

            }

            ...

As for errors, there are none, it will just freeze the program.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3751_Creator
  • 656
  • 2
  • 8
  • 22

4 Answers4

2

Guessing: possibly this is a case where extracting the ActionListener is not a great idea, since it uses a private method of your object.

Classes with generic/reusable functionality deserve to be on their own. As long as they are intended for specific usage, it's not bad practice (at all!) to only put them as close as possible to the spot where they're used. I can imagine that your setSize method is not part of your class' public interface, so the ActionListener is merely 'glue' to couple an event to your specific class.

In this case, you would create a 'tiny' line of glue:

abstract class ActionAdapter implements ActionListener {
}
...
frame.Console_Bouton.addActionListener(
    new ActionAdapter(){ // anonymous inner class 
      void actionPerformed(ActionEvent e){
         ... // (no need to check source!)
      }
    });
xtofl
  • 40,723
  • 12
  • 105
  • 192
1

Create a new class as:

 ButtonAction implements actionListner
{
  //put the code above here
}
Toufic
  • 292
  • 3
  • 8
0

A Good way of doing this is use Callback mechanism.

I have posted an answer in the same context here

JFrame in separate class, what about the ActionListener?


-- EDIT--

Get the source from ActionEvent then find its parent (get parent of parent if needed until you get the desired component that needs to be re sized) and call setSize() on it.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I think he's trying to follow the MVC pattern and, in that context, is moving the action listener from the View class to the Controller class. Callbacks are more inline with Multithreading. – W.K.S Mar 30 '14 at 12:00
  • @W.K.S Yes I am trying to follow the MVC. – 3751_Creator Mar 30 '14 at 12:01
  • I don't think so that Callbacks are more inline with Multithreading only. Its a pattern that can be any where in any technology. – Braj Mar 30 '14 at 12:07
-1
  1. Create an instance of the View in the controller

  2. Change the access modifier of setSize(xx,yy)method from private to public.

  3. replace setSize in actionPerformed() with to view.setSize(xx,yy).

W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • which part is not needed? – W.K.S Mar 30 '14 at 12:10
  • Apparently, the class works allright without public `setSize`. Therefor it's not needed to make it public. Unless it's part of the 'published' functionality of that class, I think it's way better to keep it private. – xtofl Mar 30 '14 at 12:13