Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How do I loop the text for my GUI?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I loop the text for my GUI?

    private void enterData()
    	{
     
     
    	    carRegistration = registrationField.getText(); // car registration is what users type in the registration field
     
    	    carAge = Integer.parseInt((String) ageCombo.getSelectedItem()); // the age is to be selected from the combo box
     
    	    boolean accident = accidentCheckBox.isSelected(); // the accident status depends on whether or not the checkbox is ticked
     
    	    discount = Fee*0.25; // declaring that the discount is 25% of the fee
     
    	    int[] cars; // declare an array called cars
     
    	    cars = new int[ 10 ]; // declare the number of cars
     
    	    if(accidentCheckBox.isSelected()) // if the accident checkbox is displayed, the accident status should display as YES
    	       Accident = "YES";
    	    else
    	       Accident = "NO"; // if the accident checkbox is displayed, the accident status would display as NO
     
     
     
     
     
    	    if(carAge>5)
    	      Fee = 350.00; // if the car is above 5 years of age, the fee would be $350.00
     
     
     
     
     
    	    if(carAge<=5)
    	      Fee = 200.00; // if the car is under or equal to 5 years of age, the fee would be $200.00
     
     
     
     
     
    	    if(accidentCheckBox.isSelected())
    		  Fee = Fee; // if the car has been in an accident, the fee will not change
    		else
    		  Fee = Fee - discount; // if the car has not been in an accident, the fee will be discounted
     
     
     
            if(registrationField.getText().equals(""))
            {
    			JOptionPane.showMessageDialog(this, "Please enter a car's registration"); // error message if car registration isn't entered
    	    }
     
            else
     
            {
     
     
    	    textArea.setText(String.format("%10s %10s %10s %10s", "Registration","Age", "Accident", "Fee\n")); // placing the titles of the four categories of information into text
     
    	    textArea.append(String.format("--------------------------------------------\n")); // underline separates the titles of categories from the information of each car
     
    	    textArea.append(String.format("%10s %10s %10s %10s", carRegistration, carAge, Accident, "$" +Fee)); // the four categories information displayed into text for each car
     
    	}
     
    	registrationField.setText(""); // reset the registration field when data is entered
     
        ageCombo.setSelectedIndex(0); // reset the combo box to the default number selected when data is entered
     
    	accidentCheckBox.setSelected(false); // reset the check box to its default state when data is entered
     
    	registrationField.requestFocus();

    I just want to loop the Registration, Age, Accident and Fee displayed for a car 10 times.


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I loop this text into my GUI?

    private void enterData()
    	{
     
     
    	    carRegistration = registrationField.getText(); // car registration is what users type in the registration field
     
    	    carAge = Integer.parseInt((String) ageCombo.getSelectedItem()); // the age is to be selected from the combo box
     
    	    boolean accident = accidentCheckBox.isSelected(); // the accident status depends on whether or not the checkbox is ticked
     
    	    discount = Fee*0.25; // declaring that the discount is 25% of the fee
     
    	    int[] cars; // declare an array called cars
     
    	    cars = new int[ 10 ]; // declare the number of cars
     
    	    if(accidentCheckBox.isSelected()) // if the accident checkbox is displayed, the accident status should display as YES
    	       Accident = "YES";
    	    else
    	       Accident = "NO"; // if the accident checkbox is displayed, the accident status would display as NO
     
     
     
     
     
    	    if(carAge>5)
    	      Fee = 350.00; // if the car is above 5 years of age, the fee would be $350.00
     
     
     
     
     
    	    if(carAge<=5)
    	      Fee = 200.00; // if the car is under or equal to 5 years of age, the fee would be $200.00
     
     
     
     
     
    	    if(accidentCheckBox.isSelected())
    		  Fee = Fee; // if the car has been in an accident, the fee will not change
    		else
    		  Fee = Fee - discount; // if the car has not been in an accident, the fee will be discounted
     
     
     
            if(registrationField.getText().equals(""))
            {
    			JOptionPane.showMessageDialog(this, "Please enter a car's registration"); // error message if car registration isn't entered
    	    }
     
            else
     
            {
     
     
    	    textArea.setText(String.format("%10s %10s %10s %10s", "Registration","Age", "Accident", "Fee\n")); // placing the titles of the four categories of information into text
     
    	    textArea.append(String.format("--------------------------------------------\n")); // underline separates the titles of categories from the information of each car
     
    	    textArea.append(String.format("%10s %10s %10s %10s", carRegistration, carAge, Accident, "$" +Fee)); // the four categories information displayed into text for each car
     
    	}
     
    	registrationField.setText(""); // reset the registration field when data is entered
     
        ageCombo.setSelectedIndex(0); // reset the combo box to the default number selected when data is entered
     
    	accidentCheckBox.setSelected(false); // reset the check box to its default state when data is entered
     
    	registrationField.requestFocus();

    I want to loop the carRegistration, carAge, Accident and Fee info 10 times for 10 cars.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: How do I loop the text for my GUI?

    Threads merged.

    Please don't post multiple threads on the same topic.

    Have you considered a for() loop?

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    Joey231 (September 22nd, 2014)

  5. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop the text for my GUI?

    I've tried for (int Counter = 0; Counter <= 9; Counter++), but on the GUI no differences were made. no new lines of text were entered, if I type in new data into my fields it'll just replace the previous line of text.

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: How do I loop the text for my GUI?

    Variable names should begin with lowercase letters. The word 'Counter' looks like a class name or a data type rather than a variable name.

    Other general comments: the code is inconsistently formatted, the lines are too long (keep to <80 columns), and the method you posted is doing too much. A method name should describe what it does, it should do just that one thing, it should do it well, and it should do nothing else.

    I don't see a for() loop in the code you posted. Are you talking about different code? If you want help with code, post the code you want help with.

  7. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I loop the text for my GUI?

    No that is the code I'm working on, I deleted the for() statement because it didn't help

    --- Update ---

    Well I just want to loop this bit:

    for (int counter = 0; counter <= 9; counter++)
     
    {
     
    textArea.setText(String.format("%10s %10s %10s %10s", "Registration", "Age", "Accident", "Fee\n")); // placing the headings of the four categories of information into text
     
    textArea.append(String.format("--------------------------------------------\n")); // underline separates the titles of categories from the information of each car
     
    textArea.append(String.format("%10s %10s %10s %10s", carRegistration, carAge, Accident, "$" +Fee)); // the four categories information displayed into text for each car
     
    }

    I put the for statement in but it does not loop

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: How do I loop the text for my GUI?

    Posted before you modified your last post:

    I tried to be clear: Post the code you want help with rather an invitation for us to write code for you.

    Since you're having diffculty writing a for() loop, start by writing a simple for() loop that does something 10 times - prints out a result, for example. Then modify that for() loop to call a method that does something simple - prints out a result, for example. Then write and call another method from the for() loop that does something simple but returns a result that is (you guessed it) then printed out by the calling method.

    Come back when you need help with any of those for() loops.

    Added after you modified your post:

    Make the above loop runnable, put it in a method that is called by a main() method, and figure out why it doesn't loop (if it doesn't).

  9. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How do I loop the text for my GUI?

    If you call setText(...) inside the loop then the text will be reset in each iteration. You can use the append(...) method inside the loop, but the setText should be outside of the loop.

Similar Threads

  1. JAVA GUI and FILE TEXT
    By Jelena in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 1st, 2014, 04:51 AM
  2. Text output in GUI
    By andreas81 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 26th, 2013, 09:36 AM
  3. simple gui to check if text is a palindrome
    By Rakshith in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 25th, 2013, 10:46 AM
  4. Parsing from a GUI text field
    By jdubicki in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2012, 03:46 PM
  5. Adding entered text from one GUI to another
    By fride360 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 24th, 2011, 01:08 PM