This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How do I cycle through an array of strings and display them one at a time when JButton is clicked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I cycle through an array of strings and display them one at a time when JButton is clicked" Watch "How do I cycle through an array of strings and display them one at a time when JButton is clicked" New topic
Author

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

Todd Beggs
Greenhorn

Joined: May 15, 2012
Posts: 1
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.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
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();
answerB.addActionListener(abHandler);

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


//add components to pane
pane.add(questionLabel);
pane.add(questionTextField);
pane.add(keyLabel);
pane.add(keyTextField);
pane.add(answerLabel);
pane.add(answerTextField);
pane.add(answerB);
pane.add(clearB);




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

{
@Override
public void actionPerformed( ActionEvent ex)
{

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

if(inFile.exists())
{

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

//create valve

BufferedReader aBufferedReader = new BufferedReader( aFileReader );
String[]myArray;
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];



questionTextField.setText(answer0);

}

}
catch(Exception ed)
{
String errorMessage = "\nFile not found.";
System.out.println(errorMessage);
}


}
}
}

private class ClearButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
questionTextField.setText("");
keyTextField.setText("");
answerTextField.setText("");

}
}


public static void main(String[] args)
{

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

}
}


Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201




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.


~Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I cycle through an array of strings and display them one at a time when JButton is clicked
 
Similar Threads
Please rescue me out
Frustrated at GridBagLayout
make the last button align horizontally with other components
Null pointer exception in button event handler.
Display Frame on Center of Screen