Search code examples
jtextfield

how to retrieve data from multiple JTextField


we are creating a project in netbeans which needs to read data from 3 JTextFields in order for us to use it in equations to solve a problem here the part we already finished:

package combustionofgaseousfuels;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CombustionOfGaseousFuels extends JFrame 
{
    private JTextField C;
    private JTextField H;
    private JTextField Excess;
    private JTextField Answer;
    private JTextField Theo;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private ButtonGroup rad1;
    private JRadioButton ORSAT;
    private JRadioButton WET;
    private JRadioButton BOTH;
    private JRadioButton c4;
    private JRadioButton c5;
    
    public CombustionOfGaseousFuels()
    { 
        super("Flue Gas Analyzer");
        
        ORSAT= new JRadioButton("ORSAT Analysis",false);
        WET= new JRadioButton("Wet Analysis",false);
        BOTH= new JRadioButton("Both",false);
        rad1= new ButtonGroup();
        rad1.add(ORSAT);
        rad1.add(WET);
        rad1.add(BOTH);
        
        panel1=new JPanel();
        panel1.setLayout(new GridLayout(1,3));
        panel1.add(ORSAT);
        panel1.add(WET);
        panel1.add(BOTH);
        
        label1= new JLabel("Enter the Number of Carbon and Hydrogen in Your Fuel:");
        label2= new JLabel("C:");
        label3= new JLabel("H:");
        label4= new JLabel("How Much is the %Excess Air?");
        label5=new JLabel("Your Flue Gas Analysis is:");
        label6=new JLabel("The Theoretical O2 is:");
        label7=new JLabel("What Flue Gas Analysis Do You Want?");
                
        C=new JTextField(5);
        C.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                        int carbon;
                        carbon= Integer.parseInt( ( (JTextField)
                        event.getSource()).getText());
                        
                         H=new JTextField(5);
                         H.addActionListener(
                            new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent event)
                                {
                                    int hydrogen;
                                    hydrogen=Integer.parseInt(((JTextField)
                                            event.getSource()).getText());
                                    
                                      Excess= new JTextField(5);
                                      Excess.addActionListener(
                                        new ActionListener(){
                                            @Override
                                            public void actionPerformed(ActionEvent event)
                                            {
                                                int excess;
                                                excess=Integer.parseInt(((JTextField)
                                                        event.getSource()).getText());
                                                
                                                {
                                                    if (ORSAT.isSelected()){
                                                        //double c = Integer.parseInt( carbon );
                                                        //double h = Integer.parseInt( hydrogen );
                                                        //double x = Integer.parseInt( excess );
        
                                                        double tc = (carbon*1);
                                                        double th = (hydrogen*1);
                                                        double tx = (excess/100);
        
                                                        double theo = tc + (th/4);
                                                        double xs = tx * theo;
                                                        double osupplied = (1+ tx)* theo;
                                                        double n = 3.761904762 * osupplied;
      
        
                                                        double total = (tc) + xs + n;
                                                        double carbondioxide = ((tc)/ total)*100;
                                                        double nitrogen = (n/ total)*100;
                                                        double oxygen = (xs/total)*100;
                                                    
                                                        Answer.setText(String.valueOf(total));
                                                    }
                                                }
                                            }
                                        });
                                }
                            });
                    }
                });
        
        
        
       
      
        
        
        
        Answer=new JTextField(10);
        Answer.setEditable(false);
        
        Theo=new JTextField(10);
        Theo.setEditable(false);
        
        Container project=getContentPane();
        project.setLayout(new GridLayout(15,1));
        project.add(label1);
        project.add(label2);
        project.add(C);
        project.add(label3);
        project.add(H);
        project.add(label4);
        project.add(Excess);
        project.add(label7);
        project.add(panel1);
        project.add(label5);
        project.add(Answer);
        project.add(label6);
        project.add(Theo);
        
        
        setSize (400,300);
        setVisible(true);
    }
    
    public static void main(String[] args) 
    {
        CombustionOfGaseousFuels analyzer=new CombustionOfGaseousFuels();
        analyzer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        
        
    }
    
}

There is no error in the codes but when we try to run it, it shows some errors, .please help us in pursuing our project.


