|
Why is the Text in the JTextArea getting replaced instead of getting added?
|
Posted: Dec 10, 2018 3:02 AM
|
|
Advertisement
|
"text.append(" - " + words.get(i) + " (" + count.get(i) + " Mal)" + "\n" + " [ IDN: "+ identNum + " ]");"
Even tho I have ".append", the words in window2 are getting replaced if I repeat adding words from window1 into window2
Here's the full code of the programm if someone wants to test it:
package Files;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Shoppen {
public static void main(String[] args) {
//Window 1
//Windowsettings
JFrame frame1 = new JFrame("Selection");
frame1.setVisible(true);
frame1.setSize(500, 500);
frame1.setLocationRelativeTo(null);
frame1.setResizable(false);
//Ober e MenuBar
JMenuBar mb = new JMenuBar();
JMenu i1 = new JMenu("add article");
JMenu i2 = new JMenu("checkout");
mb.add(i1);
mb.add(i2);
frame1.setJMenuBar(mb);
frame1.s etDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel te2 = new JLabel("Welcome", JLabel.CENTER);
frame1.add(te2);
//Items
String[] is1 = {"Pizza", "Milk", "Breat", "Water", "Apple", "Rice", "Tomato", "Sausage", "Banana", "Yogurt"};
int[] identNum = {1, 2, 3, 4, 5, 6, 7, 8, 9 , 10};
double[] pl1 = {1.61, 0.75, 1.22, 0.42, 0.24, 1.52, 0.31, 1.47, 0.45, 1.86};
String[] is2 = {"print receipt"};
//Window 2
JFrame frame2 = new JFrame("shopping cart");
frame2.setVisible(true);
frame2.setSize(300, 1080);
frame2.setResizable(false);
frame2.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
Container con1 = frame2.getContentPane();
con1.setLayout(null);
//Date and Time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String uhrzeit = sdf.format(new Date());
Date date = java.util.Calendar.getInstance().getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy");
String dateString = dateFormatter.format(date);
//Feedback
JFrame frame3 = new JFrame("checkout");
for (int i = 0; i < is2.length; i++) {
JMenuItem mi3 = new JMenuItem(is2);
i2.add(mi3);
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "receipt is getting printed!");
frame3.setVisible(true);
}
});
}
for (int i = 0; i < is1.length; i++) {
JMenuItem mi1 = new JMenuItem(is1);
i1.add(mi1);
mi1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "you added " + mi1.getText());
JTextArea text = new JTextArea();
text.setBounds(0, 100, 150, 675);
frame2.add(text);
//Wordcounter
Scanner fileInput = new Scanner(mi1.getText());
ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> count = new ArrayList<Integer>();
while(fileInput.hasNext()) {
String nextWord = fileInput.next();
if(words.contains(nextWord)) {
int index = words.indexOf(nextWord);
count.set(index, count.get(index) + 1);
} else {
words.add(nextWord);
count.add(1);
}
}
fileInput.close();;
for(int i = 0; i < words.size(); i++) {
text.setLineWrap(true);
text.append(" - " + words.get(i) + " (" + count.get(i) + " time(s))" + "\n" + " [ IDN: "+ identNum + " ]");
int rechnung1 = count.get(i);
String rechnung2 = words.get(i);
text.setEditable(true);
//Window 3
frame3.setVisible(false);
frame3.setSize(300, 1080);
frame3.setResizable(false);
frame3.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - frame3.getWidth();
int y = (int) rect.getMaxY() - frame3.getHeight();
frame3.setLocation(x, y);
Container con2 = frame3.getContentPane();
con2.setLayout(null);
JTextArea te1 = new JTextArea();
double summe = rechnung1 * pl1;
te1.setEditable(false);
te1.setText("| |" + "\n" +
"|------------------------------|" + "\n" +
"| My purchase |" + "\n" +
"| " + dateString + " " + uhrzeit + " |" + "\n" +
"|------------------------------|" + "\n" +
"| EUR |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"| " + rechnung2 + " (" +rechnung1 + "x)" + " " + rechnung1 * pl1 + " |" + "\n" +
"| [IDN: "+ identNum + "]" + " |" + "\n" +
"| |" + "\n" +
"|------------------------------|" + "\n" +
"| Summe EUR |" + "\n" +
"| " + summe + " |" + "\n" +
"|------------------------------|" + "\n" +
"| |");
te1.setLineWrap(true);
te1.setBounds(0, 100, 150, 675);
te1.setEditable(false);
frame3.add(te1);
}
}
});
}
}
}
|
|