Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I cycle through an array of strings and display them one at a time when JButton is clicked  RSS feed

Todd Beggs
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, I am a 42 year old computer science major but only two years in so I still have much to learn. I have pulled some strings from a text file and loaded them into an array. What is the best way to have each string displayed every time the next question button is pushed? I am displaying it into a Text Field box, is this what would normally be used or is there a box better suited for this purpose? I was planning on doing basically the same thing in the program with a file full of jpg's, but was hoping to make them display in the same window each time a new question was brought up. Any thoughts, suggestions, or flat out help would be appreciated, Thanks.

(here is what I have so far)

package keys;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Keys extends JFrame {

//static variables to hold frame dimensions
private static final int WIDTH = 500;
private static final int HEIGHT = 200;

//declare component objects
private JLabel questionLabel,keyLabel,answerLabel;
private JTextField questionTextField,keyTextField,answerTextField;
private JButton answerB;
private JButton clearB;

//declare reference variable for each handler
private AnswerButtonHandler abHandler;
private ClearButtonHandler cbHandler;

public Keys() //constructor that defines frame
setTitle(" RADRIFFS Key Signature Practice");//set the title of the frame
setSize( WIDTH, HEIGHT);

Container pane = getContentPane();//get content frame (jframe method)

GridLayout aGrid = new GridLayout( 4, 2);//create a 3-row 2 column layout

pane.setLayout(aGrid); //set layout for pane

//instantiate JLabel objects

questionLabel = new JLabel("Question");
keyLabel = new JLabel("Your Answer");
answerLabel = new JLabel("Result");

//instantiate JTExtField objects

questionTextField = new JTextField(30);
keyTextField = new JTextField(30);
answerTextField = new JTextField(30);

//instantiate buttons and register handlers
answerB = new JButton("Next Question");
abHandler = new AnswerButtonHandler();

// instantiate buttons and register handlers
clearB = new JButton("Clear");
cbHandler = new ClearButtonHandler();

//add components to pane

centerFrame(WIDTH, HEIGHT);//call method to center frame on screen
}//end constructor

public void centerFrame(int frameWidth, int frameHeight)
//create a Toolkit object
Toolkit aToolkit = Toolkit.getDefaultToolkit();

//create Dimension object with user screen information
Dimension screen = aToolkit.getScreenSize();

//assign x, y, postion of upper-left corner of frame
int xUpperLeftCorner = (screen.width - frameWidth) /2;
int yUpperLeftCorner = (screen.height-frameHeight) /2;

//method to position frame on user's screen
setBounds( xUpperLeftCorner, yUpperLeftCorner, frameWidth, frameHeight);

}//end method

private class AnswerButtonHandler extends JFrame implements ActionListener

public void actionPerformed( ActionEvent ex)

File inFile = new File( "questions/questions.txt");//locate data source


FileReader aFileReader = new FileReader( inFile); //create pipeline

//create valve

BufferedReader aBufferedReader = new BufferedReader( aFileReader );
myArray = new String[30];

for(int x = 0; x < myArray.length;x++){
String answer0,answer1,answer2;

myArray[x] = aBufferedReader.readLine();
answer0 = myArray[0];
answer1 = myArray[1];
answer2 = myArray[2];



catch(Exception ed)
String errorMessage = "\nFile not found.";


private class ClearButtonHandler implements ActionListener
public void actionPerformed (ActionEvent e)


public static void main(String[] args)

JFrame aKeys = new Keys();
aKeys.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);


Bill Johnston
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi there,

I added code tags (to add them in the future either manually type in [ c o d e ] and [ / c o d e ] - but w/o the spaces, around you code, or clidk the "code" button over your reply box and put your code inside them).

Now as to your question, since your Strings are questions, I would imagine you'll have a spot in the GUI for the answers. Therefore it might be more suitable to have the questions as JLabels and the answers in an editable JTextField.

It would also be better to put your String Questions in an ArrayList.

As to cycling through them, whichever Collection you use, your would just loop through it and replace the text on the Component as you do so.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!