Solution

  • public class CombustionOfGaseousFuels extends JFrame 
    {
        private JTextField C;
        private JTextField H;
        private JTextField Excess;
        private JTextField Answer;
        private JTextField Theo;
        private JPanel panel1;
        private JPanel panel2;
        private JPanel panel3;
        private JPanel panel4;
        private JPanel panel5;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JLabel label5;
        private JLabel label6;
        private JLabel label7;
        private ButtonGroup rad1;
        private JRadioButton ORSAT;
        private JRadioButton WET;
        private JRadioButton BOTH;
        private JRadioButton c4;
        private JRadioButton c5;
    
        public CombustionOfGaseousFuels()
        { 
            super("Flue Gas Analyzer");
    
            ORSAT= new JRadioButton("ORSAT Analysis",false);
            WET= new JRadioButton("Wet Analysis",false);
            BOTH= new JRadioButton("Both",false);
            rad1= new ButtonGroup();
            rad1.add(ORSAT);
            rad1.add(WET);
            rad1.add(BOTH);
    
            panel1=new JPanel();
            panel1.setLayout(new GridLayout(1,3));
            panel1.add(ORSAT);
            panel1.add(WET);
            panel1.add(BOTH);
    
            label1= new JLabel("Enter the Number of Carbon and Hydrogen in Your Fuel:");
            label2= new JLabel("C:");
            label3= new JLabel("H:");
            label4= new JLabel("How Much is the %Excess Air?");
            label5=new JLabel("Your Flue Gas Analysis is:");
            label6=new JLabel("The Theoretical O2 is:");
            label7=new JLabel("What Flue Gas Analysis Do You Want?");
    
            C=new JTextField(5);
            H=new JTextField(5);
            Excess= new JTextField(5);
            C.addActionListener(
                    new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent event)
                        {
                            final int  carbon;
                            carbon= Integer.parseInt( ( (JTextField)
                            event.getSource()).getText());
    
    
                             H.addActionListener(
                                new ActionListener(){
                                    @Override
                                    public void actionPerformed(ActionEvent event)
                                    {
                                        final int hydrogen;
                                        hydrogen=Integer.parseInt(((JTextField)
                                                event.getSource()).getText());
    
    
                                          Excess.addActionListener(
                                            new ActionListener(){
                                                @Override
                                                public void actionPerformed(ActionEvent event)
                                                {
                                                    int excess;
                                                    excess=Integer.parseInt(((JTextField)
                                                            event.getSource()).getText());
    
                                                    {
                                                        if (ORSAT.isSelected()){
                                                            //double c = Integer.parseInt( carbon );
                                                            //double h = Integer.parseInt( hydrogen );
                                                            //double x = Integer.parseInt( excess );
    
                                                            double tc = (carbon*1);
                                                            double th = (hydrogen*1);
                                                            double tx = (excess/100);
    
                                                            double theo = tc + (th/4);
                                                            double xs = tx * theo;
                                                            double osupplied = (1+ tx)* theo;
                                                            double n = 3.761904762 * osupplied;
    
    
                                                            double total = (tc) + xs + n;
                                                            double carbondioxide = ((tc)/ total)*100;
                                                            double nitrogen = (n/ total)*100;
                                                            double oxygen = (xs/total)*100;
    
                                                            Answer.setText(String.valueOf(total));
                                                        }
                                                    }
                                                }
                                            });
                                    }
                                });
                        }
                    });
    
    
    
    
    
    
    
    
            Answer=new JTextField(10);
            Answer.setEditable(false);
    
            Theo=new JTextField(10);
            Theo.setEditable(false);
    
            Container project=getContentPane();
            project.setLayout(new GridLayout(15,1));
            project.add(label1);
            project.add(label2);
            project.add(C);
            project.add(label3);
            project.add(H);
            project.add(label4);
            project.add(Excess);
            project.add(label7);
            project.add(panel1);
            project.add(label5);
            project.add(Answer);
            project.add(label6);
            project.add(Theo);
    
    
            setSize (400,300);
            setVisible(true);
        }
    
        public static void main(String[] args) 
        {
            CombustionOfGaseousFuels analyzer=new CombustionOfGaseousFuels();
            analyzer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    
        }
    
    }