Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionListener question

Status
Not open for further replies.

bos

Technical User
Oct 1, 2002
50
0
0
US
On a program I'm working on, I'd like to be able to either right-click or alt-click or something similar to get two separate actions from the same button. I usually use the button.addActionListener(actionevent e) code for buttons but I don't know how to implement the ideas above. Please let me know any suggestions you might have.

Thanks
 
I think a good way to do what you want is to use the
Code:
addMouseListener
method from the superclass
Code:
Component
Water is not bad as soon as it stays out human body ;-)
 
Unfortnately, I do not belive it is possible to capture the right-click event in java (unless there is a method in 1.4). I've read through the 'Mouse*' classes in java.awt.event and there are only references to "mouse clicked" - no left or right button events.

However in the class MouseEvent class (triggered by MouseListener or MouseMotionListener, there is a 'getClickCount()' method - which I reckon you could set up to listen for double-clicks (instead of right-clicks).

Failing that, it is possible to link actions to keyboard depressions - take a look at the KeyEvent and KeyListener classes - you can add mneumonics to your buttons to indicate these actions - or even ToolTips when the mouse passes over the button.

Failing that - have you considered a second button ?!
 
Hi bos,
To do this you will need to add a
Code:
MouseListener
to your button. This will allow you to determine when they press down on the button, using
Code:
mousePressed (MouseEvent)
, as well as which button was pressed. Since
Code:
MouseEvent
inherits from
Code:
InputEvent
, you can also check for things like alt-down or ctrl-down using the methods
Code:
isAltDown ()
,
Code:
isControlDown ()
, etc. This should allow you to do what you want. [smile]

Hope this helps,
MarsChelios
 
You can check if the right mouse button was clicked.
here is the code for checking this:


if ((m.getModifiers() & InputEvent.BUTTON3_MASK)== InputEvent.BUTTON3_MASK)
{

....

}

the InputEvent.BUTTON3_MASK is static from the package java.awt.event

m stands for the mouseEvent

greetz Cruisy
 
hey, Bos, are you still alive ???
Did theses hints helped ??? Water is not bad as soon as it stays out human body ;-)
 
Sorry about that. I was out for awhile and I assumed that the initial responses to my post were the final word on the subject. You were all helpful in your posts; however I have to admit that what I know in java is self-taught so I am a little unfamiliar with mouselisteners. I've taken the code of the program I'm writing and cut out what I deem are the unnecessary parts. I was wondering if one of you could give me an example based off my current code on how to implement the mouselisteners you mentioned. The array of buttons I want to listen for mouse actions is right under the //*********************** segment of the program and is called treevg[]. Currently they have an actionlistener because of my inexperience with mouselisteners. If you need the whole code, I can send it later.


Here it is, and go easy on me:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.border.*;
import java.io.*;
import javax.swing.filechooser.*;

public class ims3 extends JFrame {



public ims3() {
super("Testing");
setSize (970, 715);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container pane = getContentPane();



for (vgcreateint=1; vgcreateint<=32; vgcreateint++) {
treevg[vgcreateint] = new JButton(emptyvg);
treevg[vgcreateint].setBounds(20, 40+(36*(vgcreateint-1)), 50, 36);
treevg[vgcreateint].setBackground(new Color(250, 250, 250));
treevg[vgcreateint].setBorder(BorderFactory.createEmptyBorder());
treelabel[vgcreateint] = new JButton(&quot;Volume Group &quot;+vgcreateint);
treelabel[vgcreateint].setBounds(54, 40+(36*(vgcreateint-1)), 140, 36);
treelabel[vgcreateint].setBackground(new Color(250, 250, 250));
treelabel[vgcreateint].setBorder(BorderFactory.createEmptyBorder());
final int vgcreateint2=vgcreateint;
//*************************************************************//
treevg[vgcreateint].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent w) {
currentvg=vgcreateint2;
for (vgcreateint3=1; vgcreateint3<=32; vgcreateint3++) {
treevg[vgcreateint3].setBackground(new Color(250, 250, 250));
treelabel[vgcreateint3].setBackground(new Color(250, 250, 250));
treearray.setBackground(new Color(250, 250, 250));
}
treevg[vgcreateint2].setBackground(new Color(0, 250, 0));
treelabel[vgcreateint2].setBackground(new Color(0, 250, 0));
if (activepanel==true) {
activepanel=false;
midpanel.remove(midrightb);
midpanel.add(midrighta);
midpanel.add(volumepane);
midpanel.setVisible(false);
midpanel.setVisible(true);
}
volumepanel.removeAll();
volumepanel.add(vgheaderbutton);
for (abc3=1; abc3<=numberofvolumes[vgcreateint2]; abc3++) {
if (numberofvolumes[vgcreateint2] != abc3) {
treevolume[abc3].setIcon(volume);
volumepanel.add(treevolume[abc3]);
volumepanel.add(vollabel[abc3]);
volumepanel.add(vollabel2[abc3]);
}
else
{
treevolume[abc3].setIcon(volumeend);
volumepanel.add(treevolume[abc3]);
volumepanel.add(vollabel[abc3]);
volumepanel.add(vollabel2[abc3]);
}
}
volumepanel.setVisible(false);
volumepanel.setVisible(true);
volumepanel.setPreferredSize(new Dimension(220, (50+(46*numberofvolumes[vgcreateint2]))));
}
});
treelabel[vgcreateint].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentvg=vgcreateint2;
for (vgcreateint3=1; vgcreateint3<=32; vgcreateint3++) {
treevg[vgcreateint3].setBackground(new Color(250, 250, 250));
treelabel[vgcreateint3].setBackground(new Color(250, 250, 250));
treearray.setBackground(new Color(250, 250, 250));
}
treevg[vgcreateint2].setBackground(new Color(0, 250, 0));
treelabel[vgcreateint2].setBackground(new Color(0, 250, 0));
if (activepanel==true) {
activepanel=false;
midpanel.remove(midrightb);
midpanel.add(midrighta);
midpanel.add(volumepane);
midpanel.setVisible(false);
midpanel.setVisible(true);
}
volumepanel.removeAll();
volumepanel.add(vgheaderbutton);
for (abc3=1; abc3<=numberofvolumes[vgcreateint2]; abc3++) {
if (numberofvolumes[vgcreateint2] != abc3) {
treevolume[abc3].setIcon(volume);
volumepanel.add(treevolume[abc3]);
volumepanel.add(vollabel[abc3]);
volumepanel.add(vollabel2[abc3]);
}
else
{
treevolume[abc3].setIcon(volumeend);
volumepanel.add(treevolume[abc3]);
volumepanel.add(vollabel[abc3]);
volumepanel.add(vollabel2[abc3]);
}
}
volumepanel.setVisible(false);
volumepanel.setVisible(true);
volumepanel.setPreferredSize(new Dimension(220, (50+(46*numberofvolumes[vgcreateint2]))));
}
});
}

// ----------------------- Treearray Button ---------------------------------- //

treearray.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (vgcreateint3=1; vgcreateint3<=32; vgcreateint3++) {
treevg[vgcreateint3].setBackground(new Color(250, 250, 250));
treelabel[vgcreateint3].setBackground(new Color(250, 250, 250));
}
treearray.setBackground(new Color(0, 250, 0));
if (activepanel==false) {
activepanel=true;
midpanel.remove(midrighta);
midpanel.remove(volumepane);
midpanel.add(midrightb);

midpanel.setVisible(false);
midpanel.setVisible(true);
}
}
});

// ------------------------ arrayvolnum Button --------------------------------- //

arrayvolnum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (abc=1; abc<=vgcount; abc++) {
numberofvolumes[abc]=Integer.parseInt(&quot;0&quot; + arrayvolnumfield.getText());
if (numberofvolumes[abc]>=1) {
if (abc != vgcount) {
treevg[abc].setIcon(vgit);
} else {
treevg[abc].setIcon(vgendit);
}
} else {
if (abc != vgcount) {
treevg[abc].setIcon(emptyvg);
} else {
treevg[abc].setIcon(emptyvgend);
}
}
}

}
});



setVisible(true);
}

public static void main(String[] arguments) {
ims3 imthree = new ims3();

}
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top