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:
  button color  nicegyal at 20:51 on Saturday, April 22, 2006
 

I consider myself a beginner in programming so I need a little help if that is possible�below is the code of my program �.i have designed 100 buttons by using 2D array method�.

But the problem that I�m having is I can�t figure out how to change the colour of any of the buttons when I click it


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

public class RobotPath extends JFrame
implements ActionListener {

JPanel buttonPanel, robotPathPanel, directionPanel;
JMenuBar robotMenuBar;
JMenu fileMenu, helpMenu;
JMenuItem exitItem, aboutItem, istructionsItem;
JButton tbtForward, tbtRotate, tbtClear, tbtExit;
JButton[][] tbtPath;
int row = 10, col = 10;
int x = 0, y = 0;

public static void main (String[] args) {
RobotPath frame = new RobotPath();
frame.setTitle("Robot Path Emulator - Java Application"); //frame title
frame.setBounds(250, 90, // setting the position
750, 600); //setting the size
frame.createGUI();
frame.setResizable(false);
frame.setVisible(true); //displaying the window

}


private void createGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() ); // Set border layout manager

//set the menu bar
robotMenuBar = new JMenuBar();
setJMenuBar(robotMenuBar);

// file menu, with Exit
fileMenu = new JMenu("File");

exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(this);

robotMenuBar.add(fileMenu);

// help menu, with about, instructions
helpMenu = new JMenu("Help");

aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
aboutItem.addActionListener(this);

istructionsItem = new JMenuItem("Istructions");
helpMenu.add(istructionsItem);
istructionsItem.addActionListener(this);

robotMenuBar.add(helpMenu );

//JPanel for the buttons
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(20, 1));

tbtForward = new JButton("Forward"); //Creating buttons: Forward,
buttonPanel.add(tbtForward); //Rotate, Clear, Exite
tbtForward.addActionListener(this);

tbtRotate = new JButton("Rotate");
buttonPanel.add(tbtRotate);
tbtRotate.addActionListener(this);

tbtClear = new JButton("Clear");
buttonPanel.add(tbtClear);
tbtClear.addActionListener(this);

tbtExit = new JButton("Exit");
buttonPanel.add(tbtExit);
tbtExit.addActionListener(this);

//JPanel for the Robot Path Emulator
robotPathPanel = new JPanel();
robotPathPanel.setBackground(Color.white);

JButton[][] tbtPath = new JButton[row][col]; // Array to store buttons
for (int x = 0 ; x < row ; x++)
{
for (int y = 0 ; y < col ; y++)
{
tbtPath [x] [y] = new JButton ("(" + x + "," + y + ")");
robotPathPanel.add(tbtPath[x][y]);
tbtPath [x] [y].setBackground (Color.green);
tbtPath [x] [y].addActionListener (this);
}
}

//JPanel for the directions
directionPanel = new JPanel();

window.add("East", buttonPanel);
window.add("Center", robotPathPanel);
window.add("South", directionPanel);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == exitItem) {
System.exit(0);
}
if(e.getSource() == tbtExit) {
System.exit(0);
}

}
}


  Re: button color  crwood at 01:53 on Sunday, April 23, 2006
 


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

public class RP extends JFrame implements ActionListener {

JPanel buttonPanel, robotPathPanel, directionPanel;
JMenuBar robotMenuBar;
JMenu fileMenu, helpMenu;
JMenuItem exitItem, aboutItem, istructionsItem;
JButton tbtForward, tbtRotate, tbtClear, tbtExit;
JButton[][] tbtPath;
int row = 10, col = 10;
int x = 0, y = 0;

public static void main (String[] args) {
RP frame = new RP();
frame.setTitle("Robot Path Emulator - Java Application"); //frame title
frame.setBounds(250, 90, // setting the position
750, 600); //setting the size
frame.createGUI();
// frame.setResizable(false);
frame.setVisible(true); //displaying the window
}

private void createGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
// BorderLayout is the default layout manager for JFrame
window.setLayout(new BorderLayout() ); // Set border layout manager

//set the menu bar
robotMenuBar = new JMenuBar();
setJMenuBar(robotMenuBar);

// file menu, with Exit
fileMenu = new JMenu("File");

exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(this);

robotMenuBar.add(fileMenu);

// help menu, with about, instructions
helpMenu = new JMenu("Help");

aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
aboutItem.addActionListener(this);

istructionsItem = new JMenuItem("Istructions");
helpMenu.add(istructionsItem);
istructionsItem.addActionListener(this);

robotMenuBar.add(helpMenu );

//JPanel for the buttons
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
gbc.fill = gbc.HORIZONTAL;
tbtForward = new JButton("Forward"); //Creating buttons: Forward,
buttonPanel.add(tbtForward, gbc); //Rotate, Clear, Exite
tbtForward.addActionListener(this);

tbtRotate = new JButton("Rotate");
buttonPanel.add(tbtRotate, gbc);
tbtRotate.addActionListener(this);

tbtClear = new JButton("Clear");
buttonPanel.add(tbtClear, gbc);
tbtClear.addActionListener(this);
tbtExit = new JButton("Exit");
buttonPanel.add(tbtExit, gbc);
tbtExit.addActionListener(this);
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
buttonPanel.add(new JLabel(), gbc);

//JPanel for the Robot Path Emulator
robotPathPanel = new JPanel(new GridLayout(10,0,2,2));
robotPathPanel.setBackground(Color.white);

ButtonListener buttonListener = new ButtonListener();
JButton[][] tbtPath = new JButton[row][col]; // Array to store buttons
for (int x = 0 ; x < row ; x++) {
for (int y = 0 ; y < col ; y++) {
tbtPath [x] [y] = new JButton ("(" + x + "," + y + ")");
robotPathPanel.add(tbtPath[x][y]);
tbtPath [x] [y].setBackground (Color.green);
tbtPath [x] [y].addActionListener (buttonListener);
}
}

//JPanel for the directions
directionPanel = new JPanel();

window.add("East", buttonPanel);
window.add("Center", robotPathPanel);
window.add("South", directionPanel);
}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == exitItem) {
System.exit(0);
}
if(e.getSource() == tbtExit) {
System.exit(0);
}
}

