5 Replies - 375 Views - Last Post: 12 March 2011 - 10:31 PM Rate Topic: -----

#1 Eyeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-March 11

Help with awt ActionListener

Posted 12 March 2011 - 07:38 PM

Please bare with me-I believe I have been working with java for a total of three days as of tonight. I have a quick question- I can't seem to figure out a way to go about doing this.
Let's say I have one class with my main method which extends JFrame-lets call this class frame.I have two more classes (call them button and field) which extend JButton and JTextField respectively. (unfortunately I have no code to show you right now so this is all hypothetical) in my main method I create a JFrame and create an instance of both my button class and my field class,(let's call these instances buttonObject and textObject) and add them to my frame.
My question is, If I add an ActionEvent to buttonObject, how can I have it access my TextObject's getText() method when textObject is defined in a different class from buttonObject's ActionEvent?

I'm sorry if this made no sense, im away from my computer and trying to write this from my memory. When I'm at my computer I will show you my attempt at this code so you can hopefully see what I am trying to do- for now if anyone is following me any help at all would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with awt ActionListener

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Help with awt ActionListener

Posted 12 March 2011 - 08:23 PM

Firstly, is there any reason your extending these classes? Are you redesigning a button or something, if not, no need to do this. I dont fully understand what your saying, but to me it seems like you may be having a design issue here.

Ok, this is what I understand. Your in your main class and you create two instances for your two Objects (Button and Text). You add an ActionEvent to one of these Objects (Button). When this button is pressed, you want to access something from the Text Class (ps, dont give them these names when your coding). If what I am describing is correct, why dont you just use the instance you created to your Text class to access the method?
Was This Post Helpful? 0
  • +
  • -

#3 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Help with awt ActionListener

Posted 12 March 2011 - 08:32 PM

I had this same issue but with help from DIC got it resolved.

Here was my topic I started (and the solution):
http://www.dreaminco...d-mouse-events/
Was This Post Helpful? 0
  • +
  • -

#4 Eyeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-March 11

Re: Help with awt ActionListener

Posted 12 March 2011 - 09:04 PM

View Postnick2price, on 12 March 2011 - 08:23 PM, said:

Firstly, is there any reason your extending these classes? Are you redesigning a button or something, if not, no need to do this. I dont fully understand what your saying, but to me it seems like you may be having a design issue here.

Ok, this is what I understand. Your in your main class and you create two instances for your two Objects (Button and Text). You add an ActionEvent to one of these Objects (Button). When this button is pressed, you want to access something from the Text Class (ps, dont give them these names when your coding). If what I am describing is correct, why dont you just use the instance you created to your Text class to access the method?


First of all no I would never name an actual class or object this, I'm just trying to keep things simple. I know that the best way in this case would be to have one class file, I am just trying to figure out if there EXISTS a way to access a method from an object created in another class. I'm sorry if I'm just wasting your time.

bmcginnis, if I understood correctly your solution was to create a method which returned a boolean stating whether or not a togglebutton was selected, yet I cannot think of a way to implement this with a non-toggle button?

thank you guys for the quick replies, again I'm very new to this so feel free to just tell me I'm an idiot/in over my head and I'll quietly go back to reviewing helloworld programs.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Help with awt ActionListener

Posted 12 March 2011 - 09:43 PM

Given your scenario in your original post, the cleanest solution would be to instantiate your JButton and JTextField in the JFrame subclass. Then add an ActionListener to the JButton there, and have it interact with the JTextField instance variable. Classes should not be designed in a way that requires their instances to interact with other classes or Objects they may or may not have access to in other scenarios. This goes to proper encapsulation.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with awt ActionListener

Posted 12 March 2011 - 10:31 PM

Or you can create your own class that extends JButton and implements ActionListener

Pass your JTextField to the constructor of this class
Then you can access it in the actionPerformed

class MyButton extends JButton implements ActionListener {
    JTextField theTextFieldToUse;

    // constructor
    MyButton(String text, JTextField field) {
      super(text);
      theTextFieldToUsee = field;
      addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        // OK button clicked lets play with theTextFieldToUse now
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1