简体   繁体   English

使用从文件读入的新数据更新JTextArea

[英]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. 但是我的JTextArea不显示新数据。 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 这是创建JTextArea的ResultsView类

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重绘它有效,但是当我尝试添加刚刚创建的新窗格并重新绘制窗格时,没有任何内容出现。

votePanel = new ResultsView(aState);

Creating a new Component doesn't add the component to the GUI. 创建新组件不会将组件添加到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. 您应该拥有一个只使用新文本刷新文本区域的方法,而不是创建新的ResultsView面板。 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. revalidate()是键,因为它调用布局管理器,因此可以正确调整所有组件的大小和位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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