..Hi to all...just want to ask..

..I have already created an ideal layout for my calculator..my problem is...I dont know how to insert a value into a field everytime I press a button(0 to 9)..without deleting the previous integer after I press my operator..

..how shall I do so..??..

I've never used Swing/AWT but I suppose the answer to this question is pretty generic; you basically "append" the new value to the existing value rather than setting the new value as it is. So if "txtBox" is your text field, after a key is pressed, you might want to do txtBox.value += keyValue as opposed to txtBox.value = keyValue .

..whow...thanks....it works...

...uhm....there is still something I want to ask...Here is the layout for my calculator..


...it's pretty simple as it looks..whenever I turn ON buttons...buttons turned enabled..then vice versa for Off button..what makes me troubled about this is that Im setting it enabled(false) and (true) EVERY BUTTON DECLARATION..making the source codes too long....well..my question is can I possibly declare 19 buttons with enable/disable functions in a less/shorter statement..??..

Attachments Untitled.png 24.87 KB

Yes mate you can, but it would require some tweaking to your code. I'll add an example. Say you create an array of integers and you wanted to print all the values of the array how would you do it? Use a for loop. The logic is the same to JButtons; create an array of JButtons and use a for loop.

My example is simple and doesn't follow the same layout as what your calculator does:

package Beginner_Programs;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonEnabler extends JFrame 
{
	JButton button;
	JButton [] myArray = new JButton[4];
	
	/**
	 * Simply creating an array of buttons won't actually create the individual buttons therefore
	 * you have to create each individual button.
	 */
	
	public ButtonEnabler()
	{
		setLayout( new FlowLayout() );
		
		for( int i = 0; i <= 3; i++ )
		{
			myArray[i] = new JButton( "Button "+i );
			add( myArray[i] );
		}
		
		button = new JButton( "Turn Off" );
		button.addActionListener( new ActionListener () 
		{
			public void actionPerformed( ActionEvent e )
			{
				for( int i = 0; i <= 3; i++ )
				{
					myArray[i].setEnabled( false );
				}
			}
		});
		add( button );
		
		button = new JButton( "Turn On" );
		button.addActionListener( new ActionListener () 
		{
			public void actionPerformed( ActionEvent e )
			{
				for( int i = 0; i <= 3; i++ )
				{
					myArray[i].setEnabled( true );
				}
			}
		});
		add( button );
		
		setSize( 200,200 );
		setVisible( true );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
			
	}
	
	public static void main(String [] args)
	{
		new ButtonEnabler();
	}
}

In my above example I also uses the for loop to create a 4 buttons and added them individually to the JFrame. I also used the same logic to disable each button. As for tweaking your code you will need to change how you created your buttons.

Does this help you at all? Questions about my code?

Are you using some sort of collection to store the related buttons which you need to enable/disable? If not, use a List or an array and enabling/disabling would just be a matter of looping over the buttons and setting the appropriate flag.

EDIT: Slooow :-)

Edited 5 Years Ago by ~s.o.s~: n/a

..oh.!!!.I get it..thanks...now I'm done..so nice to know people like you guys here on earth...thank you...

This question has already been answered. Start a new discussion instead.