private class ButtonListener implements ActionListener {
Color selectedColor = Color.red;

public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
System.out.println(button.getBackground() != selectedColor);
if(button.getBackground() != selectedColor)
button.setBackground(selectedColor);
}
}
}


  Re: button color  nicegyal at 09:59 on Sunday, April 23, 2006
 

thanks i will edit it my own version with what you have suggested….i hope that my question was understandable …as the original color of the buttons was green and I want it to change to be black when I click and of the buttons…

  Re: button color  WhiteWizard at 04:49 on Tuesday, April 25, 2006
 

I have modified the code. check this. A mouse listener is attached to it. the buttons are setnamed as their i,j values. from this u can get the name of the button which is clicked.


=======================================================
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;

public class RobotPath extends JFrame
implements ActionListener {

JPanel buttonPanel, robotPathPanel, directionPanel;
JMenuBar robotMenuBar;
JMenu fileMenu, helpMenu;
JMenuItem exitItem, aboutItem, istructionsItem;
JButton tbtForward, tbtRotate, tbtClear, tbtExit;
JButton[][] tbtPath;
int row = 10, col = 10;
int x = 0, y = 0;

public static void main (String[] args) {
RobotPath frame = new RobotPath();
frame.setTitle("Robot Path Emulator - Java Application"); //frame title
frame.setBounds(250, 90, // setting the position
750, 600); //setting the size
frame.createGUI();
frame.setResizable(false);
frame.setVisible(true); //displaying the window

}


private void createGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() ); // Set border layout manager

//set the menu bar
robotMenuBar = new JMenuBar();
setJMenuBar(robotMenuBar);

// file menu, with Exit
fileMenu = new JMenu("File");

exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(this);

robotMenuBar.add(fileMenu);

// help menu, with about, instructions
helpMenu = new JMenu("Help");

aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
aboutItem.addActionListener(this);

istructionsItem = new JMenuItem("Istructions");
helpMenu.add(istructionsItem);
istructionsItem.addActionListener(this);

robotMenuBar.add(helpMenu );

//JPanel for the buttons
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(20, 1));

tbtForward = new JButton("Forward"); //Creating buttons: Forward,
buttonPanel.add(tbtForward); //Rotate, Clear, Exite
tbtForward.addActionListener(this);

tbtRotate = new JButton("Rotate");
buttonPanel.add(tbtRotate);
tbtRotate.addActionListener(this);

tbtClear = new JButton("Clear");
buttonPanel.add(tbtClear);
tbtClear.addActionListener(this);

tbtExit = new JButton("Exit");
buttonPanel.add(tbtExit);
tbtExit.addActionListener(this);

//JPanel for the Robot Path Emulator
robotPathPanel = new JPanel();
robotPathPanel.setBackground(Color.white);

JButton[][] tbtPath = new JButton[row][col]; // Array to store buttons
for (int x = 0 ; x < row ; x++)
{
for (int y = 0 ; y < col ; y++)
{
tbtPath [x] [y] = new JButton ("(" + x + "," + y + ")");
tbtPath [x] [y].setName(x+""+y);
tbtPath [x] [y].addMouseListener(new ButtonListener());
robotPathPanel.add(tbtPath[x][y]);
tbtPath [x] [y].setBackground (Color.green);
tbtPath [x] [y].addActionListener (this);
}
}

//JPanel for the directions
directionPanel = new JPanel();

window.add("East", buttonPanel);
window.add("Center", robotPathPanel);
window.add("South", directionPanel);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == exitItem) {
System.exit(0);
}
if(e.getSource() == tbtExit) {
System.exit(0);
}

}
class ButtonListener extends MouseAdapter
{

public void mouseClicked(MouseEvent mc)
{
try{
JButton this_lab = (JButton)mc.getSource();
System.out.println(this_lab.getName());
if(this_lab.getName().equals("00"))
{
System.out.println("Hello World");
}
}catch(Exception ex){

}
}
public void mouseEntered(MouseEvent me)
{
try{
JButton this_lab = (JButton)me.getSource();
}catch(Exception ex){

}
}
public void mouseExited(MouseEvent mx)
{
try{
JButton this_lab = (JButton)mx.getSource();

}catch(Exception ex){

}
}
}
}
=======================================================








CodeToad Experts

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








Recent Forum Threads
•  Re: C++ Beginner question
•  Re: function within loop problem
•  moncler outlet
•  Re: Display swf file in pdf
•  Re: how to create forum using asp.net with c# language?
•  Discount coach store
•  Create Better Maps with Global Mapper
•  Re: How to display a message box when record is temporary stored in session/grid view
•  Sharing object in perl


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2012