Sinisterly
Fahrenheit to Celsius Converter - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE)
+--- Thread: Fahrenheit to Celsius Converter (/Thread-Fahrenheit-to-Celsius-Converter)

Pages: 1 2


Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

My first Swing Application in Java. IDE used : NetBeans 7.3 Beta 2. JDK version 6
A lot of code was generated by the IDE itself. Suggest improvements (I will update it with Celsius to Fahrenheit so other than this suggest improvements)

[Image: temp.png]

Code:
import javax.swing.JOptionPane; public class TemperatureConverter extends javax.swing.JFrame { public TemperatureConverter() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Fahrenheit Convertor"); jLabel1.setText("Enter Temperature in Fahrenheit :- "); jTextField1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { jTextField1KeyTyped(evt); } }); jLabel2.setText("Temperature in Celcius :- "); jButton1.setText("Convert"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Clear"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2)) .addGroup(layout.createSequentialGroup() .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(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE) .addComponent(jTextField2)))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, 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(jLabel2) .addComponent(jTextField2, 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(jButton1) .addComponent(jButton2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if(jTextField1.getText().trim().isEmpty()==false){ double temp=Double.parseDouble(jTextField1.getText()); double celTemp=(5*(temp-32))/9; jTextField2.setText(Double.toString(celTemp)); }else{ JOptionPane.showMessageDialog(null, "Required TextField Empty", "Text Field Empty", JOptionPane.ERROR_MESSAGE); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(null); jTextField2.setText(null); jButton1.setEnabled(false); } private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) { jButton1.setEnabled(true); } public static void main(String args[]) { 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(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(new Runnable() { public void run() { TemperatureConverter ob=new TemperatureConverter(); ob.setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration }



Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

My first Swing Application in Java. IDE used : NetBeans 7.3 Beta 2. JDK version 6
A lot of code was generated by the IDE itself. Suggest improvements (I will update it with Celsius to Fahrenheit so other than this suggest improvements)

[Image: temp.png]

Code:
import javax.swing.JOptionPane; public class TemperatureConverter extends javax.swing.JFrame { public TemperatureConverter() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Fahrenheit Convertor"); jLabel1.setText("Enter Temperature in Fahrenheit :- "); jTextField1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { jTextField1KeyTyped(evt); } }); jLabel2.setText("Temperature in Celcius :- "); jButton1.setText("Convert"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Clear"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2)) .addGroup(layout.createSequentialGroup() .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(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE) .addComponent(jTextField2)))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, 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(jLabel2) .addComponent(jTextField2, 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(jButton1) .addComponent(jButton2)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if(jTextField1.getText().trim().isEmpty()==false){ double temp=Double.parseDouble(jTextField1.getText()); double celTemp=(5*(temp-32))/9; jTextField2.setText(Double.toString(celTemp)); }else{ JOptionPane.showMessageDialog(null, "Required TextField Empty", "Text Field Empty", JOptionPane.ERROR_MESSAGE); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(null); jTextField2.setText(null); jButton1.setEnabled(false); } private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) { jButton1.setEnabled(true); } public static void main(String args[]) { 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(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TemperatureConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> java.awt.EventQueue.invokeLater(new Runnable() { public void run() { TemperatureConverter ob=new TemperatureConverter(); ob.setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration }



RE: Fahrenheit to Celsius Converter - Deque - 05-08-2013

I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


RE: Fahrenheit to Celsius Converter - Deque - 05-08-2013

I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


RE: Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated


RE: Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated


RE: Fahrenheit to Celsius Converter - Deque - 05-08-2013

(05-08-2013, 12:47 PM)Feurex Wrote:
(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated

I know that it is easier to use a GUI Builder, but what did you learn from it? Clicking buttons?

It is your decision about what you want to achieve. Do you want to learn Swing? Write the code by yourself.
Do you want a fast result and don't care about learning Swing? Use the GUI Builder.


RE: Fahrenheit to Celsius Converter - Deque - 05-08-2013

(05-08-2013, 12:47 PM)Feurex Wrote:
(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated

I know that it is easier to use a GUI Builder, but what did you learn from it? Clicking buttons?

It is your decision about what you want to achieve. Do you want to learn Swing? Write the code by yourself.
Do you want a fast result and don't care about learning Swing? Use the GUI Builder.


RE: Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

(05-08-2013, 01:34 PM)Deque Wrote:
(05-08-2013, 12:47 PM)Feurex Wrote:
(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated

I know that it is easier to use a GUI Builder, but what did you learn from it? Clicking buttons?

It is your decision about what you want to achieve. Do you want to learn Swing? Write the code by yourself.
Do you want a fast result and don't care about learning Swing? Use the GUI Builder.

I prefer slow learning with concrete concepts. I will code everything by myself from now on :lol:


RE: Fahrenheit to Celsius Converter - Psycho_Coder - 05-08-2013

(05-08-2013, 01:34 PM)Deque Wrote:
(05-08-2013, 12:47 PM)Feurex Wrote:
(05-08-2013, 12:17 PM)Deque Wrote: I suggest you try to do the same without a GUI Builder.
Reasons: You will learn how Swing actually works.
The resulting code will be better readable and more flexible.


Ok. But using an IDE make things easier a lot easier as we just need to write the main code and the rest is all generated

I know that it is easier to use a GUI Builder, but what did you learn from it? Clicking buttons?

It is your decision about what you want to achieve. Do you want to learn Swing? Write the code by yourself.
Do you want a fast result and don't care about learning Swing? Use the GUI Builder.

I prefer slow learning with concrete concepts. I will code everything by myself from now on :lol: