Jump to content

[SOLVED]How to get specific value for a JButton in an array of JButtons?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mastrgamr

mastrgamr

    Newbie

  • Members
  • PipPip
  • 10 posts
I'm trying to get a specific value for a JButton in an array of JButtons, Here is part of my code:


[COLOR=#880000]//create and setup the board panel[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#660066]JPanel[/COLOR][COLOR=#000000] board [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]JPanel[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
                board[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]setLayout[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]GridLayout[/COLOR][COLOR=#666600]([/COLOR][COLOR=#006666]3[/COLOR][COLOR=#666600],[/COLOR][COLOR=#006666]3[/COLOR][COLOR=#666600]));[/COLOR][COLOR=#000000]
                
                [/COLOR][COLOR=#000088]for[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]int[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#666600]<[/COLOR][COLOR=#000000] spot[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]length[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000] i[/COLOR][COLOR=#666600]++){[/COLOR][COLOR=#880000]//spot array has 9 elements[/COLOR][COLOR=#000000]
                        spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#666600]][/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]JButton[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#880000]//initialize each spot to a new JButton[/COLOR][COLOR=#000000]
                        spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#666600]].[/COLOR][COLOR=#000000]setText[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"-"[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000] 
                        spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#666600]].[/COLOR][COLOR=#000000]setFont[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]new[/COLOR][COLOR=#660066]Font[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]this[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]getFont[/COLOR][COLOR=#666600]().[/COLOR][COLOR=#000000]getFontName[/COLOR][COLOR=#666600](),[/COLOR][COLOR=#000088]this[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]getFont[/COLOR][COLOR=#666600]().[/COLOR][COLOR=#000000]getStyle[/COLOR][COLOR=#666600](),[/COLOR][COLOR=#006666]30[/COLOR][COLOR=#666600]));[/COLOR][COLOR=#880000]//used to change the font size[/COLOR][COLOR=#000000]
                        spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#666600]].[/COLOR][COLOR=#000000]addActionListener[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]this[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#880000]//give each spot an ActionListener[/COLOR][COLOR=#000000]
                        board[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]add[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#666600]]);[/COLOR][COLOR=#880000]//add the buttons[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
                
                [/COLOR][COLOR=#000088]this[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]add[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]board[/COLOR][COLOR=#666600],[/COLOR][COLOR=#660066]BorderLayout[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]CENTER[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
        
        [/COLOR][COLOR=#006666]@Override[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000088]public[/COLOR][COLOR=#000088]void[/COLOR][COLOR=#000000] actionPerformed[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]ActionEvent[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#666600])[/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
                [/COLOR][COLOR=#660066]String[/COLOR][COLOR=#000000] buttonText [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]getActionCommand[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
                
                [/COLOR][COLOR=#000088]if[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]buttonText[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]equals[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"-"[/COLOR][COLOR=#666600])){[/COLOR][COLOR=#000000]
                        spot[/COLOR][COLOR=#666600][[/COLOR][COLOR=#000000]e[/COLOR][COLOR=#666600].[/COLOR][COLOR=#000000]getID[/COLOR][COLOR=#666600]()].[/COLOR][COLOR=#000000]setText[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"Changed"[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000] 
                [/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
                
        [/COLOR][COLOR=#666600]}[/COLOR]
In the ActionListener method i'm trying to set the text on a specific button a user clicks on to the changed text. I can't find anything in the Java API to help me, is there some other way to do this?

Edited by mastrgamr, 14 October 2011 - 03:36 PM.

-- Stuart, Currently: Messing with LibGDX (Java)
aspiring video game programmer and college student. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
getSource should work. EventObject (Java Platform SE 7 )

the Object return will by getSource() the button that fired the action event. You'll need to cast the Object to JButton though and then call setText.

#3
mastrgamr

mastrgamr

    Newbie

  • Members
  • PipPip
  • 10 posts
niiice. Thanks a ton!

Solution:

	@Override
	public void actionPerformed(ActionEvent e) {
		String buttonText = e.getActionCommand();
		JButton source = (JButton) e.getSource();
		if(buttonText.equals("-")){
			source.setText("Changed");
		}
	}

-- Stuart, Currently: Messing with LibGDX (Java)
aspiring video game programmer and college student. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users