-
button event how to go through an array?
Still kinda new to java could use some help on this simple problem.
I just need to see an example of a button event that will display the elements of an array one at a time per
so far all I can do is display all of the elements at the same time this is what I have so far:
public void paint(Graphics g)
{
String nday[] = {"Monday", "Tuesday", "Wednesday", "Thrusday",
"Friday", "Saturday", "Sunday"};
for (int x = 0; x < 7; x++) {
g.setColor(Color.blue);
g.drawString(nday[x],170,55);
}
}
/* When the button is clicked this method will get automatically called. but I need it to go to next element each time the Okbutton is click. I have try continue and break statements but I am sure I dont know how to use them properly.*/
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == OkButton){
repaint();
}
}
-
Don't use break or continue statements, they're not needed. What you need is a flag variable to hold information about which day to store. An int will do nicely, as this will easily represent the positions in the array. This variable will need to be a class variable so it exists even after the repaint method has finished. Each time the button is clicked the repaint method will write the string from the position of the array that the variable is currently set to. You don't need to loop, because the loop will print all the strings every time. Assume that the variable is called dayToPrint. It should initially be set to -1.
[code]public void paint(Graphics g)
{
g.drawString(nday[dayToPrint],170,55);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource == OKButton)
{
if(dayToPrint == 6) //if reached final day
{
dayToPrint = 0; //reset to first day
}
else
{
dayToPrint++ //set flag to next day
}
repaint();
}
}
-
you could also just set a global int variable called something like, "arrayElement".
set it to 0
then your paint method like this:
Code:
public void paint(Graphics g)
{
String nday[] = {"Monday", "Tuesday", "Wednesday", "Thrusday",
"Friday", "Saturday", "Sunday"};
g.setColor(Color.blue);
g.drawString(nday[arrayElement],170,55);
}
then when the user clicks the button do this:
Code:
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == OkButton){
repaint();
arrayElement++;
}
}
A kram a day keeps the doctor......guessing
-
Reply to post 'button event how to go through an array?'
Thank You all,
These both work very well and are just what I needed to learn from. Java seems to be a little hard for me, but it is getting easier every day thanks to the help from people like you all.
The next question I will be asking is info on the work a rounds for database objects (storing) from an applet. I do know PHP very well. I think I will try to get the variables from the applet them use them in PHP for the ADO.
Until next time Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks