Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2006, 06:12 PM
gregt78 gregt78 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Waukegan, IL
Posts: 14 gregt78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 8 m 22 sec
Reputation Power: 0
JFrame JTextArea Population Problem

I am working on a basic program that utilizes JFrames. The point of the project is to have the user enter a number (n) and press the button. Then the JTextArea, will populate with the "n" number of prime numbers. For example n = 4, would populate 2,3,5,7. I have the basic JFrame and when you enter a value and press generate, it puts that entry in the JTextArea. I also have the prime number method (primeNum) that can print the right amount of prime numbers. I have modified my prime number output to be a string and then using textArea.append(stringNum); that doesn't work. Any suggestions or help would be greatly appreciated!

Thanks,
Greg

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

public class Ch14Ex4 extends JFrame implements ActionListener
{ // start Ch14Ex4



//----------------------------------
//    Data Members
//----------------------------------

	/**
	 * Input number of prime numbers to display
     */
	private static int n = 1000;

    /**
     * Constant for empty string
     */
    private static final String EMPTY_STRING = "";

    /**
     * Constant for platform specific newline
     */
    private static final String NEWLINE = System.getProperty("line.separator");

    /**
     * The Swing button for Generating Numbers
     */
    private JButton generateButton;

    /**
     * The JTextField for the user to enter a text
     */
    private JTextField inputLine;

    /**
     * The JTextArea for displaying the entered text
     */
    private JTextArea  textArea;


//----------------------------------
//      Main method
//----------------------------------
    public static void main(String[] args) {
        Ch14Ex4 frame = new Ch14Ex4();
        frame.setVisible(true);
    }


//----------------------------------
//    Constructors
//----------------------------------

    /**
     * Constructor
     */
    public Ch14Ex4() {
        Container contentPane;

        //set the frame properties
        setSize      (300, 250);
        setResizable (false);
        setTitle     ("Program Ch14TextFrame3");
        setLocation  (150, 250);

        contentPane = getContentPane( );
        contentPane.setLayout(new FlowLayout());


        textArea = new JTextArea();
        textArea.setColumns(22);
        textArea.setRows(8);
        textArea.setBorder(BorderFactory.createTitledBorder(+n+" Prime Numbers"));
		textArea.setEditable(false);
        contentPane.add(textArea);

        JScrollPane scrollText= new JScrollPane(textArea);
		scrollText.setSize(200, 135);
		scrollText.setBorder(BorderFactory.createLineBorder(Color.red));
        contentPane.add(scrollText);


        inputLine = new JTextField();
        inputLine.setColumns(22);
        contentPane.add(inputLine);
        inputLine.addActionListener(this);

        //create and place a button on the frame
        generateButton = new JButton("Generate");
        generateButton.setSize(80, 30);
        contentPane.add(generateButton);
        generateButton.addActionListener(this);

        //register 'Exit upon closing' as a default close operation
        setDefaultCloseOperation( EXIT_ON_CLOSE );
    }


    public void actionPerformed(ActionEvent event) {

        if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();

            if (clickedButton == generateButton) {
              addText(inputLine.getText());
            } else {
                clearText( );
            }

        } else { //the event source is inputLine
            addText(inputLine.getText());
        }
    }

    private void addText(String newline) {
        textArea.append(newline + NEWLINE);
        inputLine.setText("");
    }

    private void clearText( ) {
        textArea.setText(EMPTY_STRING);
        inputLine.setText(EMPTY_STRING);
    }



public void primeNum(int n)
{ // start primeNum
//Program to generate prime numbers from 1 to n

        int num1,num2;
        int flag=0; //Checks to see if the number in question is prime or not
        String stringNum;

        for(num1=2;num1<=n;num1++)
            { // start for 1
                flag=0;
                for(num2=2;num2<num1;num2++) //Remove the num2<=num1 and let it be as shown
                    { // start for 2
                        if(((num1%num2)==0) & num2!=1) //Thus you can remove the third condition
                        { // start if 1
                            flag=1; //Flag set to indicate that the number is prime
                        } // end if 1
                    } // end for 2
                    if(flag==0)
                    { // start if 2
                    stringNum = String.valueOf(num1);
					textArea.append(stringNum);
					//System.out.println(num1);
                    } // end if 2
            } // end for 1
} // end PrimeNum

} // end Ch14Ex4

Reply With Quote
  #2  
Old August 16th, 2006, 01:51 AM
Hugh of Borg's Avatar
Hugh of Borg Hugh of Borg is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Switzerland
Posts: 764 Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Hugh of Borg User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 22 h 44 m 31 sec
Reputation Power: 499
ok. 3 things:
1. it would help to know what exactly does not work and what happens instead.
2. you never produce any numbers in your application
3. your event handler is a bit oversized. just check if the button was klicked and then produce the numbers. Ignore any other possibilities for now. "textarea.append()" is perfecly ok and should work but the only thing you do is appending the inputline to the textarea.
Comments on this post
gregt78 agrees: Thanks for your feedback, it helped me out.
__________________
- Hugh of Borg

The first thing young borg are taught: Keep away from Microsoft software!

