codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Jbutton events  finansa at 10:15 on Thursday, February 23, 2006
 

hi


i have a 10 by 10 grid of buttons and 3 single buttons. when i click any one of the three buttons it triggers the same event but i only want one of them to trigger the event.

how do go about doing this?

i need one of the buttons to trigger more than one event.

how do i go about doing this?

please help

thank you


my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Robot extends JFrame implements ActionListener
{
private JButton[][] b = new JButton[20][20];
private JButton east, east2, east3, east4;
private JLabel square1Label, direction1Label;
private JTextField squareField, directionField;


public Robot()
{
super ("M shape Program");
Container yourContainer;
yourContainer = getContentPane();
yourContainer.setLayout (new BorderLayout());


JButton east = new JButton("Forward");
JButton east2 = new JButton("Roatate");
JButton east3 = new JButton("Clear");
JButton east4 = new JButton("Exit");

east.addActionListener(this);
east2.addActionListener(this);
east3.addActionListener(this);
east4.addActionListener(this);

JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(10,18,5,4));

eastPanel.add(east);
eastPanel.add(east2);
eastPanel.add(east3);
eastPanel.add(east4);
yourContainer.add(eastPanel, BorderLayout.EAST);


JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));

for(int y = 0;y < 10;y++)
{
for(int x = 0;x < 10;x++)
{
b[x][y] = new JButton("");
centerPanel.add(b[x][y]);
b[x][y].setSize(200,400);
b[x][y].setBackground(Color.green);

b[x][y].addActionListener(this);



}
}
yourContainer.add(centerPanel, BorderLayout.CENTER);


JPanel southPanel = new JPanel ();
square1Label = new JLabel("Square: "); southPanel.add(square1Label);

squareField = new JTextField(2);
southPanel.add(squareField);

direction1Label = new JLabel("Direction: "); southPanel.add(direction1Label);

directionField = new JTextField(2); southPanel.add(directionField);


yourContainer.add(southPanel, BorderLayout.SOUTH);

pack();
setSize(500, 400);
setVisible(true); }


public void actionPerformed(ActionEvent event){

String actioncommand = event.getActionCommand();
if (actioncommand.compareTo("Exit") == 0)
System.exit(0);
{
if(event.getSource() == east);
else b[0][9].setBackground(Color.black);




}

}}

  Re: Jbutton events  crwood at 18:42 on Thursday, February 23, 2006
 

but i only want one of them to trigger the event
how do go about doing this?

// ...inside constructor
// declaring these (again) as local variables
// ie, JButton east = ...
// leaves the instance variable declarations (above) null
east = new JButton("Forward");
east2 = new JButton("Roatate");
east3 = new JButton("Clear");
east4 = new JButton("Exit");

// its a good idea to set these if you want to use them
east.setActionCommand("forward");
east4.setActionCommand("exit");
...

public void actionPerformed(ActionEvent event)
{
JButton button = (JButton)event.getSource();
String actionCommand = button.getActionCommand();
// show which JButton sent the event:
System.out.println("actionCommand = " + actionCommand);
// two ways to identify a button in this method:
// 1 - use its actionCommand property
if (actionCommand.equals("exit")) // east4
System.exit(0);
if(actionCommand.equals("forward")) // east
;

// 2 - use its reference (from your declaration at top)
if(button == east2) // rotate
{
/* doSomething() */
/* callAnotherMethod() */ ;
}
if(button == east3) // clear
{
/* do some things in this block (curley braces) */
;
}
else
b[0][9].setBackground(Color.black);
}

