Results 1 to 8 of 8
  1. #1
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default 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);
             }
        }
    }
    Please advise...

    Thanks,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    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?

  3. #3
    new_2_java is offline Member
    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    0

    Default

    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.");
       }
     }
    So, by "halt" I mean that the SOP should wait, untle the user clicks on ok/cancel on JFrameClass and only then the SOP should be executed.

    I hope I am explaining it clearer,

    Thanks,

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    the SOP should wait
    What's an SOP?

    You say:
    Here's what happens
    But I don't see an explanation of what is happening. You posted some code.
    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.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb 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);
    }
    Use of strings that have no detectable use are a consequence of where the JDialog was being used and should be replaced with something you can use.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    What's an SOP?
    I've pointed this several times on few members. Our thread starter use a shorten form for System.out.println() as SOP. No one cannot identify at once it is. Because it's not a valid/standard abbreviation.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Thumbs down

    Quote Originally Posted by new_2_java View Post
    So, by "halt" I mean that the SOP should wait, untle the user clicks on ok/cancel on JFrameClass and only then the SOP should be executed.
    Java is sequential program. Statement execute after the previous one is completed. So that println() not executed until, showJFrameClass(), method is completed. Carefully read Norms' replay.

Similar Threads

  1. command line args
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 08-04-2008, 03:22 AM
  2. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 06:29 PM
  3. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  4. calling linux command line in java
    By fangzhong in forum New To Java
    Replies: 0
    Last Post: 02-03-2008, 04:24 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •