CSStudent CSStudent - 6 months ago 13
Java Question

I want to keep the values of Textarea from Textfield permanently (Java GUI)

I want to keep the values of Textarea1 after inputting them through the textfield1 via a button. Everytime I finish running it the value disappears for a new one. The first time I do it I get the value of the name I inserted, however when i do it a second time it erases. please help me, thanks. Here is the code:

import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;


public class GUI_project2 extends JFrame {

private JMenuBar menuBar;
private JButton button1;
private JTextArea textarea1;
private JTextField textfield1;

static String MYARRAY[] = new String[4];
static int COUNTER = 0;
static String NEWITEM = null;
//Constructor
public GUI_project2(){

this.setTitle("GUI_project2");
this.setSize(1118,845);
//menu generate method
generateMenu();
this.setJMenuBar(menuBar);

//pane with null layout
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(1118,845));
contentPane.setBackground(new Color(192,192,192));


button1 = new JButton();
button1.setBounds(206,109,90,35);
button1.setBackground(new Color(214,217,223));
button1.setForeground(new Color(0,0,0));
button1.setEnabled(true);
button1.setFont(new Font("sansserif",0,12));
button1.setText("Add Word");
button1.setVisible(true);
//Set methods for mouse events
//Call defined methods
button1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
ButtonClicked(evt);
}
});


textfield1 = new JTextField();
textfield1.setBounds(203,46,90,35);
textfield1.setBackground(new Color(255,255,255));
textfield1.setForeground(new Color(0,0,0));
textfield1.setEnabled(true);
textfield1.setFont(new Font("sansserif",0,12));
textfield1.setText("");
textfield1.setVisible(true);


textarea1 = new JTextArea();
textarea1.setBounds(27,48,150,100);
textarea1.setBackground(new Color(255,255,255));
textarea1.setForeground(new Color(0,0,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText("");
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);

//adding components to contentPane panel
contentPane.add(button1);
contentPane.add(textfield1);
contentPane.add(textarea1);
//adding panel to JFrame and seting of window position and close operation
this.add(contentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}

//Method mouseClicked for button1
private void ButtonClicked (MouseEvent evt) {
//TODO
NEWITEM = textfield1.getText();
if (NEWITEM.compareTo("end")!=0){
MYARRAY[COUNTER] = NEWITEM;
COUNTER++;
if (COUNTER == MYARRAY.length){
increaseArraySize();
}
}
String NEWITEM = "";
listArray();
}
public void listArray(){
for (int X=0;X<MYARRAY.length;X++){
textarea1.setText(textfield1.getText());
}

}

public static void increaseArraySize(){
System.out.print("Here we increase the size to ");
String TEMP[] = new String[MYARRAY.length*2];
System.arraycopy(MYARRAY, 0, TEMP, 0, MYARRAY.length);
MYARRAY = TEMP;
System.out.println(TEMP.length);

}
//method for generate menu
public void generateMenu(){
menuBar = new JMenuBar();

JMenu file = new JMenu("File");
JMenu tools = new JMenu("Tools");
JMenu help = new JMenu("Help");

JMenuItem open = new JMenuItem("Open ");
JMenuItem save = new JMenuItem("Save ");
JMenuItem exit = new JMenuItem("Exit ");
JMenuItem preferences = new JMenuItem("Preferences ");
JMenuItem about = new JMenuItem("About ");


file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
tools.add(preferences);
help.add(about);

menuBar.add(file);
menuBar.add(tools);
menuBar.add(help);
}



public static void main(String[] args){
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI_project2();
}
});
}


}

Answer

Hello CSStudent I tried to stay as close as your code as possible and edited your listArray method in order to achieve what you are Aiming for.

I want to keep the values of Textarea1 after inputting them through the textfield1 via a button. Everytime I finish running it the value disappears for a new one. The first time I do it I get the value of the name I inserted, however when i do it a second time it erases. please help me, thanks. Here is the code:

What your current code does is 'SETTING' the text of your textarea.

According to the Docs setText does this:

Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

So lets take a look at your listArray method.

public void listArray(){
    for (int X=0;X<MYARRAY.length;X++){
        textarea1.setText(textfield1.getText());
    }
}

It loops through your String Array which starts at the Size of 4 and increases by *2 if required. The problem is that since the size of the Array is 4 and you add the String on position 0 the array looks like this {"First thing you insert",null,null,null} according to the quote from the docs if the value is null or empty it will simply delete the old text. This is why it erases as you say.

However if you slighty modify it you will achieve what you desire

public void listArray(){
    textarea1.setText("");
    for (int X=0;X<MYARRAY.length;X++){
        if(MYARRAY[X] != null)
            textarea1.append(MYARRAY[X]+"\n");
    }
}

As stated earlier I tried to stay as close to your code as possible, let me explain what this does aswell. First of all it clears the textarea by inserting "" which is a blank String, followed by that it will loop through your Array and only IF it IS NOT (!=) null it will append the saved String and a linebreak ("\n"). (This check prevents the textarea from getting erased)

If this reply solved your Problem please make sure to mark my answer as accepted, click on the check mark beside the answer to toggle it from hollow to green