How can i display and image when button1 is pressed, this is the code im using to check for a button press:
public class clientFrameButtons {
public void frameClient(){........
....
button1.addActionListener(new ActionListener(){ //ActionListener checks for button press
public void actionPerformed(ActionEvent e){
// if button is pressed the following will happen
I've tried implementing an if statement and looked at several sources online but cannot find a answer to fit my needs.
Thanks.
ActionEvent is a logical event those intention is to signal the fact that a button was actioned. So it doesn't provide any information about the user inputs that have lead to the action (it may be a shortcut, a mouse press, your mouse could have been set to left-handed, etc). If you want to care about mouse, then you must catch mouse events, with an appropriate MouseListener.
So your problem can be expressed in two ways:
you want to display an image when a GUI button is actioned then use ActionListener.
you want to make different things depending on the mouse button pressed while over a GUI button, then use MouseListener.
Related
I'm creating a program that allows users to graph vertices and connect them by edges.
One of the buttons is "Add Vertex"
I want this button to be the only button that allows the user to create a vertex by clicking on the canvas. The same logic goes for the rest of the buttons.
I know I need to use mouselistener and/or actionlistener but I don't know how to set the permissions so that the user can't just click anywhere and draw.
All the questions and articles I've read on this only show me how to make a button do a particular action but I want this button, and only this button, to allow a user to do this particular action themselves.
Any references to questions similar to this or articles that address this would be extremely helpful. Thank you
So you need to keep track of whether or not you should draw on the canvas, so define a boolean instance field
private boolean shouldDraw;
Now in your button handler, when the button is clicked
shouldDraw = true
Then in your mouse listener, when the mouse is clicked you can check for the flag
if(shouldDraw)
{
shouldDraw = false;
//do necessary work to draw
}
Here is the problem,
I have 3 textfield, 3 button and 1 label. Their text are text1, text2 text3, but1,but2,but3.
I give you an example about what I want to do; When I double click on a button, button will change label's text as button's text. I mean when I double click to but2, label's text should be but2.
I can do this with that code;
MouseAdapter ml = new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (me.getClickCount()==2) {
jLabel1.setText(jButton1.getText);
}
}
};
jButton1.addMouseListener(ml);
So It works but it works only for jButton1. I have to write different mouseListener's for all components(textfields and jbuttons). How can I do this with one listener? or one event ? Do you have any idea?
Take a look at MouseEvent#getSource ... although, to be honest, if you're using JButtons you shouldn't be using a MouseListener, but ActionListener instead. Also, generally speaking, most users won't double click a JButton as it's not intuitive for them to do so, buttons only need a single click to activate
Remember, buttons can be activated by the keyboard as well, which MouseListener won't be notified about
I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon().
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.
I'm trying to make a form so that when a user checks a checkbox and clicks a button, some code will execute. I've tried to do this in an if statement and nothing happens when I do the 2 things. I am doing this in Java with Swing.
Here is the code:
private class theHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String tftext;
tftext = tf1.getText();
if (event.getSource()==b1)
if(event.getSource()==cb1)
JOptionPane.showMessageDialog(null, tftext, "title", JOptionPane.INFORMATION_MESSAGE);
b1 is a button, cb1 is a checkbox and tf1 is a textfield.
Event.getSource() won't reference two different objects, it should reference the unique source of a single event, e.g. a Button in the case of a button click. Your nested statement will never execute.
It sounds like you should be handling the button click, and within that event handler check the state (checked or not) of the checkbox. If the checkbox is checked, then show your dialog.
Basically what you are saying there is that if the event came from the button and the event came from the checkbox, show a message.
This is not possible because one event cannot be triggered by a button and a checkbox in the same time. You cannot click on both in the same time.
private void button_Clicked_download(MouseEvent e) {
button_dl.setEnabled(false);
System.out.println("Button Clicked.");
}
When I click the button the button looks disabled. However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
How can I make it so if the button is clicked it ignores the code and is indeed disabled?
However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
This is exactly why you shouldn't use a MouseListener with a JButton but rather an ActionListener. The solution is of course obvious -- to get rid of the MouseListener and instead add an ActionListener to the JButton of interest.
you need to use an ActionListener instead of MouseClickListener.
your button is logically clicked even if it is disabled so click event will execute
There is actually a very simple way of enabling and disabling a button in java which uses a Mouse Listener.
class HoldListen extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JButton bt = (JButton)e.getSource();
if (!bt.isEnabled()) {
return;
}
// Do code
}
}
I found your question while trying to create something similar and this is how I solved it.
All the MouseListener's methods return void so it works out quite nice. In my situation going back to an ActionListener would have required a lot of extra work while a MouseListener was perfect for the job. Press set a variable which Release undid and another thread used the variable in a ongoing simulation.