-
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.
-
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
-
That makes sense right now.. I'll try to code it out tomorrow and see if I run into any more errors.
Thanks !
-
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.
-
figured out a work-around.
thanks for the advice !
Similar Threads
-
Replies: 2
Last Post: 03-06-2005, 09:14 AM
-
By Andrew Murphy in forum VB Classic
Replies: 1
Last Post: 09-03-2002, 01:17 AM
-
Replies: 0
Last Post: 03-21-2001, 04:01 PM
-
By Alex Nitulescu in forum VB Classic
Replies: 1
Last Post: 10-21-2000, 05:13 AM
-
By Gary McCallum in forum Java
Replies: 2
Last Post: 10-20-2000, 11:20 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks