School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,150 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,872 people online right now. Registration is fast and FREE... Join Now!



Referring Different Buttons!

Referring Different Buttons! Rate Topic: -----

#1 rishabhsharma  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributor w/DIC++
  • Posts: 202
  • Joined: 26-March 09


Dream Kudos: 500

Post icon  Posted 26 April 2009 - 08:27 AM

OK, I have this code. This code create a square button grid of 4X4 of numbers from 1-15(16 is not created), well it doesn't matter here.
OK, I wish to know, how to refer different buttons so that I could make them disabled and enabled simultaneously, without any user interference, like all the buttons are flashing( 'cos they are enabled and disabled simultaneously) . I could not make out how to do it. I know, I have one part of the code, 'cos I don't know how to create the second part of the code. Could you all please help me.......

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Buttonss extends JFrame 
	{
		Buttonss()
			{
				this.setSize(500,500);
				this.setTitle("Buttons");
				this.setVisible(true);
				this.setLayout(new GridLayout(4,4));
				int n = 4;
				Dimension d = new Dimension(20,20);
				int count;
				for(int i=0;i<4;i++)
					{
						for(int j=0;j<4;j++)
							{   
								count = i*n+j;
								if(count>0) 
									{
										button = new JButton(new Integer(count).toString());
										button.setPreferredSize(d);
										add(button); 
									}
							}
					}
					
			 }
			 JButton button;
	 }


This post has been edited by rishabhsharma: 26 April 2009 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 26 April 2009 - 09:26 AM

You are going to have to move your code around a little because of the fact that you create the buttons dynamically, add them to the form and don't keep a reference to them for later use. So either you will have to do something like create an array of buttons to store references to each button in or access each button through the JFrame which can get tricky.

But below I have made some modifications to your program to show you how essentially this will work with a Timer object. Timer is part of the swing library and is requires nothing extra on your part other than to include event listening.

We create a timer, tell it how often to fire and where it can find an event handler to handle the timer firing event. So we have added an action listener to the class which implements the actionPerformed method. Now each time the timer event fires, it will look to the Buttonss class to handle the event. In turn that class will point the event to the actionPerformed method where we then enable and disable a button once each second.

Since you do end the loop having a reference to number 15 button, I could show you how we can use your button reference to then toggle the button to create a flashing setup.

import java.awt.event.*;
import java.awt.*;
import java.awt.event.*; // Used for actionListening
import javax.swing.*;

public class Buttonss extends JFrame implements ActionListener
{
	// Create a timer class (this is part of swing)
	Timer t;
	
	Buttonss()
	{
			// Set the timer up to call this class every 1000 milliseconds
			t = new Timer(1000,this);
			this.setSize(500,500);
			this.setTitle("Buttons");
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setLayout(new GridLayout(4,4));
			int n = 4;
			Dimension d = new Dimension(20,20);
			int count;
			for(int i=0;i<4;i++)
				{
					for(int j=0;j<4;j++)
						{  
							count = i*n+j;
							if(count>0)
								{
									button = new JButton(new Integer(count).toString());
									button.setPreferredSize(d);
									add(button);
								}
						}
				}
			this.setVisible(true);
			
			// Start the timer after the form is fully loaded
			t.start();
			   
	}
	JButton button;
	
	public static void main(String args[]) {
		Buttonss b = new Buttonss();
	}
	
	// This event handles the timer's ticking, it is part of the Action listener
	public void actionPerformed(ActionEvent e) {
		// If the source of this event firing was the timer, go to the last button setup and toggle enabled/disabled
		// If the button is enabled, disable it. Next time event fires, it will then re-enable it.
		if (e.getSource() == t) {
				 if (button.isEnabled()) { button.setEnabled(false); }
				 else { button.setEnabled(true); }
		}
	}

}




If you follow some of the in code comments you will see what I am doing here. It is not too complicated as long as you understand that the timer object is triggering the current class' actionPerformed event.

Hope this helps you out. Enjoy!

"At DIC we be timer class mastering code ninjas... there are 12 numbers on the clock because the ancients thought 12 was the perfect number, that was until they called 1-800-CALL-DIC and discovered that was the perfect number" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 rishabhsharma  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributor w/DIC++
  • Posts: 202
  • Joined: 26-March 09


Dream Kudos: 500

Posted 28 April 2009 - 12:41 AM

View PostMartyr2, on 26 Apr, 2009 - 09:26 AM, said:

You are going to have to move your code around a little because of the fact that you create the buttons dynamically, add them to the form and don't keep a reference to them for later use. So either you will have to do something like create an array of buttons to store references to each button in or access each button through the JFrame which can get tricky.

But below I have made some modifications to your program to show you how essentially this will work with a Timer object. Timer is part of the swing library and is requires nothing extra on your part other than to include event listening.

We create a timer, tell it how often to fire and where it can find an event handler to handle the timer firing event. So we have added an action listener to the class which implements the actionPerformed method. Now each time the timer event fires, it will look to the Buttonss class to handle the event. In turn that class will point the event to the actionPerformed method where we then enable and disable a button once each second.

Since you do end the loop having a reference to number 15 button, I could show you how we can use your button reference to then toggle the button to create a flashing setup.

import java.awt.event.*;
import java.awt.*;
import java.awt.event.*; // Used for actionListening
import javax.swing.*;

public class Buttonss extends JFrame implements ActionListener
{
	// Create a timer class (this is part of swing)
	Timer t;
	
	Buttonss()
	{
			// Set the timer up to call this class every 1000 milliseconds
			t = new Timer(1000,this);
			this.setSize(500,500);
			this.setTitle("Buttons");
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setLayout(new GridLayout(4,4));
			int n = 4;
			Dimension d = new Dimension(20,20);
			int count;
			for(int i=0;i<4;i++)
				{
					for(int j=0;j<4;j++)
						{  
							count = i*n+j;
							if(count>0)
								{
									button = new JButton(new Integer(count).toString());
									button.setPreferredSize(d);
									add(button);
								}
						}
				}
			this.setVisible(true);
			
			// Start the timer after the form is fully loaded
			t.start();
			   
	}
	JButton button;
	
	public static void main(String args[]) {
		Buttonss b = new Buttonss();
	}
	
	// This event handles the timer's ticking, it is part of the Action listener
	public void actionPerformed(ActionEvent e) {
		// If the source of this event firing was the timer, go to the last button setup and toggle enabled/disabled
		// If the button is enabled, disable it. Next time event fires, it will then re-enable it.
		if (e.getSource() == t) {
				 if (button.isEnabled()) { button.setEnabled(false); }
				 else { button.setEnabled(true); }
		}
	}

}




If you follow some of the in code comments you will see what I am doing here. It is not too complicated as long as you understand that the timer object is triggering the current class' actionPerformed event.

Hope this helps you out. Enjoy!

"At DIC we be timer class mastering code ninjas... there are 12 numbers on the clock because the ancients thought 12 was the perfect number, that was until they called 1-800-CALL-DIC and discovered that was the perfect number" :snap:



Thanks Friend, It's working but this program enables and disables last button solely and no reflection on other buttons. I want that each button enables and disables individually. I think we will have to create timer for each thread, isn't it? So please renovate your code.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month