Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    I've just come to OPP world from C. So, it will take time to learn OPP to optimize code in java. Your code is excellent and perfect application and example of of OPP so far I understand.

    I thought just to open multiple programs so I wrote that bad code. I would try to combine the 3 separate programs into a single program using classes but I would never reached to such code (yours). Many many thanks to you for your effort and waste your time for me.

    I read the whole code and tried to understand, some thing is clear to me and something is not fully clear (understand but having some confusion). So, if you make me understand the following code, it will be very helpful for me.


    Java Code:
      
    public JPanel getMainPanel() {
        return mainPanel;
    
     public void setTextFieldText(String text) {
        tf.setText(text);

    Java Code:
    JDialog dialog = new JDialog(frm, "Prog 1", false);
            dialog.getContentPane().add(prog.getMainPanel());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);

    Not fully clear why these are used

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your pr1 and pr2 classes appeared to me to be the exact same GUIs the only difference being the value of text in the JTextField. So to simplify things as much as possible, it would be best to create one single class to create the GUI, but allow the calling code to change the JTextField value to whatever is needed. That is what this public method does:
    Java Code:
    public void setTextFieldText(String text) {
        tf.setText(text);
    }
    It accepts as a parameter a String and sets the tf JTextField's text to that String.

    This way, if you want to give your prX (I called the class Prog1) dialog's jtextfield the "ls -1" string, you can create it easily like so:
    Java Code:
    Prog1 prog = new Prog1(); // creates the jpanel
    prog.setTextFieldText("ls -l"); // sets the textfield held by the jpanel.
    The next bit of code here creates a JDialog and places the JPanel created in Prog1 into it. It is usually best for your application to have one JFrame and then if it needs to open other windows, create dialogs (JDialogs) to display these Windows. To keep my apps as flexible as possible, I usually have my GUI classes all create JPanels. Then if I want to display the JPanel in a JFrame, I simply create a JFrame on the fly, add the JPanel created by my app into the JFrame's contentPane and display it. If later, I decide to show the GUI in an applet, all I have to do is create a JApplet, and place my JPanel in the applet's contentPane and similarly for a JDialog. Again, this is all done with the goal of increasing the flexibility and usability of the code. See comments below:
    Java Code:
          // get the text of the button pressed
          String actionCommand = ae.getActionCommand();
    
          // get the String to put into the "tf" JTextField from the btnTextFieldMap created specifically to hold this information
          String textString = btnTextFieldMap.get(actionCommand);
    
          // create a Prog1 object.  This will create a JPanel, mainPanel, that holds all of the GUI elements
          Prog1 prog = new Prog1();
    
          // set the tf JTextField with the text String extracted from the Map above
          prog.setTextFieldText(textString);
    
          // Create a new JDialog to hold the GUI JPanel.  The false makes it a non-modal dialog
          JDialog dialog = new JDialog(frame, "Program", false);
    
          // get the dialog's contentPane and add my Prog1's JPanel (obtaiend via getMainPanel the method) into it
          dialog.getContentPane().add(prog.getMainPanel());
    
          // pack it so that the layout managers are told to layout the components
          dialog.pack();
    
          // center it on the screen
          dialog.setLocationRelativeTo(null);
    
          // display it
          dialog.setVisible(true);
    This last method again lets me create a JPanel in my Prog1 application (called mainPanel), and then extract this JPanel when I desire to display it in a root container such as a JDialog, JFrame, or JApplet. It allows my Prog1 class to avoid subclassing JPanel which you'll find out is a good thing.
    Java Code:
    public JPanel getMainPanel() {
        return mainPanel;
    }

  3. #23
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    @Fubarable
    Thanks a lot for helping me.
    I understood your code and explanation and I made my program based on your example. Now I want to know a litte from you.

    I need to open a new Terminal window (by clicking a button) where a command will be executed.

    example:
    by clicking a button a Terminal will be opened with "ls -l" command.

  4. #24
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    I did it by using dtterm/xterm as below

    Java Code:
    Process p = Runtime.getRuntime().exec("xterm -e ./script1.sh variable1");
    It opens a new terminal window and execute script1.sh script taking variable1 as a variable for the script.
    But the window disappears just after executing and viewing the output. I want to close the terminal window manually. i.e, automatic exit of the window should be stopped.

  5. #25
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    I did all the things. The problem was in my script. Sorry for the inconvenience.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. multiple program
    By mm2236 in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-02-2009, 12:27 PM
  2. Multiple window
    By shoeb83 in forum New To Java
    Replies: 1
    Last Post: 08-30-2009, 10:59 PM
  3. Replies: 6
    Last Post: 02-24-2009, 02:50 PM
  4. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 AM
  5. Doubt in simultaneous 'implementation' and 'extension'
    By ajaygargnsit in forum New To Java
    Replies: 2
    Last Post: 12-20-2007, 09:33 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
  •