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.
Related
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(...);
I am attempting to add drag-and-drop functionality to my application, whereby the originator of the drag event is a JTable. I am currently using the built-in drag support JTable offers by calling setDragEnabled(true).
The problem I'm facing is that in order to commence a drag operation, one has to first click on a row of the table, and then release the mouse; it is only the second mouse press (and all subsequent mouse presses) that generate drag events. This occurs even if the JTable loses focus - i.e. Once the first left-click operation has been performed, drag-and-drop works perfectly until I swap in a new TableModel. When new model has been installed one needs to perform a left-click on the table before drags start working again.
Reading the API documentation for setDragEnabled(boolean) the implication is that this is the L&F's responsibility and hence there may not be anything I can do to solve this. Does anyone have any suggestions? I am using the Alloy L&F but would be reluctant to change it.
I discovered a hacky solution, which was to add a MouseListener to the JTable and hook into the TransferHandler's exportAsDrag method when the mouse is pressed:
final JTable actionTbl = new JTable();
actionTbl.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent evt) {
// Need to explicitly start a drag operation when the mouse is pressed.
// Otherwise drags are only started *after* the user has clicked once
// on the JTable (this could be down to the L&F not doing the right thing).
actionTbl.getTransferHandler().exportAsDrag(actionTbl, evt, TransferHandler.COPY);
}
});
Quote from the javadoc of the setDragEnabled method
When automatic drag handling is enabled, most look and feels (including those that subclass BasicLookAndFeel) begin a drag and drop operation whenever the user presses the mouse button over an item (in single selection mode) or a selection (in other selection modes) and then moves the mouse a few pixels.
If I read this correctly, you should get the desired behavior when you use single selection mode.
If you need that behavior combined with multiple selection, you could opt to manually handle the incoming mouse events on the table, and on mouse_down adjust the selection and then delegating the mouse event to the JTable. So in pseudo-code:
protected void processEvent(AWTEvent e) {
if ( isMouseDownEvent( e ) ){
adjustSelection( e );
}
super.processEvent( e );
}
Note: I haven't tested this. It is solely based on what I read in the javadoc, and might have some unwanted side-effects as the JTable itself will still handle the event and react on it
Adamski's solution works really well, but I had to change TransferHandler.COPY in the last line to TransferHandler.MOVE in order for it to work:
final JTable actionTbl = new JTable();
actionTbl.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent evt) {
actionTbl.getTransferHandler().exportAsDrag(actionTbl, evt, TransferHandler.MOVE);
}
});
As you know, without using !getValueIsAdjusting when you select a row in a jtable (by clicking) the selection change event fire twice. It doesn't happen if you select a row using the keyboard arrow. To resolve it, you check if getValueIsAdjusting returns false.
My question is why does the event fire twice if I select a row by clicking it, but not when using the keyboard arrow? And what does getValueIsAdjusting do to resolve it?
As the javadoc which JB Nizet linked to states, getValueIsAdjusting() checks whether a specific event (a change) is part of a chain, if so it will return true. It will only return false when the specified event is the final one in the chain.
In your case, selecting a row by clicking actually fires two events: a mouseDown and mouseUp event and both are sent to your event listener. If you correctly implement getValueIsAdjusting() to return whenever the value is true, you will only act on the final event in the chain, which is the mouseUp event that fires when you let go of the left mouse button.
The Java Tutorials include an example that captures events, you can use that to log the selection events and experiment with it yourself. Remove the return on the event.getValueIsAdjusting() check to log every event that's fired.
String interessen[]= {"aaaaaaa", "bbbbbbbb", "ccccccccc", "ddddddd"};
myList = new JList<>(interessen);
myList.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting())
System.out.println(myList.getSelectedValue());
}
});
The code above shows what getValueIsAdjusting do, without these method an event may be called eg. two times (it depends of event).
Output without getValueIsAdjusting loop after clicking on some element of JList:
aaaaaaa
aaaaaaa
with loop:
aaaaaaa
I have a frame and a JWindow. In my frame I have a textfield, whenever I type something to the field, the window will appear with list of suggestions below the textfield. I used a keylistener to the field.
When I press the enter key on the list of suggestion in the window, the word that I select goes to the field.
Now the problem is that the window still appear, I want the window to disappear whenever I select a word.
Could someone got an idea about this?
Thanks..
Try this:
jWindowInstance.setVisible(false);
I'm assuming you have an OK button on there, in which case you should be able to set the default button on the root pane of the window, e.g.
window.getRootPane().setDefaultButton(okBtn);
Try this:
jWindowInstance.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
jWindowInstance.dispose(); // Release resources
// OR
jWindowInstance.setVisble(false); // Just hide the window so you can reuse it afterwards
}
}
});
You can send the selected item to the textbox, right? I assume that you have used some kind of event listener to do that. At the end of the action method, make the window's visibility to false. Swaranga's way should work.
I have a JList with 5 options in it and when one of the items becomes selected or clicked i want the text area next to it to show a paragraph of text relative to the item clicked. It should do this for each item in the list but I cant seem to find how to do it in the API
How would my program know if an item in the JList was selected so I can work with the data?
Use addListSelectionListener. You can create a subclass (anonymous or not) of ListSelectionListener that does the work you want.
myList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent ev)
{
// handle ev
}
});
You should register a Listener for events on your JList. When the Swing UI fires one off, this Listener class will get the message and react accordingly.