How to turn off auto selection in combobox while navigating in dropdown? - java

the Title states my problem almost completely.
I have some combo box classes which derive from JComboBox, additionally we use the PlasticUI from JGoodies.
My Problem is that when I navigate through the available items in the drop down popup
those items are automatically being selected.
This only happens when I use the navigation keys, hovering with the mouse over the objects is fine.
In my case this is pretty bad because it somehow provokes the lazy-loaded data in the object to be loaded and slow the combo box down immensely.
How can I turn this behavior off?
I tried debugging, but I cannot find a place to set a breakpoint properly, too much magic happening in the background :/
Plzz help :)

You can use the function ActionEvent.getModifiers() to check if the ItemChangeEvent got fired with the keyboard or the mouse.
JCheckBox box = new JCheckBox();
box.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
if (e.getModifiers() == 0) {
System.out.println("keyboard");
} else {
System.out.println("mouse");
}
}
});

Related

How to enable a button when text is entered in a text field in Netbeans

I'm very new at coding Java and Netbeans. So basically, I have a "save" button and three text fields, I want to enable the Button when these three text fields are edited and disable the button when one of them is empty. Also I'm wondering where I should put my codes. Since it's Netbeans I'm only familiar with ActionPerformed methods, there you can set an action when a button is pressed.
If you can keep it simple it would be appreciated!
public project() {
initComponents();
//Here I want the window to appear in the middle of the screen
setLocationRelativeTo(null);
if(txfField1.getText().equals("")){
btnSave.setEnabled(false);
}
else {
btnSave.setEnabled(true);
}
}
I tried with this code on only one of the three text fields and It does not work, the button is always enabled. The button is initially disabled. Additionally I have also tried to put my code below this method:
public class project extends javax.swing.JFrame {
You can use event handlers to change the state of the button. For example, if you have one text field and you want to change the state of the button depending on the data inside the text field, you could use something like
if (!jTextField1.getText().equals("")) {
jButton1.setEnabled(true);
} else {
jButton1.setEnabled(false);
}
and the event handler you can use
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
You can generate this automatically in Netbeans by going to the event tab when you have clicked on a component in the design view.
It seems in your example you have the right idea, however you need to update the button using events such as key pressing, key releasing etc
You can add it in onblur() method of those text boxes.
If it can, you can add a validation with an error message on click of save button, which might be more meaningful.

How do I override JTextPane's selection behavior when Swing D&D is disabled?

Background: I am using custom AWT D&D in my (heavily) customized JTextPane subclass. In other words, I have disabled Swing's D&D with pane.setDragEnabled(false) and I use my own DragSource, DragGestureListener, etc.
The problem: The default selection behavior in JTextPane when Swing's D&D is disabled is as follows:
Select some text using the mouse
Mouse press inside the selection with the intent of starting a drag
Desired: selection is not lost.
Actual: upon mouse press, selection is immediately lost, giving no opportunity for me to start my drag operation since there is now nothing to drag.
I have partially traced this back to BasicTextUI$DragListener, since this is the class that calls the pane's getDragEnabled() method, but BasicTestUI doesn't seems to do much with the text component's selection. So, I'm still not exactly where the selection is being cleared, but I need to find it so I can eliminate the behavior.
I have employed a hack that involves setting a persistent highlight from a carat listener, so even though the selection is lost a highlight will remain that my drag can interact with. I am not happy with this and it has other side effects.
Many thanks for any pointers.
After many more hours of reviewing JDK source, I've determined that the selection behavior is controlled by the Caret and not anything in the text component or UI hierarchy.
A mildly customized Caret seems to do the trick. Note that if you don't override mouseDragged(), the custom drag will still work but the selection will typically be altered in the pane after the drag starts, making the user think they're only dragging part of the text they selected.
textPane.setCaret(new DefaultCaret() {
#Override
public void mousePressed(MouseEvent evt) {
int pos = textPane.viewToModel(evt.getPoint());
if (pos > textPane.getSelectionStart() && pos < textPane.getSelectionEnd()) {
return;
}
super.mousePressed(evt);
}
#Override
public void mouseDragged(MouseEvent e) {
if (dragItem != null) return;
super.mouseDragged(e);
}
});

Mouse event buggy

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.

Undesired drag and drop behaviour with JTable

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);
}
});

How to hide the JWindow when I press Enter key?

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.

Categories

Resources