Simple (?) Syntax Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Simple (?) Syntax Question

  1. #1
    Join Date
    Mar 2005
    Posts
    24

    Simple (?) Syntax Question

    I can't seem to get this to work. I assume it's a fairly simple/common problem.. But I can't figure out a way around it.

    I have a method which returns a "Connection." Then I have an action listener within that method. On the click of a button (which is defined in the method) I want to return my connection.

    Code:
    Connection c;
    public Connection getConnection(){
       c = new Connection();
       JFrame f = new JFrame();
       <All the frame code>
    
       JButton button = new Button("Button");
       f.add(button);
    
       button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
             return c;
          }
       });
    }
    Unfortunately, I can't return an object from a void-type method. I tried to just put a loop at the end of the method, that would "stall" the program until the button was clicked, but infinite loops and GUIs seem not to be too friendly, as the GUI didn't display any of the components as long as I was stuck in the loop, even though they had all been established and added before entering the loop.

    Sorry if this wasn't very articulate. I think the code should speak for itself. Let me know if I can be clearer.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    I think what you are going to want to do is write your code so that you show this form and then in the ActionListener, you call whatever function you want to handle the new Connection object. For instance:
    Code:
    private void handleConnection(Connection c) {
      // ... some code here to handle the connection ...
    }
    
    Connection c;
    public void showConnectionDialog(){
       c = new Connection();
       JFrame f = new JFrame();
       <All the frame code>
    
       JButton button = new Button("Button");
       f.add(button);
    
       button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
             handleConnection(c);
          }
       });
    }
    Is this what you were looking for? What exactly is it that this frame does? My guess is that it modifies properties about the connection, but that's just a guess. Hope this helps.
    ~evlich

  3. #3
    Join Date
    Mar 2005
    Posts
    24
    That makes sense right now.. I'll try to code it out tomorrow and see if I run into any more errors.

    Thanks !

  4. #4
    Join Date
    Mar 2005
    Posts
    24
    Ok.. I'm actually running into the same problem I had earlier, which is just that there's no way to return a connection. In your example, we still don't get around the issue because both of your methods are void.

    Yes, the frame justallows for editing properties of a connection.

  5. #5
    Join Date
    Mar 2005
    Posts
    24
    figured out a work-around.

    thanks for the advice !

Similar Threads

  1. Replies: 2
    Last Post: 03-06-2005, 10:14 AM
  2. Very Simple Question - I think
    By Andrew Murphy in forum VB Classic
    Replies: 1
    Last Post: 09-03-2002, 02:17 AM
  3. Simple Java DOM question
    By Lenin in forum XML
    Replies: 0
    Last Post: 03-21-2001, 05:01 PM
  4. Simple Data Environment Question
    By Alex Nitulescu in forum VB Classic
    Replies: 1
    Last Post: 10-21-2000, 06:13 AM
  5. Simple Class Variables question
    By Gary McCallum in forum Java
    Replies: 2
    Last Post: 10-20-2000, 12:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center