Reply With Quote
  #3  
Old August 16th, 2006, 05:52 PM
gregt78 gregt78 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Waukegan, IL
Posts: 14 gregt78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 8 m 22 sec
Reputation Power: 0
Thumbs up

Thanks so much for your help. I got it working. It's not the prettiest, but it works. For reference I have included the working version below. Hope it can serve some benefit to someone else.

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

public class Ch14Ex4 extends JFrame implements ActionListener
{ // start Ch14Ex4



//----------------------------------
//    Data Members
//----------------------------------

	/**
	 * Input number of prime numbers to display
     */
	private static int n;

    /**
     * Constant for empty string
     */
    private static final String EMPTY_STRING = "";

    /**
     * Constant for platform specific newline
     */
    private static final String NEWLINE = System.getProperty("line.separator");

    /**
     * The Swing button for Generating Numbers
     */
    private JButton generateButton;

    /**
     * The JTextField for the user to enter a text
     */
    private JTextField inputLine;

    /**
     * The JTextArea for displaying the entered text
     */
    private JTextArea  textArea;


//----------------------------------
//      Main method
//----------------------------------
    public static void main(String[] args) {
        Ch14Ex4 frame = new Ch14Ex4();
        frame.setVisible(true);
    }


//----------------------------------
//    Constructors
//----------------------------------

    /**
     * Constructor
     */
    public Ch14Ex4()
    { // start Ch14Ex4
        Container contentPane;

        //set the frame properties
        setSize      (300, 250);
        setResizable (false);
        setTitle     ("Program Ch14TextFrame3");
        setLocation  (150, 250);

        contentPane = getContentPane( );
        contentPane.setLayout(new FlowLayout());


        textArea = new JTextArea();
        textArea.setColumns(22);
        textArea.setRows(8);
        textArea.setBorder(BorderFactory.createTitledBorder(+n+" Prime Numbers"));
		textArea.setEditable(false);
        contentPane.add(textArea);

        JScrollPane scrollText= new JScrollPane(textArea);
		scrollText.setSize(200, 135);
		scrollText.setBorder(BorderFactory.createLineBorder(Color.red));
        contentPane.add(scrollText);


        inputLine = new JTextField();
        inputLine.setColumns(22);
        contentPane.add(inputLine);
        inputLine.addActionListener(this);

        //create and place a button on the frame
        generateButton = new JButton("Generate");
        generateButton.setSize(80, 30);
        contentPane.add(generateButton);
        generateButton.addActionListener(this);

        //register 'Exit upon closing' as a default close operation
        setDefaultCloseOperation( EXIT_ON_CLOSE );
    } // end Ch14Ex4


    public void actionPerformed(ActionEvent event) 
    {

        if (event.getSource() instanceof JButton) 
        {
            JButton clickedButton = (JButton) event.getSource();

 			textArea.setText(EMPTY_STRING); // clears the textArea


            if (clickedButton == generateButton) 
            {

              String uInput = inputLine.getText();
              n = Integer.parseInt(uInput);
              int num1,num2;
			          int flag=0; //Checks to see if the number in question is prime or not
			          String stringNum2;

			          for(num1=2;num1<=n;num1++)
			              { // start for 1
			                  flag=0;
			                  for(num2=2;num2<num1;num2++) //Remove the num2<=num1 and let it be as shown
			                      { // start for 2
			                          if(((num1%num2)==0) & num2!=1) //Thus you can remove the third condition
			                          { // start if 1
			                              flag=1; //Flag set to indicate that the number is prime
			                          } // end if 1
			                      } // end for 2
			                      if(flag==0)
			                      { // start if 2
			                      stringNum2 = String.valueOf(num1);
			  					textArea.append(stringNum2 + NEWLINE);
			  					//System.out.println(num1);
			                      } // end if 2
            				} // end for 1


            } 
            else 
            {
             	textArea.setText(EMPTY_STRING);
        		inputLine.setText(EMPTY_STRING);
            }

        } 
        else 
        { //the event source is inputLine

         textArea.setText(EMPTY_STRING); // clears the textArea

            String uInput = inputLine.getText();
			              n = Integer.parseInt(uInput);
			              int num1,num2;
						          int flag=0; //Checks to see if the number in question is prime or not
						          String stringNum2;

						          for(num1=2;num1<=n;num1++)
						              { // start for 1
						                  flag=0;
						                  for(num2=2;num2<num1;num2++) //Remove the num2<=num1 and let it be as shown
						                      { // start for 2
						                          if(((num1%num2)==0) & num2!=1) //Thus you can remove the third condition
						                          { // start if 1
						                              flag=1; //Flag set to indicate that the number is prime
						                          } // end if 1
						                      } // end for 2
						                      if(flag==0)
						                      { // start if 2
						                      stringNum2 = String.valueOf(num1);
						  					textArea.append(stringNum2 + NEWLINE);
						  					//System.out.println(num1);
						                      } // end if 2
            				} // end for 1
        }
    }


} // end Ch14Ex4

Last edited by gregt78 : August 16th, 2006 at 05:54 PM. Reason: typo

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > JFrame JTextArea Population Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT