Results 1 to 8 of 8
- 11-05-2008, 11:14 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 60
- Rep Power
- 0
Calling a JFrame window from a command line program.
Hi all,
I have a command line Java program. I would like to call another class which extends JFrame, just to display a small windows with a couple of fields, then the program should halt, untill the user make a selection on the JFrame window, and only then the program should continue based on user's selection.
i.e. MainClass.java
Java Code:public class MainClass { public static void main(String[] arg) { JFrameClass jf = new JFrameClass(); jr.showJFrameClass(); // halt and do actions only when clicks on ok on JFrameClass. // ... } }
JFrameClass.java
Java Code:public class JFrameClass extends JFrame implements ActionListener { // make a JFrame window with a comboBox // and an OK and CANCEL button when user selects cancel // windows is closed and when clicks OK, then the program flow should go // back to MainClass.java public JFrameClass() { // set necessary variables, controles etc... } public void showJFrameClass() { this.setVisible(true); } public actionPerformed(ActionEvent e) { if (e.getSource() == OKButton) { // do somehting and return back the flow of the program // back to MainClass.java this.setVisible(false); } } }
Thanks,
- 11-06-2008, 12:41 AM #2
What you describe is the normal execution flow for a program.
Not sure what "halt" means.
If you display a window and then return from the method, none of your code is executing. If you have added listeners to components being shown in your window, when the user interacts with them, the listeners will be called.
What happens when you execute your code?
- 11-06-2008, 04:16 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 60
- Rep Power
- 0
Thanks Norm for the reply,
Here's what happens. consider the MainClass.java
Java Code:public class MainClass { public static void main(String[] arg) { JFrameClass jf = new JFrameClass(); jr.showJFrameClass(); // halt and do actions only when clicks on ok on JFrameClass. By this I mean, the codes from here on ward should not be executed, untill the user makes a selection on the JFrameClass.java. Say if I have the following SOP, then they will be executed. // ... System.out.println("This shouldn't be printed, before the user clicks on ok/cancel on the JFrameClass, but this line gets executed, while the window is displaying."); } }
I hope I am explaining it clearer,
Thanks,
- 11-06-2008, 05:30 PM #4the SOP should wait
You say:
Here's what happens
What happens when you execute the code? Does it do what you want? If not explain what it does and what you want it to do.
If by "halt" do you mean to interrupt the execution of a method in the middle of it where you show your comment.
That is not a good design. What you want to do is use listeners to handle the user clicks and have that listener code start a new method to do what you want done at that time.
- 11-06-2008, 05:52 PM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 13
As Norm already said, you should add listeners to the JFrame or w/e the user has to click on, and use that to call the method that executes the piece of code that you want it to (so after those comments in your main method)
I die a little on the inside...
Every time I get shot.
- 11-08-2008, 06:05 PM #6
JDialog
Basically, the 'command line' portion of your program will 'wait' due to the nature of threading, it is the thread from the main in the command line that enters the JWindow stuff, in general use JDialog as those tend to be 'modal' which means 'wait' as you are using the term.
I tried it recently, it works:Java Code:catch(java.security.NoSuchAlgorithmException inwskes) { // Object[] nullNames = {"first", "second", "third"}; String result = (String) javax.swing.JOptionPane.showInputDialog(new java.awt.Frame(), "NoSuchAlgorithmException: "+END_OF_LINE+exception.getMessage() + END_OF_LINE, "Hashing Algorithm not available - see message",javax.swing.JOptionPane.PLAIN_MESSAGE,null,nullNames,"first"); return new String("NoSuchAlgorithmException: " + new java.util.Date().toString() + result); }
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-09-2008, 04:36 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 11-09-2008, 04:40 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Similar Threads
-
command line args
By MarkWilson in forum NetBeansReplies: 3Last Post: 08-04-2008, 04:22 AM -
How to close an open JFrame window from a jsp page?
By kasisaiganesh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-27-2008, 07:29 PM -
Java command line
By agouzoul in forum New To JavaReplies: 2Last Post: 04-02-2008, 01:12 PM -
calling linux command line in java
By fangzhong in forum New To JavaReplies: 0Last Post: 02-03-2008, 05:24 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 01:23 AM
Bookmarks