/* Oct 23, 2017 */ package chapter12.exercises.lab12; import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; /** * * @author banhmisg9509 */ public class TemperatureConversion extends javax.swing.JFrame { private String Unit1; private String Unit2; private final TemperatureConverter tc; /** * Creates new form TemperatureConversion */ public TemperatureConversion() { initComponents(); this.Unit1 = (String) cbbTempUnits1.getSelectedItem(); this.Unit2 = (String) cbbTempUnits2.getSelectedItem(); this.tc = new TemperatureConverter(); } /** * 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") // private void initComponents() { lbStatus = new javax.swing.JLabel(); tfLeftResult = new javax.swing.JTextField(); tfRightResult = new javax.swing.JTextField(); lbEquals = new javax.swing.JLabel(); cbbTempUnits1 = new javax.swing.JComboBox<>(); cbbTempUnits2 = new javax.swing.JComboBox<>(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Temperature Converter"); setResizable(false); lbStatus.setFont(new java.awt.Font("Bitstream Vera Sans Mono", 1, 18)); // NOI18N lbStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lbStatus.setText("Temperature Conversion"); tfLeftResult.setHorizontalAlignment(javax.swing.JTextField.CENTER); tfRightResult.setHorizontalAlignment(javax.swing.JTextField.CENTER); lbEquals.setFont(new java.awt.Font("Bitstream Vera Sans", 1, 18)); // NOI18N lbEquals.setText("="); cbbTempUnits1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Celsius", "Fahrenheit", "Kelvin" })); cbbTempUnits1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbbTempUnits1ActionPerformed(evt); } }); cbbTempUnits2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Fahrenheit", "Celsius", "Kelvin" })); cbbTempUnits2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbbTempUnits2ActionPerformed(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() .addGap(38, 38, 38) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(cbbTempUnits1, 0, 111, Short.MAX_VALUE) .addComponent(tfLeftResult)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lbEquals) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(cbbTempUnits2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(tfRightResult, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)) .addContainerGap(44, Short.MAX_VALUE)) .addComponent(lbStatus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(26, 26, 26) .addComponent(lbStatus) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(tfLeftResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tfRightResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lbEquals)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cbbTempUnits1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cbbTempUnits2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(24, Short.MAX_VALUE)) ); tfLeftResult.getDocument().addDocumentListener(new SimpleDocumentListener() { @Override public void update(DocumentEvent e) { Runnable doUpdate = new Runnable() { @Override public void run() { tfRightResult.setText(tfLeftResult.getText()); //tfRightResult.setText(String.format("%.3f", processResult(Unit1, Unit2, tfLeftResult.getText()))); } }; SwingUtilities.invokeLater(doUpdate); } }); tfRightResult.getDocument().addDocumentListener(new SimpleDocumentListener() { @Override public void update(DocumentEvent e) { Runnable doAssit = new Runnable() { @Override public void run() { tfLeftResult.setText(tfRightResult.getText()); //tfLeftResult.setText(String.format("%.3f", processResult(Unit2, Unit1, tfRightResult.getText()))); } }; SwingUtilities.invokeLater(doAssit); } }); pack(); setLocationRelativeTo(null); }// private void cbbTempUnits1ActionPerformed(java.awt.event.ActionEvent evt) { this.Unit1 = (String) cbbTempUnits1.getSelectedItem(); } private void cbbTempUnits2ActionPerformed(java.awt.event.ActionEvent evt) { this.Unit2 = (String) cbbTempUnits2.getSelectedItem(); } private double processResult(String Unit1, String Unit2, String input) { double result = 0.0; if (Unit1.equals(Unit2) || input.isEmpty()) { return result; } switch (Unit1) { case "Celsius": if (Unit2.equals("Fahrenheit")) { result = tc.CelsiusToFahrenheit(tc.ValidateInput(input)); } else { result = tc.CelsiusToKelvin(tc.ValidateInput(input)); } break; case "Fahrenheit": if (Unit2.equals("Celsius")) { result = tc.FahrenheitToCelsius(tc.ValidateInput(input)); } else { result = tc.FahrenheitToKelvin(tc.ValidateInput(input)); } break; case "Kelvin": if (Unit2.equals("Celsius")) { result = tc.KelvinToCelsius(tc.ValidateInput(input)); } else { result = tc.KelvinToFahrenheit(tc.ValidateInput(input)); } break; default: break; } return result; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TemperatureConversion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TemperatureConversion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TemperatureConversion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TemperatureConversion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TemperatureConversion().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JComboBox cbbTempUnits1; private javax.swing.JComboBox cbbTempUnits2; private javax.swing.JLabel lbEquals; private javax.swing.JLabel lbStatus; private javax.swing.JTextField tfLeftResult; private javax.swing.JTextField tfRightResult; // End of variables declaration } @FunctionalInterface interface SimpleDocumentListener extends DocumentListener { void update(DocumentEvent e); @Override default void insertUpdate(DocumentEvent e) { update(e); } @Override default void removeUpdate(DocumentEvent e) { update(e); } @Override default void changedUpdate(DocumentEvent e) { update(e); } }