Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:30:04+00:00 2026-06-01T03:30:04+00:00

So, I made a class that takes arrays and calculates a value from them.

  • 0

So, I made a class that takes arrays and calculates a value from them. I then decided (unknowingly) to incorporate it into a GUI interface. All went well until I noticed this strange error; one of the jtextfields (prarie) would not store text while the other (yard) does.

I looked around and found my problem similiar to mine on this site;
Updating text in a JTextField

But he had one that doesn’t work at all, where I have one that works and one that doesn’t.

The Code is here (it’s a bit long, but most of it is GUI), so hold your breath!:

import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Window {

/**
 * @param args
 */
private static int numb;
private static double micro, centi;
private static JTextField[] yard,prarie;
private static double[] charges,distances;

public static void main(String[] args) 
{
    //create a small dialog window to take in number of charged objects
    JPanel startup = new JPanel();
    JTextField many = new JTextField(5);
    startup.add(many);

    int result = JOptionPane.showConfirmDialog(null,startup , "Please Enter How Many Charged Objects are Being Evaluated", JOptionPane.OK_CANCEL_OPTION);
    many.requestFocusInWindow();

    //once ok is clicked, then the number input will be stored under 'numb' 
    //then proceed to inputFields
    if (result == JOptionPane.OK_OPTION) 
    {
        numb = Integer.parseInt(many.getText());
        inputFields();
    }
}

//this window opens the various JTextFields for input
public static void inputFields()
{


    //top JTextFields
    yard = new JTextField[numb];
    JPanel chargePanel = new JPanel();
    for(int x=0;x<numb;x++)
    {
        yard[x] =new JTextField(5);
        chargePanel.add(new JLabel("Charge "+ Integer.toString(x+1)+":"));
        chargePanel.add(yard[x]);
        chargePanel.add(Box.createVerticalStrut(15)); // a spacer
    }

    //bottom JTextFields
    prarie = new JTextField[numb-1];
    JPanel meterPanel = new JPanel(); 
    for(int x=0;x<numb-1;x++)
    {
        prarie[x]=new JTextField(5);
        meterPanel.add(new JLabel("Meters "+ Integer.toString(x+1)+":"));
        meterPanel.add(new JTextField(5));
        meterPanel.add(Box.createVerticalStrut(15)); // a spacer
    }

    //JCheckBoxes 
    JCheckBox isMicro = new JCheckBox("Charges are in terms of microCoulombs");
    JCheckBox isCm = new JCheckBox("Distances are in terms of centiMeters");

    JPanel chechBox = new JPanel();
    chechBox.add(isMicro);
    chechBox.add(Box.createVerticalStrut(20));
    chechBox.add(isCm);

    //Paste them all together into one window
    GridLayout gufi = new GridLayout(3,1);
    JPanel host = new JPanel(gufi);
    host.add(chargePanel);
    host.add(meterPanel);
    host.add(chechBox);
    int result1 = JOptionPane.showConfirmDialog(null, host, "Please Enter Charge and Distance Values", JOptionPane.OK_CANCEL_OPTION);

    //if ok is clicked, then go to 'printArr()' to print the JTextFields
    //then go to assign the values from the JTextFields to private double arrays 'yard' and 'prarie'
    if (result1 == JOptionPane.OK_OPTION) 
    {
        micro = (isMicro.isSelected())? Math.pow(10, -6): 1; 
        centi = (isCm.isSelected())? .01: 1;


        printArr();
        assign();
    }
}

//a makeshift method to print the value from the JTextFields 
//to fix the problem of why prarie wouldn't store numbers
public static void printArr()
{
    System.out.println("Charges are:");
    for(int x=0;x<numb;x++)
        System.out.print(yard[x].getText() + " ");
    System.out.println("Distances are:");
    for(int x=0;x<numb-1;x++)
        System.out.print(prarie[x].getText() + "  ");
}

//assigns values from JTextFields to the private double arrays 'yard' and 'prarie'
public static void assign()
{

    try {
        charges = new double[numb];
        for(int x=0;x<numb;x++)
            charges[x]=micro*Double.parseDouble(yard[x].getText().trim());

        distances = new double[numb-1];
        for(int x=0;x<numb-1;x++)
            distances[x]=centi*Double.parseDouble(prarie[x].getText().trim());

    } catch (NumberFormatException e) {
        e.printStackTrace();
        //inputFields();
    }
    calculate();
}
public static void calculate()
{


    JPanel sample = new JPanel();
    JTextField whichOne = new JTextField(5);
    sample.add(whichOne);

    int result = JOptionPane.showConfirmDialog(null,sample , "Please Enter Which Charged Object thy Wishs For", JOptionPane.OK_CANCEL_OPTION);
    whichOne.requestFocusInWindow();
    if (result == JOptionPane.OK_OPTION) 
    {
        int target = Integer.parseInt(whichOne.getText());



    }
}

}

Anyone who runs the code and takes the time to enter dummy values will see that ‘yard’ stores values while ‘prarie’ does not. Why is this?

*I’m pretty sure I’m overlooking obvious (as always).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:30:05+00:00Added an answer on June 1, 2026 at 3:30 am

    Change:

    meterPanel.add(new JTextField(5));
    

    to:

    meterPanel.add(prarie[x]);
    

    in the for loop for the prarie textfields

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a music class that will play a song. I have made onResume
This is a C++ class that I have made with n number of pointers.
I made a form class in c# that has a devexpress grid, a label
I have a custom-made view that extends the View class. I would like 2
I've got a class (which extends MovieClip) that loads in an external SWF (made
I am trying to make a recursive class that takes a string as a
I make some method, that takes digits and operands from stack and display it
I have a template class that I have made called hash . My template
I made a code that translate strings to match each word from the array
Hey so I'm making a serialization function that takes a base class pointer 'Joint'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.