繁体   English   中英

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

[英]Updating JTextArea with new data read in from file

我有一个用户可以键入状态的搜索框,它将从文本文件中读取有关该状态选举结果的数据。 但是我的JTextArea不显示新数据。 我调试并确定数据正在正确读取。 我已经阅读了许多与我类似的问题,但没有找到适用于我的特定问题的解决方案。 任何人都可以提出任何关于我应该怎么做的建议。 这是我的代码。

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

}

这是创建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);

    }



}

我试图从容器窗格中删除votePanel重绘它有效,但是当我尝试添加刚刚创建的新窗格并重新绘制窗格时,没有任何内容出现。

votePanel = new ResultsView(aState);

创建新组件不会将组件添加到GUI。 组件只是坐在内存中。

您应该拥有一个只使用新文本刷新文本区域的方法,而不是创建新的ResultsView面板。 然后文本区域将自动重新绘制。

另一种选择要复杂得多。 代码类似于:

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

revalidate()是键,因为它调用布局管理器,因此可以正确调整所有组件的大小和位置。

暂无
暂无

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

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