Search code examples
javastringjtextarea

How can I cycle through and display elements from a split string in java?


I have to do a project for college that reads in data from an external text file and has a next/previous/exit button to cycle through the various blocks of text. This is my first encounter with gui's and I'm finding displaying items quite difficult. In various iterations I have made the correct blocks of text appear but cannot cycle through them. Now they are not even displaying. I have plenty of time, so if I'm just going the completely wrong way about this, a complete rebuild isn't out of the question and any suggestions are welcome. It compiles fine, but nothing displays in the JTextArea.

Here is my code:

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

public class MainFrame extends JFrame
{
//Main panels declaration
private JPanel main1;
private JPanel main2;
private JPanel main3;

//Label Declaration
private JLabel topBar;

//Text area declaration
private JTextArea mainText;
int page = 0;

//Buttons declaration
private JButton prev;
private JButton next;
private JButton exit;

String output = "";
String [] hero = output.split("@");


//Constructor
public MainFrame() throws IOException
{   //Declare Layouts
    GridLayout grid = new GridLayout(2,1);
    BorderLayout bor = new BorderLayout(50, 10);
    FlowLayout flo = new FlowLayout();

    //Set frame layout
    setLayout(flo);
    setSize(800, 600);
    setVisible(true);

    //Initialise main panel 1
    main1 = new JPanel();
    main1.setLayout(grid);
    add(main1);
    main1.setBorder(new EtchedBorder());


    //Initialise main panel 2
    main2 = new JPanel();
    main2.setLayout(bor);
    main1.add(main2);


    //Text area
    mainText = new JTextArea(20,50);
    JScrollPane JSP = new JScrollPane(mainText);
    main2.add(bor.CENTER,mainText);
    mainText.setBorder(new EtchedBorder());
    mainText.setEditable(false);
    mainText.setText(hero[page]);

    //File Handling
    File inputFile = new File ("ProjectInputFile.txt");
    Scanner scanner = new Scanner(inputFile);


    while(scanner.hasNextLine())
    {
        output = output + (scanner.nextLine() + "\n");
    }

    //Top Bar
    topBar = new JLabel("Hero Viewer", SwingConstants.CENTER);
    main2.add(topBar, bor.PAGE_START);

    //Initialise main panel 3
    main3 = new JPanel();
    main3.setLayout(flo);
    main1.add(main3);

    //Prev Button
    prev = new JButton("Prev");
    main3.add(prev);
    ActionListener PrevList = new PrevButton(); //Call Button Listener
    prev.addActionListener(PrevList);

    //Exit Button
    exit = new JButton("Exit!");
    main3.add(exit);
    ActionListener ExitList = new ExitButton(); //Call Button Listener
    exit.addActionListener(ExitList);

    //Next Button
    next = new JButton("Next");
    main3.add(next);
    ActionListener NextList = new NextButton(); //Call Button Listener
    next.addActionListener(NextList);
}

//Prev button event listener
public class PrevButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page = page --;
        mainText.setText(hero[page]);

    }
}

//Exit button event listener
public class ExitButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        setVisible(false);
        dispose();
        System.exit(0);
    }
}

//Next button event listener
public class NextButton implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        page++;
        mainText.setText(hero[page]);
    }
}

//Main Method
public static void main (String [] args) throws IOException
{
    MainFrame MF = new MainFrame();
}

}

Solution

  • After

    while(scanner.hasNextLine())
    {
        output = output + (scanner.nextLine() + "\n");
    }
    

    Try adding

    hero = output.split("@");
    

    I doubt if your hero array is empty.