javaswinguser-interfacejframecontentpane

How to reset or refresh Jframe with new values


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");
    }

}

Solution

  • 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.