1

I have a Jframe that the user enters new information through a Joptionpane, it is added to an array which is then appended and displayed to a contentpane.. the cycle then repeats till the user enters "STOP". Currently the program is outputting the new array under the old one. How would I clear away the old array in the content pane and only display the new values?

import java.awt.*;
import java.util.LinkedList;
import java.util.List;
public class Project1GUI {
    static JFrame Frame;
    static TextArea unsorted_words, sorted_words, linked_words;
    public Project1GUI(String title){
            //All this does is make an empty GUI FRAME. 
                    Frame=new JFrame();//i made a new variable from the JFrame class
                    Frame.setSize(400,400);//Used the Variable from JFrame and used some of it functions. This function sets the hieght and width of the Frame
                    Frame.setLocation(200,200);//This sets where the Empty Frame should be
                    Frame.setTitle(title);//This puts a title up top of the Frame
                    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//places an x box that closes when clicked on
                    Frame.setVisible(true);//This activates the JFram when is set true.
                    Frame.setLayout(new GridLayout(1,2));//This sets the layout of the Frame and since i want a Grid i used a GirdLayout
                    //Functions and placed it inside the setlayout functions. to  get 2 grids i places 1 meaning 1 row and 2 for 2 cols
                    unsorted_words=new TextArea(); //From the TextArea class i made three variables
                    sorted_words= new TextArea();
                    linked_words= new TextArea();
                    Container panel=new Container();
                    panel=Frame.getContentPane();
                    panel.add(unsorted_words);
                    panel.add(sorted_words);
                    panel.add(linked_words);

    }


    public void add_unsorted(String words){
        unsorted_words.append(words+"\n");//add words to GUI

    }

    public void add_sorted(String words){
        sorted_words.append(words+"\n");
    }

    public void add_linked(List<String> linked_words2){
        linked_words.append(linked_words2+"\n");
    }

}
JpersaudCodezit
  • 143
  • 2
  • 13

1 Answers1

1

For a more definitive answer, post an MCVE

Seeing as you haven't posted any code, I'm guessing you are using a JLabel or a JList or something of that sort to display the array. No matter which one you are doing, you need to tell the component to update it's content, it doesn't just do it itself. To do that, you need to call the components .setText() or similar method.

If you have a JLabel or JTextArea it could look like this.

labelOrTextArea.setText("New Text");

If you are using a JList you should update the lists Default List Model like this

dlm.addElement("New Text");

UPDATE

I see a couple things wrong with your code. First off JFrame Frame = new JFrame conventionally, variables should start with a lower case letter and they should not contain underscores '_'. You are also using AWT Components instead of Swing components. You should be using the likes of JTextArea, JPanel (Theres no JContainer), JLabel etc.

You are also never adding the panel to the frame.

frame.add(panel);

You should also not be adding stuff to the frame or panels after you set its visibility to true. So you should setup your frame like this

import javax.swing.*;
import java.awt.*;
import java.util.List;
public class Project1GUI
{
    JTextArea unsorted_words, sorted_words, linked_words;

    public Project1GUI()
    {
        JFrame frame = new JFrame("Title");
        JPanel panel = new JPanel(new GridLayout(2, 1));

        unsorted_words = new JTextArea();
        sorted_words = new JTextArea();
        linked_words = new JTextArea();

        panel.add(unsorted_words);
        panel.add(sorted_words);
        panel.add(linked_words);

        frame.add(panel);
        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

You can then implement the methods you currently have and call them in an ActionListener or such.

Result:

Three TextAreas

On top of all of that, you should not rely on the use of static as it takes away from the main points of OOP.

Community
  • 1
  • 1
Jonah
  • 1,013
  • 15
  • 25
  • WOW thanks a bunch Great insight and advice.. Im new to learning Java so sorry about the obvious confusions but again thanks ! – JpersaudCodezit Mar 24 '16 at 15:53
  • One question though, If I were to implement the code as you've described, would the JFrame refresh if automatically if the user enters a new string to store in the array? – JpersaudCodezit Mar 24 '16 at 16:06
  • What exactly do you mean? Are you trying to get the contents from the textareas and store that in an array ? – Jonah Mar 24 '16 at 16:09
  • The main program will read in a .txt file and store those strings in an array. Which is then sorted alphabetically and creates a seperate linked list of all the sorted words. The two arrays and linked list are now passed to my GUI and are appended. Back in the main program the user is asked through a window if they would like to add words.If they do the word is added to the arrays and linked list and again it is passed to the GUI and displayed. My question is how do i get rid of the old arrays and linked list so that the GUI only displays the new arrays and list WITH the word the user input? – JpersaudCodezit Mar 24 '16 at 19:56
  • I would suggest maybe using `Collections` and having one store the one you read the file into then one with the sorted values and then the one you want to append to. You can then pass those `Collections` to `LinkedLists` and display them using `textArea.setText()` then when you get the input, you can use the `.append()` method to add the input of the user. – Jonah Mar 25 '16 at 04:59
  • Jesus lol had no idea it was gunna be this complicated lol. Ill keep tweaking with it. Thank you for taking time to give me advice though, much appreciated! – JpersaudCodezit Mar 26 '16 at 21:10
  • No problem! If you would like, I could whip up an example and add it as an edit to my answer. – Jonah Mar 26 '16 at 21:12