i need one of the buttons to trigger more than one event.
how do i go about doing this?
see actionPerformed above for one way: call more than one method or do more than one thing (via statements) inside the curley braces for the JButton that you want to do more than one thing. You can also add more than one ActionListener to a JButton; this is rare.
This code below runs okay. I renamed it so you can run it as–is without name clashing.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Robot1 extends JFrame implements ActionListener
{
private JButton[][] b = new JButton[10][10];
private JButton east, east2, east3, east4;
private JLabel square1Label, direction1Label;
private JTextField squareField, directionField;

public Robot1()
{
super ("M shape Program");
Container yourContainer;
yourContainer = getContentPane();
yourContainer.setLayout (new BorderLayout());

// declaring these (again) as local variables
// ie, JButton east = ...
// leaves the instance variable declarations (above) null
east = new JButton("Forward");
east2 = new JButton("Roatate");
east3 = new JButton("Clear");
east4 = new JButton("Exit");

// its a good idea to set these if you want to use them
east.setActionCommand("forward");
east4.setActionCommand("exit");

east.addActionListener(this);
east2.addActionListener(this);
east3.addActionListener(this);
east4.addActionListener(this);

JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(10,18,5,4));

eastPanel.add(east);
eastPanel.add(east2);
eastPanel.add(east3);
eastPanel.add(east4);
yourContainer.add(eastPanel, BorderLayout.EAST);

JPanel centerPanel = new JPanel ();
centerPanel.setLayout(new GridLayout(10,10));

for(int y = 0;y < 10;y++)
{
for(int x = 0;x < 10;x++)
{
b[x][y] = new JButton("");
centerPanel.add(b[x][y]);
// this has no affect
// setPreferredSize works better
// b[x][y].setSize(200,400);
b[x][y].setBackground(Color.green);

b[x][y].addActionListener(this);
}
}
yourContainer.add(centerPanel, BorderLayout.CENTER);

JPanel southPanel = new JPanel ();
square1Label = new JLabel("Square: ");
southPanel.add(square1Label);

squareField = new JTextField(2);
southPanel.add(squareField);

direction1Label = new JLabel("Direction: "); southPanel.add(direction1Label);

directionField = new JTextField(2); southPanel.add(directionField);

yourContainer.add(southPanel, BorderLayout.SOUTH);

setDefaultCloseOperation(EXIT_ON_CLOSE);
// use one or the other, not both
// pack();
setSize(500, 400);
setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
JButton button = (JButton)event.getSource();
String actionCommand = button.getActionCommand();
// show which JButton sent the event:
System.out.println("actionCommand = " + actionCommand);
// two ways to identify a button in this method:
// 1 - use its actionCommand property
if (actionCommand.equals("exit")) // east4
System.exit(0);
if(actionCommand.equals("forward")) // east
;

// 2 - use its reference (from your declaration at top)
if(button == east2) // rotate
{
/* doSomething() */
/* callAnotherMethod() */ ;
}
if(button == east3) // clear
{
/* do some things in this block (curley braces) */
;
}
else
b[0][9].setBackground(Color.black);
}

public static void main(String[] args)
{
new Robot1();
}
}


  Re: Jbutton events  finansa at 16:19 on Friday, February 24, 2006
 

thank you. i'l try it when i get home.

if i get stuck again could you help out?


thanks my friend

  Re: Jbutton events  crwood at 17:01 on Friday, February 24, 2006
 

Yes.

  Re: Jbutton events  finansa at 18:01 on Friday, February 24, 2006
 

hi again. you mention these statments:
/* doSomething() */
/* callAnotherMethod() */ ;

i don't understand where to put my statments in this section



i need the forward button to make these event's every time i press the button click after click:

b[0][9].setBackground(Color.black);
b[0][8].setBackground(Color.black);
b[0][7].setBackground(Color.black);
b[0][6].setBackground(Color.black);
b[0][5].setBackground(Color.black);
b[0][4].setBackground(Color.black);
b[0][3].setBackground(Color.black);
b[0][2].setBackground(Color.black);

the things is i don't know how to make it do these events per click in the actionPerformed?


thank you





  Re: Jbutton events  crwood at 01:43 on Monday, February 27, 2006
 

You can put them under the if statement that tests for the JButton that you want to trigger the color change, say like

