简体   繁体   中英

Updating JTextArea with new data read in from file

I have a search box that a user can type in a state and it will read in the data from a text file about that states election results. However my JTextArea doesn't show the new data. I debugged and know for certain the data is being read in correctly. I've read lots of problems similiar to mine but have found no solution that worked for my particular problem. Can anyone offer any suggestions as to how I should go about this. Here is my code.

    package view;

import data.VoteIO;
import business.State;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

//illustrate listening for a selection of the JList
public class Voting2000 extends JFrame implements ActionListener{

    private ResultsView votePanel;
    private Container pane;
    private JTextField search;
    private JButton goSearch;
    private JLabel instructions;

    public Voting2000() throws IOException{
        votePanel = new ResultsView(new State("Nebraska", "NE")); 
        search = new JTextField();
        goSearch = new JButton("Search");
        instructions = new JLabel("To search for a states input must be in following format State, State's abbreviate for example Nebraska, NE ");
        pane = getContentPane();
        goSearch.addActionListener(this);
        pane.setLayout(new BorderLayout());
        pane.add(BorderLayout.NORTH,instructions);
        pane.add(BorderLayout.CENTER, votePanel);
        pane.add(BorderLayout.SOUTH,search);
        pane.add(BorderLayout.EAST,goSearch);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) throws IOException{
            Voting2000 listing = new Voting2000();
    }

    public void actionPerformed(ActionEvent e)
    {
        String state = search.getText().toLowerCase();
        String[] fields = state.split(",");
        try {
            State aState = new State(fields[0].trim(),fields[1].trim());;
            votePanel = new ResultsView(aState);
            pane.add(BorderLayout.CENTER,votePanel);
            pane.revalidate();
            pane.repaint();
        } catch (IOException ex) {
            Logger.getLogger(Voting2000.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Here is ResultsView class where the JTextArea is created

package view;

import javax.swing.*;
import java.util.List;
import business.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author rmildenb
 */
public class ResultsView extends JPanel{
    private JTextArea results;
    private Stats stat;


    public ResultsView(){

        createView();
    }

    public ResultsView(Stats state) {
        this.stat = state;
        createView();
    }

    public void createView(){
        results = new JTextArea(5, 35);
        JScrollPane pane = new JScrollPane(results);
        pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(pane);
        showInformation();
    }

    public void showInformation(){
        results.setText("");
        results.setText(stat.getDescription());
        results.setCaretPosition(0);

    }



}

I've tried to remove the votePanel from the Container pane the repaint it which works but when I try to add the new one I just created and repaint the pane nothing appears.

votePanel = new ResultsView(aState);

Creating a new Component doesn't add the component to the GUI. The Component is just sitting in memory.

Instead of creating a new ResultsView panel, you should have a method that simply refreshes the text area with new text. Then the text area will repaint itself automatically.

The other option is far more complex. The code is something like:

panel.remove(exisiting ResultsView panel);
panel.add( new ResultsView panel );
panel.revalidate();
panel.repaint();

The revalidate() is the key as this invokes the layout manager so all components can be sized and positioned properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM