I am trying to create buttons that change the color of an object when they are pressed. However the object changes color whenever my mouse merely hovers over the button. Am I using the wrong listener? I'm not sure where I'm going wrong. Thanks in advance.
blue.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
object.setColor(color.blue);
objectIcon.repaint();
}
}
);
Try to use an ActionListener on the button.
E.g.
blue.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
object.setColor(color.blue);
objectIcon.repaint();
}
});
Related
Can anyone help me with my issue? I can't seem to figure out how I can make a jButton hide after it has been clicked ONCE.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//The hide code would go here
}
you are looking for
jButton1.setVisible(false);
First of all, just use implement interface actionListener to your class:
public class test implements ActionListener {
Next, add actionListener to your button:
button.addActionListener(this);
Last in the implemented actionPerformed method:
public void actionPerformed(ActionEvent e){
if(e.getSource.equals(button)) button.setVisible(false); //set's the buttons visibility to false.
}
**Edit: If you want to do it for one button click, in the actionPerformed method if statement you can also do: **
public void actionPerformed(ActionEvent e){
if(e.getSource.equals(button) && !once){
button.setVisible(false);
once = true; //once is a boolean which shows if the button has been clicked once
}
}
Hope this helps.
I am a studying programming we just started Swing I have to make a simple boat management I need about 20 buttons. I am using the setVisible() method for every button I just wonder if is there another way of doing that.
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
button.setVisible(false);
button1.setVisible(false);
button2.setVisible(true);
button3.setVisible(true);
}
});
If I understand your question, you could define two utility methods like
static void setVisible(JButton... btns) {
for (JButton btn : btns) {
btn.setVisible(true);
}
}
static void setInvisible(JButton... btns) {
for (JButton btn : btns) {
btn.setVisible(false);
}
}
Then you could call those with any number of buttons; like
setInvisible(button, button1);
setVisible(button2, button3);
As for making different buttons do different things, define an ActionListener per button (or per unique action).
Just add another new ActionListener(){....} to each button and modify the actionPerformed(ActionEvent e) method accordingly
You can just implement the action listener in your class like:
public class XYZ implements ActionListener
Then add it to your buttons like:
b1.addActionListener(this);
b2.addActionListener(this);
...
Then override the actionPerformed method:
public void actionPerformed(ActionEvent e) {
//Here do your tasks.
// To identify the button, use : e.getSource();
}
As the title says, I would like to know the syntax for that
I am making a first person boxing game when you click the button it punches the enemy but after releasing it, the image of the punch disappears right away.
the concept of the game is how many click the user can make in a certain period of time
I don't have a code to show because its blank inside the button
Start with this:
JButton btnPunch = new JButton("");
btnPunch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnPunch.setVisible(btnPunch.isVisible() ? false : true);
}
});
If you want an action to be done On Release use:
addMouseListener(new
MouseAdapter() {
public void mouseReleased(MouseEvent e)
{ // TODO: add your code here
} });
I want when I enter a button, text to appear in the console. How can I combine the methods there is my comfusing, can someone explain and give example.
Try
JButton button = new JButton("Button1");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button1 was Clicked!");
}
});
// add button to a container
Use a MouseListener. For example:
JComponent button = new JButton();
component.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered the button");
}
});
MouseAdapter is a special MouseListener that has default empty implementations of all the other methods that the MouseListener provides, so you don't have to override them. You may want to look at the Javadoc for MouseAdapter, MouseListener, and MouseEvent.
Add an ActionListener to the button. In the actionPerformed() method print text on the console or whatever else you want.
Im trying to use a KeyListener to do something when the button m is pressed, it needs to add an image to a JPanel, however it doesn't do anything :/
I use :
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==('m')){
panel.add(mario);
Thread marioS = new AePlayWave("sm64itsamemario.wav");
marioS.start();
}
}
});
Edit:
The answer is to set the focus to the panel before trying to invoke the listener :)
so I added a mouselistener that sets the focus to the panel on click :
panel.addMouseListener( new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) {
panel.requestFocusInWindow();
});