Search code examples
javaswingjbuttonjtextarea

Change JTextArea texts from JButton's actionPerformed which is in another class


I have a fully functional console-based database which I need to add GUIs to. I have created a tab page (currently only one tab) with a button "Display All Student" which when triggered will display a list of students inside a JTextArea which of course is in its own class and not inside the button's action listener class. Problem is, the JTextArea is not recognised inside button's action listener. If I add parameter into the action listener, more errors arise. Help?

I have searched Stack Overflow for similar problems but when I tried it in my code, doesn't really do the trick? Or maybe I just need a nudge in the head. Anyways.

Here is my code so far:

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

public class StudDatabase extends JFrame 
{
    private     JTabbedPane tabbedPane;
    private     JPanel      studentPanel;
    private static Scanner input = new Scanner(System.in);    
    static int studentCount = 0;
    static Student studentArray[] = new Student[500];

    public StudDatabase()
    {
        setTitle("Student Database");
        setSize(650, 500);
        setBackground(Color.gray);

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the tab pages
        createStudentPage();
        // more tabs later...

        // Create a tab pane
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab( "Student Admin", studentPanel );
        topPanel.add( tabbedPane, BorderLayout.CENTER );
    }

    public void createStudentPage()
    {
        studentPanel = new JPanel();
        studentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        JButton listButton = new JButton("List All Student(s)");
        listButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent event) 
            {
                if(studentCount > 0) 
                {
                    for(int i=0; i<studentCount; i++)
                    {
                        // print out the details into JTextArea
                        // ERROR! textDisplay not recognised!!!
                        textDisplay.append("Student " + i);
                    }
                    System.out.printf("\n");
                }
                else // no record? display warning to user
                {
                    System.out.printf("No data to display!\n\n");
                }            
          }
        });
        studentPanel.add(listButton);

        JTextArea textDisplay = new JTextArea(10,48);
        textDisplay.setEditable(true); // set textArea non-editable
        JScrollPane scroll = new JScrollPane(textDisplay);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        studentPanel.add(scroll);
    }

    public static void main(String[] args) 
    {    
        StudDatabase mainFrame = new StudDatabase();
        mainFrame.setVisible(true);
    }

Solution

  • Your code isn't working for the same reason this wouldn't work:

    int j = i+5;
    int i = 4;
    

    You have to declare variables before using them in Java.

    Secondly, in order to use a variable (local or instance) from inside an inner class - which is what your ActionListener is - you need to make it final.

    So, the below code will compile and run:

    final JTextArea textDisplay = new JTextArea(10,48);
     ...
    listButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent event) 
            {
            ...
            textDisplay.append("Student " + i);