This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes closing a window and returning a value from a subroutine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "closing a window and returning a value from a subroutine" Watch "closing a window and returning a value from a subroutine" New topic

closing a window and returning a value from a subroutine

Chuck Geiger

Joined: Jun 26, 2012
Posts: 17
I am relatively new to Java and this is my first posting here. I'm trying to adapt a program I wrote years ago in Visual Basic. This is one class that is called by another to prompt the user to select the type of variable (the data are read in elsewhere in the larger program) from a radio button list (mine has 10 choices; I limited it to 3 here). Once the radio button is set, they click on the JButton ("Done") to pass the varType back to the calling subroutine and close the JWindow (I preferred the JWindow to the dialog box choices because I don't want the user to be able to "X" out of this dialog. Two problems are preventing this from compiling. The first is where to put the Return statement (currently in the button's ActionListener class). The second is where or how to close the JWindow (I'm using "dispose()", currently in the subroutine called by the ActionListener). I left out the imports to save space. Thanks for any help.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Am curious to know why you want method vtDone() ( and a few others ) to be static?
Problem #2 i.e. "Cannot make a static reference to the non-static method" will not happen if you do not do so.

And your problem #1 is because probably you are trying to return a value from a class.

class DoneListener implements ActionListener {
public void actionPerformed(ActionEvent bE) {
objBE = bE.getSource();
return varType; //PROBLEM 1

Java (and if I am correct other languages also) do not have this feature.
Besides, which java version are you using ? I get a few additional compilation errors with Java 6 with this class.

"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Chuck Geiger

Joined: Jun 26, 2012
Posts: 17
First, thank you. I am using version 7. The reason why it is static is partly because I am still learning the difference, and partly because I am using Eclipse to build the program. I am calling this part of the program with the following line:

If I make the methods in FormVT non-static, the above line generates an error that it cannot call a non-static method. I do have other concerns about that other part of the program, but I am trying to compile in order to debug.
Are you saying that I need to "return" the value from someplace other than the DoneListener class?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19739

You shouldn't be declaring anything static in that class. No static methods, no static variables. And don't access the FormVT in an static context either. Create an instance of the FormVT class and use that instance throughout.
Chuck Geiger

Joined: Jun 26, 2012
Posts: 17
I think I understand. I just found another posting on coderanch that seems to do what I am attempting without "static" anything. Back to the drawing board, but I'm optimistic. Thanks.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20128

I've sometimes used the JOptionPane technique for this. In pseudo code:
The result will be a field of the MyDialog instance. Upon pressing OK you give it a proper value, upon pressing Cancel you set it to null (or some other default value). Note that this one method is the only static member of the entire class.

How To Ask Questions How To Answer Questions
Chuck Geiger

Joined: Jun 26, 2012
Posts: 17
Rob, your answer did the trick for me. I used JDialog instead of JOptionPane, but the result is probably similar. I disabled the "X" window closer to get the effect I needed. The only thing that doesn't work is setting the background color of the BorderLayout.CENTER section. I'm attaching my revised code (again, with some repetitive bits deleted and "imports" left out) in case it helps anyone. Again, thanks a ton!
I agree. Here's the link:
subject: closing a window and returning a value from a subroutine
It's not a secret anymore!