简体   繁体   中英

JTextArea not updating in gui

I have a Java program that reads a text file of names and sorts them into groups. The number per group is entered via a JTextArea , the "submit" JButton is clicked, then the names are assigned randomly.

Finally the groups are placed in a JTextArea . Everything works fine up until the names are supposed to appear in the JTextArea . They don't show up. This is supposed to be done in the actionPerformed() method.

The only way I can get them to show up is by including a String literal in the JTextArea's append method. Then I have to click "submit" a 2nd time and the names do appear but the String literal (" Groups\\n ") appears twice, once before and once after.

Why do I need the literal and why doesn't the text appear immediately.

Here's my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class GUIForGroups extends JFrame implements ActionListener
{
    JLabel numOfG = new JLabel("How many per group?  ");
    JTextField num = new JTextField(3);
    JButton sub = new JButton("Make Groups!");
    JPanel top = new JPanel();

    JLabel botTitle = new JLabel("The Groups are:");
    private ArrayList<String> roster;
    private ArrayList<String> team;
    //     ArrayList<JLabel> numbers = new ArrayList<JLabel>();
    //     ArrayList<JTextField> teams = new ArrayList<JTextField>();
    JPanel bottom = new JPanel();
    JTextArea area = new JTextArea(400,20);
    JScrollPane scrollPane = new JScrollPane(area); 
    ArrayList<String> groups;
    int n;
    public GUIForGroups()
    {
        setSize(800,400);
        setTitle("Group Picker!");
        Container c = getContentPane();
        c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS ) );
        top.add(numOfG);
        top.add(num);
        top.add(sub);
        sub.addActionListener(this);
        c.add(top);
        c.add(bottom);
        try {
            roster = getRoster();
        }
        catch (IOException ioe) {
            JOptionPane.showMessageDialog(null,"Error reading in data, exiting");
            System.exit(0);
        }
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent evt) 
    {
        String userIn ;
        userIn    = num.getText()  ;
        n  = Integer.parseInt( userIn ) ;
        team = sortInGroups(n);
        String s = setPrint();
        //         /bottom.add(botTitle);

        area.append("Groups\n"+setPrint());  // PROBLEM HERE, NOT WORKING
       //          System.out.print(s);  // was for testing 
        bottom.add(area);
        add(bottom);

        repaint();
    }

    public void paintComponent(Graphics g)
    {
        area.append("The groups are: \n"+setPrint());
        add(bottom);
    }

    public ArrayList<String> getRoster() throws IOException
    {
        roster = new ArrayList<String>();
        Scanner fr = new Scanner(new File("csroster.txt"));
        while (fr.hasNext())
        {
            String name = fr.next();
            roster.add(name);
        }
        return roster;
    }

    public ArrayList sortInGroups(int n)
    {
        int i = 0, j=0;
        String next="";
        ArrayList<String> groups = new ArrayList<String>();
        while (roster.size() > 0 )
        {
            while (i<n && roster.size()>0)
            {
                int num = (int)(Math.random()*roster.size());
                next +=  roster.remove(num) + "   ";
                i++;
            }
            i=0; 
            groups.add(next);
            next = "";
        }
        return groups;
    }

    public String setPrint()
    {
        int teamNum = 1;
        String groups="";
        for (String t: team)
        {
            groups += "Team # "+teamNum+++" is: "+t+"\n";

        }
        return groups;
    }


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

You must call revalidate() instead of repaint() .

public void actionPerformed(ActionEvent evt) 
{
    ...

    revalidate();
}

or don't add the area to bottom in the actionPerformed method - do it in the constructor. Then no need for revalidate or repaint

public GUIForGroups()
{
    ...
    c.add(bottom);
    bottom.add(area);  // missing a JScrollPane here
    try {
        ...
}


public void actionPerformed(ActionEvent evt) 
{
    String userIn ;
    userIn    = num.getText()  ;
    n  = Integer.parseInt( userIn ) ;
    team = sortInGroups(n);
    String s = setPrint();

    area.append("Groups\n"+setPrint());
}

Replace repaint(); with revalidate() in your action performed. Also you can remove paintComponent method.

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