/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package deploy.common; import javax.swing.JFrame; import javax.swing.JOptionPane; /** * * @author Will */ public class ExperimentStartDialogue extends javax.swing.JDialog { public static final int MAX_CHARS = 10; public static final int MAX_DIGITS = 3; private boolean isExperimentStart = false; /** Creates new form ExperimentStartDialogue */ public ExperimentStartDialogue(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); this.setLocationRelativeTo(null); } public static boolean showDialogue(JFrame parent, StringBuffer initials, StringBuffer subjectID){ ExperimentStartDialogue myDialogue = new ExperimentStartDialogue(parent, true); myDialogue.setVisible(true); if(myDialogue.isExperimentStart){ initials.append(myDialogue.getInitials()); subjectID.append(myDialogue.getSubjectNumber()); } return myDialogue.isExperimentStart; } public String getInitials(){ return this.initialsTextField.getText().trim(); } public String getSubjectNumber(){ return this.subjectNumberSpinner.getValue().toString(); } /** 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { startButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); initialsTextField = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); subjectNumberSpinner = new javax.swing.JSpinner(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Run experiment..."); setResizable(false); startButton.setText("Start Experiment!"); startButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startButtonActionPerformed(evt); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); jLabel1.setText("Please enter your initials and subject number:"); subjectNumberSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 999, 1)); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(cancelButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(startButton)) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(initialsTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(subjectNumberSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 342, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(initialsTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(subjectNumberSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(startButton) .add(cancelButton)) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed this.isExperimentStart = true; if(this.initialsTextField.getText().trim().length() > MAX_CHARS) { JOptionPane.showMessageDialog(this, "Please limit initials to " + MAX_CHARS + " characters max."); return; } else if(this.initialsTextField.getText().trim().length() < 1){ JOptionPane.showMessageDialog(this, "Please enter your initials."); return; } else if((Integer.parseInt(this.subjectNumberSpinner.getValue().toString())) < 1){ JOptionPane.showMessageDialog(this, "Please enter a valid subject number (1-999)."); return; } this.setVisible(false); }//GEN-LAST:event_startButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed this.isExperimentStart = false; this.setVisible(false); }//GEN-LAST:event_cancelButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { //java.awt.EventQueue.invokeLater(new Runnable() { //public void run() { // ExperimentStartDialogue dialog = new ExperimentStartDialogue(new javax.swing.JFrame(), true); // dialog.addWindowListener(new java.awt.event.WindowAdapter() { // public void windowClosing(java.awt.event.WindowEvent e) { // System.exit(0); // } // }); // dialog.setVisible(true); //} //}); StringBuffer initials = new StringBuffer(ExperimentStartDialogue.MAX_CHARS); StringBuffer subjectID = new StringBuffer(ExperimentStartDialogue.MAX_CHARS); boolean isStart = ExperimentStartDialogue.showDialogue(null, initials, subjectID); if(isStart){ System.out.println(initials + " - " + subjectID); } System.exit(0); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancelButton; private javax.swing.JTextField initialsTextField; private javax.swing.JLabel jLabel1; private javax.swing.JButton startButton; private javax.swing.JSpinner subjectNumberSpinner; // End of variables declaration//GEN-END:variables }