public void actionPerformed(...
...
if (actionCommand.equals("exit")) // east4
System.exit(0);

// or for whatever JButton you want to
// trigger this change...
if(actionCommand.equals("forward")) // east
{
for(int j = 2; j < 10; j++)
b[0][j].setBackground(Color.black);
}

or you can call a method from actionPerformed and put the code in the method, say

public void actionPerformed(...
...
if (actionCommand.equals("exit")) // east4
System.exit(0);
if(actionCommand.equals("forward")) // east
change backGroundColor();
...
/** somewhere in the Robot class... */
private void changeBackgroundColor()
{
for(int j = 2; j < 10; j++)
b[0][j].setBackground(Color.black);
}


  Re: Jbutton events  finansa at 18:12 on Monday, February 27, 2006
 

hi mate.

its turning all those buttons black from pressing forward button once with this statment:
for(int j = 9; j < 10; j++)
b[0][j].setBackground(Color.black);


i need it to be set so that it turns a button black in the grid every time you press forward only.

abit like this

press forward once:
b[0][9].setBackground(Color.black);

press forward again:
b[0][8].setBackground(Color.black);

press forward again
b[0][7].setBackground(Color.black);


kind regards


  Re: Jbutton events  crwood at 20:01 on Monday, February 27, 2006
 

Keep track of the row number in the class as an instance variable, say count.

public class Robot1 extends JFrame implements ActionListener
{
...
int count; // instance variable

public Robot1()
{
super ("M shape Program");
count = 9;
...

public void actionPerformed(ActionEvent event)
{
if(actionCommand.equals("forward")) // east
{
if(count < 2 /*some_limit*/)
/* do something about it/reset it */;
b[0][count--].setBackground(Color.black);
}

Or you could use two instance variables, say row and col and change those (values) around in your event code. You'll have to test for them to keep them within limits to avoid ArrayIndexOutOfBoundsExceptions.

  Re: Jbutton events  finansa at 23:15 on Monday, February 27, 2006
 

hi mate

i don't know what statment to put after
if(count < 2....... and on the second line.

i'm just getting errors


if(count < 2 /*some_limit*/)
/* do something about it/reset it */;
b[0][count --].setBackground(Color.black);


i don't understand this section to well only parts of it.


sorry mate


  Re: Jbutton events  crwood at 00:40 on Tuesday, February 28, 2006
 

If we keep pressing the "Forward" button the black buttons march up the left hand column until the top one turns black, ie b[0][0]. The next press of the "forward" button tries to call this:

b[0][-1].setBackground(Color.black);

in the actionPerformed method. Ouch — ArrayIndexOutOfBoundsException. There is no array element for b[0][-1]. So you want to make sure that the count instance variable cannot go below zero or above the highest index in the b[][this part] array, here, for index = 9. So we can add a test or two to keep count within bounds no matter what the user may do.

if(actionCommand.equals("forward")) // east
{
if(count >= 0 && count < b[0].length) // safe zone
b[0][count--].setBackground(Color.black);
}

It all depands on what you want to do with the "Formward" button and making the grid buttons black. If you want to keep moving from right to left you could change the indices of b[][] like this

public class Robot1 extends JFrame implements ActionListener
{
// instance variables in class scope
...
int row, col;

public Robot1()
{
super ("M shape Program");
row = 9; // start at
col = 0; // southwest corner
....
}
...
public void actionPerformed(ActionEvent event)
{
JButton button = (JButton)event.getSource();
String actionCommand = button.getActionCommand();
...
if(actionCommand.equals("forward")) // east
{
if(row < 0)
{
col++; // next column
row = b.length-1; // reset to bottom row
}
if(col > b[0].length-1) // past the last column
{
// now what?
col = 0; // start over
// or what ever you have in mind...
}
b[row][col].setBackground(Color.black);

// counting up from bottom to top with each press
// of this button -> row decrements for next time
row--;
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: need help in applet communication
•  Re: MS Access connection in JSP
•  Highlight a DataGrid Row and Stay there -
•  Numeric field
•  Re: visible/invisible divs
•  radio buttons questionnaire
•  Re: Splitter in DHTML, no frames
•  Re: how to set value to texbox
•  Re: IE page Redirect


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006