I am creating a click test for mousepad in Java. I can differentiate between left, right and middle click through:
if(evt.getButton()==3) // or 1 or 2
What I can't seem to do is to differentiate between 2 lefts.
The 2 lefts being click on 1 and click on 2 in the image above. I have tried to see if I get different values from them when I click by debugging and checking the event object but its the same. For keyboard one can differentiate between two Ctrl or 2 shifts by getting keyLocation, can one do something similar with click? Like not get where on the screen is clicked but which button was pressed.
private void formMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(evt.getButton());
if (evt.getButton() == 3) {
//3=right
button1.setBackground(Color.GREEN);
textField1.setText("Code : Right");
} else if (evt.getButton() == 2) {
//middle
button6.setBackground(Color.GREEN);
textField1.setText("Code : Middle");
} else if (evt.getButton() == 1) {
//1=left
button5.setBackground(Color.GREEN);
textField1.setText("Code : Left");
} else {
textField1.setText("Code : " + evt.getButton());
}
}
This above is my code so far in regards with click.
I have searched a lot but couldn't find information that could help yet.
As far as I know, there is no way to do this. The trackpad will simply present itself as if mouse1 has been pressed when you click the trackpad. Keyboards have different keycodes for left/right shift, etc... I don't believe there are different keycodes for "trackpad click" and "mouse 1".
Anyway, why would you want to add functionality that only laptop users (and only certain laptop users) would be able to use. What about desktops? What about mobile?
You can you below code SwingUtilities methods
SwingUtilities.isLeftMouseButton(MouseEvent anEvent)
SwingUtilities.isRightMouseButton(MouseEvent anEvent)
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)
Like:
if(SwingUtilities.isLeftMouseButton(evt)){
// do your work
}
// same for right and middle mouse key
But if you want to check where click happed on the mouse from enter link description here
Java Swing is an old technology, it supports the traditional mouse wheel rotation events.
So i don't think its possible to get difference b/w click happening on diff location of touchpad.
From MouseEvent
There is no way to get the location of mouse click.
Related
I'd like to know if it's possible to navigate through Vaadin grid or treegrid and select an item using only keyboard arrow keys? From what i've seen while testing the components, the default behavior seems to be either to move only to one specific cell in grid or to a specific row in treegrid. Selection can be achieved if the user presses spacebar.
I've tried to add a shortcutListener to grid but it doesn't seem to work with arrow keys. And the grid scrollbar doesn't move with the selected item.
grid.addShortcutListener(new ShortcutListener("Down", KeyCode.ARROW_DOWN, null) {
#Override
public void handleAction(Object sender, Object target) {
//..//
selectedItem = dataSource.get(currentSelectedItemIndex);
grid.select(selectedItem);
grid.scrollTo(currentSelectedItemIndex); // this doesn't seem to do anything??
//..//
}
});
I guess my problem is that i don't know how to acquire event that moves selection to other cell/row.
Here's an image to represent the problem which i'm facing. The item that has only blue border was selected using arrow keys. I'd like to select an item automatically when user presses arrow keys (Down or Up) without the spacebar.
Image taken from here: https://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/features
Edit1:
I'm using latest version of Vaadin - 8.1.6.
Edit2:
I tried to add couple of listeners to see if i could at least register the movement to the next/previous cell by using arrow up/down but without any luck.
Here's a list of listeners i've tried:
addSelectionListener - only registers selection after spacebar press
or mouse click. Not quite what i'm looking for.
addItemClickListener - only registers selection from mouse click.
addShortcutListener - registers pressed key but it doesn't work with arrows.
Is there any listener that could potentially help me with this issue?
The Grid component has basic keyboard navigation. If you need advanced options, like you have mentioned, for keyboard navigation, I would warmly recommend to test this add-on:
https://vaadin.com/directory/component/gridfastnavigation-add-on
I'm using a javax.swing.JTable to show rows in a database table.
I need to fire two different event for two different cases:
when is selected at least a row (or a click on at least a row is performed).
when a double click on a row is performed.
I already looked for an answer on stack overflow, but I didn't find anything satisfying .
Any idea?
when is selected at least a row (or a click on at least a row is performed).
You should monitor changes to the row selection using the JTables ListSelectionModel via a ListSelectionListener. This will notify you when the selection is changed by the user using the mouse or the keyboard or if the selection is changed programmatically for some reason
See How to Write a List Selection Listener for more details
when a double click on a row is performed
The only way you can detect this is through a use of a MouseListener. Normally, users expect that a left mouse button click will do one action and the right mouse button will do something else.
You will want to use SwingUtilities.isLeftMouseButton or SwingUtilities.isRightMouseButton to determine what the user is actually doing.
See How to Write a Mouse Listener for more details
You can add a mouse listener to the table and capture event over there with mouse event like below
table.addMouseListener
(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
}
if (e.getClickCount() == 1)
{
}
}
}
);
}
and to capture selection event you can use
table.getSelectionModel().addListSelectionListener(...);
In my app I added a filter to the display that handles a KeyDown event.
It works just fine when the app is running on windows, but now that I'm trying to run it on Linux it doesn't detect the keypress event.
Any ideas what can be the problem?
The code is a bit messy so I'm adding only the outline for now.
answerListener = new Listener() {
#Override
public void handleEvent(Event event) {
...
};
Display.getDefault().addFilter(SWT.KeyDown, answerListener);
The app Is a trivia game, when I press a key in the game screen, I should get 4 sec to choose an answer. So the answers button is enabled only if a key is pressed before.
There are 3 main SWT composites on the screen. A group with the answers buttons, A list with text strings, and another group at the left of the screen.
When I press on the list of strings with the mouse and then I press a key, a small text box (you can see at the picture) with my pressed key char pops up, but the answers are still disabled.
The text box appears on the screen for 4 sec and then disappears.
When I click before on any other group, the small window don’t pop up, but the answer group are again disabled.
On Windows none of those symptoms occur. The game works as it should work.
I have the following code for JList. On click for an item in the list it should highlight the selected item. But if I press too fast it wont actually select the next item on list on the first click. How should I solve this?
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
String selectedItem = (String) jl.getSelectedValue();
if(selectedItem == "Page One"){
System.out.print("Page one");
}
}
}
};
jl.addMouseListener(mouseListener);
A MouseListener is in appropriate for the task, instead use a ListSelectionListener
Take a look at How to write a List Selection Listener and How to use lists for more details
On click for an item in the list it should highlight the selected item
This is the default behaviour, so I'm not sure why you are doing this.
But if I press too fast it wont actually select the next item on list on the first click.
Probably because you aren't generating a mouseClicked event. A mouseClicked event is only generated when a mousePressed/mouseReleased event is generated at the same pixel location. Maybe the mouse is moving slightly. Try just adding your code to mousePressed.
but i only want mouse click, even if the user using the arrow key to change it should not happen
That is a terrible UI. The user should control whether they want to use the mouse or keyboard. Advanced users will use the keyboard and beginners will use the mouse.
I'm making a game as an android app in which the user clicks on buttons to change its color and so on...
One thing I'm trying to implement is to make some initial moves when the app is started by randomly performing clicks on various buttons. However, I'm having a real hard time trying to figure out how to randomly select some number of buttons and do its performClick() method. Does anyone have any ideas?
Thank you
Billy
Place your buttons in array, generete random number, so that number would be an button-array index.
What Mighter said above should work. But it sounds like the code would be cleaner and more MVC-like if you separate your view code(button handler) from controller(the logic to alter game state), and directly call your controller instead of doing performClick(), in other words:
Move the "change color" logic inside
each button click handler into a
method alterState(int actionId);
Call alterState() within each
button's click handler
When the app starts, call
alterState(new Random().nextInt() %
NUM_ACTIONS) in a loop to perform
your random moves.
Very Simple way to select Radio Button Randomly: suppose 3 radio buttons are there
int a = new Random().nextInt(3);
if(a == 0)
{
idAccountOption.click();
//(idAccountOption)-id of radio button on application
}
else if(a == 1)
{
idPremisesOption.click();
}
else if(a == 2)
{
idRouteOption.click();
}