Results 1 to 5 of 5
  1. #1
    wavefunction is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Passing arguments from java to python

    Hey all,

    I'm relatively new to Java, so I've been bumbling about a bit. I'm by no means a beginner programmer, but all of my programming up to this point has been strictly computational (Fortan, Python, C/C++). I've run into a slight problem in my latest program... Just wondering if anyone could help me out.


    I've been working on a program to analyze titration data, all written in python. The program works great, but I'm looking to make it a bit more elegant, hence the java.

    Just for a bit of background, the proper invocation of the program is as such:

    $python titrations2_1.py <pHfile> <volfile>

    Where pHfile and volfile are text files containing data points


    What I want to do is make this file input a but more elegant. I know that I hate staring at cmd all day, so I don't want my users to have to. My goal is to input the name of the text files into a JForm and then somehow pass those to my python script.


    What I've tried so far:

    Really the only thing that I've been able to think of is to have java output these files as a formatted command in cmd. I've been using NetBeans to create to GUI, but I cant seem to get the syntax for taking to text in the text fields and then in turn creating a system command with it.


    Any help would be great. Like I said, I'm rather new to java, and any help with either the method I described above or to find a new one would be much appreciated.


    Thanks,

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Why not write the gui in Python?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This may be helpful: Executing a Command | Example Depot

    If that snippet type will work it should be fairly simple to get the files from the textfield inputs with JTextFields getText() method and then build the command from the input. All this can be done in an action listener attached to a button.

  4. #4
    wavefunction is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    So I tried that, and I'm sure that it works under normal circumstances, but for some reason, when I work with NetBeans, it just throws around errors.

    Here's the situation:

    I go into NetBeans, create a new Java Application, and uncheck the "create main class". Then I create a JForm and put all of the nessicary TextFields, Labels and Buttons. I then change the Variable names to suit what they are being used for (pHtextField, ect...). Then I right click on the button and create an event. Then I type the following

    Java Code:
    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
            String pHFile=pHTextField.getText();
            String volFile=volTextField.getText();
            String[] commands=new String[]{"python titrations2_2.py",
            pHFile, volFile};
            try {
                Process child = Runtime.getRuntime().exec(commands);
            } catch (IOException e){
            }
            }

    An obvious error arises with the IOException, because I haven't yet imported java.io.*. So I do that and then it starts telling me that it can't find my TextFields, as in the variable doesn't exist.

    The entire code generated is as follows:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Data_Input.java
     *
     * Created on Jul 10, 2011, 10:04:36 AM
     */
    package source;
    import java.io.*
    
    /**
     *
     * @author David
     */
    public class Data_Input extends javax.swing.JFrame {
    
        /** Creates new form Data_Input */
        public Data_Input() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jFrame1 = new javax.swing.JFrame();
            pHTextField = new javax.swing.JTextField();
            volTextField = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            submitButton = new javax.swing.JButton();
    
            jFrame1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
            jFrame1.getContentPane().setLayout(jFrame1Layout);
            jFrame1Layout.setHorizontalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            jFrame1Layout.setVerticalGroup(
                jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Titration Data Input");
    
            jLabel1.setText("pH Data File");
    
            jLabel2.setText("Volume Data File");
    
            submitButton.setText("Submit");
            submitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    submitButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(pHTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(volTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(66, 66, 66)
                            .addComponent(submitButton)))
                    .addContainerGap(184, Short.MAX_VALUE))
            );
    
            layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {pHTextField, volTextField});
    
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(pHTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(volTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGap(18, 18, 18)
                    .addComponent(submitButton)
                    .addContainerGap(202, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
            String pHFile=pHTextField.getText();
            String volFile=volTextField.getText();
            String[] commands=new String[]{"python titrations2_2.py",
            pHFile, volFile};
            try {
                Process child = Runtime.getRuntime().exec(commands);
            } catch (IOException e){
            }
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Data_Input().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JFrame jFrame1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField pHTextField;
        private javax.swing.JButton submitButton;
        private javax.swing.JTextField volTextField;
        // End of variables declaration
    }

    Again, Any help would be great.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's the exact error messages? Also, some of the code is cut off for met, but you have the attached action listener in the same class as the text fields, correct?

    A few things, the handler method for ActionListener should be actionPerformed(ActionEvent e), it's important that it overrides and has the same name. Then you will want to add an action listener to the button

    Java Code:
    button.addActionListener(this)
    
    Or
    
    button.addActionListener(Listener)
    You can make the main class implement ActionListener, and then use the this option.

    It may be helpful to read this: http://download.oracle.com/javase/tu...nts/intro.html
    Last edited by sunde887; 07-10-2011 at 05:42 PM.

Similar Threads

  1. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 12:01 AM
  2. Problem passing arrayed arguments to a method
    By vcl in forum New To Java
    Replies: 1
    Last Post: 06-27-2011, 08:04 PM
  3. Passing arguments to jar file with a pipe
    By clover in forum New To Java
    Replies: 7
    Last Post: 06-27-2011, 05:36 PM
  4. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 04:00 PM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 12:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •