Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing a window and returning a value from a subroutine

 
Chuck Geiger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 275
Eclipse IDE jQuery Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
vtDone();
}
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.


 
Chuck Geiger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chuck Geiger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic