Reputation: 191
I tried to create a java GUI which displays a multiplication table of a number input by the user in a text area
The problem is, the results are not being displayed in the text area completely. For example if I input the number 3, only "30" is being displayed in the text area. But the result is completely being displayed in the output monitor i.e..by using System.out.print method. Is is not possible to display the result in a text area should I use any other component. Help!
Here's the coding I've done
int input_val = Integer.parseInt(InputNum.getText().trim());
int c=input_val*10;
int i;
for(i=input_val;i<=c;i=i+input_val)
{
TableOutputArea.setText(i+"\n");
System.out.print(i+"\n");
}
Output by the monitor:
3
6
9
12
15
18
21
24
27
30
Upvotes: 0
Views: 1627
Reputation: 394
Your problem is the following statement:
TableOutputArea.setText(i+"\n");
This statement clears the text which is in the TextArea and set the new text into it. So just 30 is inserted in your TextArea. To solve this you should use
TableOutputArea.append()
It appends text to the end of the String in the TextArea
Upvotes: 2
Reputation: 24626
As stated in my comments, use JTextArea.append() instead of using JTextArea.setText(). JTextArea.append()
allows you to add, more text to the existing content of the JTextArea
, unlike JTextArea.setText()
, which simply replaces the new text, with the previous text. Here is one small example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaExample {
private static final int GAP = 5;
private JTextField tField;
private JTextArea tArea;
private void displayGUI() {
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(GAP, GAP));
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
JLabel label = new JLabel("Enter number: ", JLabel.CENTER);
tField = new JTextField(10);
tField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int number = Integer.parseInt(tField.getText());
tArea.setText("");
for (int i = 1; i <= 10; ++i) {
tArea.append("" + number + " X " + i + " = " + (i * number) + "\n");
}
}
});
upperPanel.add(label);
upperPanel.add(tField);
tArea = new JTextArea(10, 10);
contentPane.add(upperPanel, BorderLayout.PAGE_START);
contentPane.add(tArea, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JTextAreaExample().displayGUI();
}
});
}
}
OUTPUT:
EDIT:
Though on the other hand, if you want the numbers to be properly aligned, one below each other, you can modifly the single line (JTextArea.append(...)
) with this line tArea.append(String.format("%-3d X %-3d = %-3d\n", number, i, (number * i)));
Upvotes: 2
Reputation: 920
You have to use TableOutputArea.append(i+"\n");
instead of the setText()
method.
Upvotes: 3