|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
||||
|
||||
|
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.
__________________
- Hugh of Borg The first thing young borg are taught: Keep away from Microsoft software! |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > JFrame JTextArea Population Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|