Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting External "this" In An Internal Class 1

Status
Not open for further replies.

BoulderBum

Programmer
Jul 11, 2002
2,179
US
I just finished up a big project, and am now looking on making small improvements to the code while teaching myself a little more good Java programming practice.

I need to call this.validate() or something from within an ActionListener internal class for a button, and currently I'm using a private method ( getThis(){ return this; } ) to provide my reference (naturally, the "this" pointer in the internal class references the ActionListener).

I can't help but feel there's a simpler and better way to do what I want. Can anyone provide hook me up with an improvement?
 
Indeed, there is a simpler and better way to do "this" :
Here is a complete working example (run the program and press one of the buttons)
Instead of just printing "...this", you can call any method for that class.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DynamicButtonExample {

  // You should read this data from a file
  private String caption[] = { "B1", "B2" , "B3" };
  private String actionCommand[] = { "Cmd1", "Cmd2" , "Cmd3" };
  private Controller controller = new Controller();

  public DynamicButtonExample() {
  }

  public static void main(String[] args) {
    DynamicButtonExample example = new DynamicButtonExample();
    example.doIt();
  }

  public void doIt() {
    JFrame f = new JFrame("Button example");
    JPanel buttonPanel = getButtonPanel();
    f.getContentPane().add(buttonPanel, "South");
    f.pack();
    f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            System.exit(0);
          }
      });
    f.setSize(200,200);
    f.setVisible(true);
  }

  private JPanel getButtonPanel()  {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
    for (int i=0; i<caption.length; i++) {
      JButton button = new JButton( caption[i] );
      button.addActionListener    ( controller     );
      button.setActionCommand     ( actionCommand[i] );
      panel.add(button);
    }
    return panel;
  }

  public class Controller implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String value = ((JButton)(e.getSource())).getActionCommand();
      System.out.println(&quot;Sending value &quot; + value + &quot; to the server ...&quot;);
      System.out.println(&quot;this=&quot; + this);
      System.out.println(&quot;DynamicButtonExample.this=&quot; + DynamicButtonExample.this);
      System.out.println(&quot;DynamicButtonExample.Controller.this=&quot; + DynamicButtonExample.Controller.this);
    }
  }

}
 
&quot;Indeed, there is a simpler and better way to do &quot;this&quot; :&quot;

Boooo. [thumbsdown]

Thanks for replying. I could be missing something, but I don't think the above quite answers my question. I tried simply calling (my JFrame class name).validate() from within the internal class, but there was a compile error stating the method needs to be static for that to work.

I can't implement validate() as static, however, because it needs to be instance-specific. I suppose I can have a private method which simply calls this.validate() from outside the class, which is a little more secure, but I was wondering if there was a way to reference the owner of an internal class by instance.

Here's what I have so far:


//ocrp is a subclassed JPanel in my JFrame
ocrp.getShrinkButton().addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
Dimension d = ocrp.getSize();

if( d.getHeight() > 110 )
{
ocrp.setSize( (int) d.getWidth(),
(int) d.getHeight() - 30);
ocrp.reSizePref();
//getThis() returns reference to current instance of my JFrame class
getThis().validate();
}
else
minimize(); //my own method
}
}
);



How should I change getThis().validate();?

While I'm at it, I tried having the panel sizing be a private method of the JPanel (ocrp), but if I made the panel too small, then fiddled with the resize buttons a few more times, they lost functionality (even though the debugger shows that the resize code is called). What's the deal with that?
 
I think my code solved your question. But you have another problem.
>> there was a compile error stating the method needs to be static for that to work.
I assume you are calling everything from the main method, which is static. If so, you can try to do it like in my code :
Code:
  public static void main(String[] args) {
    DynamicButtonExample example = new DynamicButtonExample();
    example.doIt();
  }

  public void doIt() {
    .....
  }
First create an instance of your main class. Then call a method to do all the rest. [ doIt() in the example is an instance method and not a static one ].

 
I may be a semi-newb, but you're dealing with someone having a little more experience than you're giving credit for. ;-)

Actually, the class in question is a standalone subclassed JFrame, called OCalcFrame (which is instantiated in a different application class).

In the constructor, I initialize some resize buttons on a subclassed JPanel member (the reason the buttons are initialized by OCalcFrame is because they must communicate with OCalcFrame's LayoutManager). The code to initialize one button is shown in my previous post.

Maybe I misunderstood your suggestion, but what I tried to do was to change:
getThis().validate();
to
OCalcFrame.validate();
within the internal anonymous ActionListener I create for a button.

I think the call to OCalcFrame.validate() requires a static member function because it doesn't appear to reference a particular instance of an OCalcFrame.

I'm thinking either DynamicButtonExample.toString() is implemented as a static function, or I missed the point of your original post (which is probably more likely).

 
I realized my error right before I went to bed...
OCalcFrame.validate();
needed to be
OCalcFrame.this.validate();

Don't I feel sheepish. [hammer]

Thanks, holo!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top