Is it possible to disable a JComboBox when a JTextField is edited? - java

I'm working on a form and I'd like to have a drop-down box where you select a person. It brings up their stored information in the text-fields below, but as soon as you edit one of the text-fields it disables the drop-down box until you save or cancel the changes. The purpose of this is to prevent the user from editing something, thinking it's saved, and then changing to a different person and losing their changes.

Add a DocumentListener to all your text fields. Whenever any data is changed you disable the combo box. When the data is saved you enable the combo box.
See How to Write a DocumentListener for more information and examples.
A better approach might be to popup a JDialog with the data to be changed.

Dynamically disabling combo boxes doesn't seem like a common practice. Perhaps instead you could indicate to the user when something is saved, and if the user attempts to switch people after info has been inputted, you could notify them and ask if they want to continue and lose the data. Is it not possible that some users will enter data, try to use a disabled combo box, and not knowing why it is disabled, they will think you program is broken?

Related

Disabling JavaFX ComboBox input

How do I disable input on my editable Combobox? (Well, actually JFoenix JFXCombobox but it's basically the same apart from it's appearance)
setEditable(false) would disable keyboard input on the Combobox but the list would still appear
setDisabled(true) would disable whole Combobox but I want user to be able to focus Combobox so he can copy it's contents if necessery.
Why do I want it like that? In my forms user must first click edit button to be able to change stuff.
Basically, the method ComboBox.setEditable adds/removes an Editor to the ComboBox which can be retrieved via ComboBox.getEditor()
To keep the TextField (to copy from) but disable user-input, simply set the editable flag on the underlying TextField:
private ComboBox<String> myComboBox;
[...]
myComboBox.setEditable(true);
myComboBox.getEditor().setEditable(false);
EDIT:
As #jewelsea said in a comment below, you can hide the list as soon as the user requests to open it:
myComboBox.setOnShown(event -> comboBox.hide());
I think it would be "cleaner" to disable the button which opens the dropdown but unfortunately I have not yet found a way to do that.

Making a editable form in java netbeans

How can I design a editable form as in detail of a person by clicking edit button and edit the respective fields of person's details and save it on the same place where they are shown.
eg.
Full Name : JTextField1
Address: JTextField2
and so on...
Now What I want is to save those values on respective places and when I want to edit them then show editable textfield with same values displayed.
Are there any API's or plugins to do it or should I just have to do as setVisible(true/false) for necessary action to perform??
If you are using JavaFX, you can use setEditable(boolean), which will disable or enable the field for editing. You can also use setDisabled(boolean) to disable the field for anything other than viewing.
If you are using Swing/AWT, you can use setEditable(boolean) as well as setEnabled(boolean) to do the same as JavaFX.
Since you are using Swing/Awt, use setEditable to disable or enable the fields when you press the Edit button. :)

GWT- PopupPanel with search?

I am looking for a way to create a popup dialog box when a user double clicks a textinput field that will contain a scroll-able list (from database table) where the user can select a field, hit ok, and have it placed into the textbox when popup closes.
The other major requirement is to have a filter/ or search field in the popup to aid the user in finding the correct option to select from quicker.
What is the best way to implement this?
Modification to gwt's popup panel? maybe a JOptionPane? are there any simple solutions already designed for free commercial use?
You could implement this with a com.google.gwt.user.client.ui.PopupPanel. You can make a PopupPanel that contains a ListBox with your data from the database, along with a OK button. When a user selects a value and hits OK, you should utilize an EventBus along with a custom Event that will pass the value to the field on the page. The page will have an event handler that will catch the event and put it into the field.
Another option is to use a com.google.gwt.user.client.ui.SuggestBox. It is a box that autocompletes / suggests values as you type, kind of like the Youtube search bar.
I can offer more resources to help you accomplish this, if you'd like.

jTextField autocomplete from database

I want a jTextField that shows suggestions in a popup as the user types (like google suggests). I want suggestions data to be fetched from a database table. I have been looking at SwingX and GlazedLists but I think they provide jComboBox autocomplete, and jTextField does not show a popup in these libraries.
I want to monitor user input and re-query the database after specific intervals.
Thanks.
I would keep looking into SwingX or GlazedLists, to avoid re-inventing the wheel. But if you are doing it yourself:
Add a KeyListener to the field and show a popup just below the text field whenever the user types. The popup could just be a menu with possible items or maybe even a JList. Make sure your database query can keep up with the typing or put the work on a separate thread.

How do I fire an action when the user leaves a JTextBox?

I've got a JTextField, and I'd like the system to do some processing on what the user typed whenever the user leaves the text field. The ActionListener that you can add to just the JTextField only fires when the user presses enter, however.
I'd like the processing routine to run whenever the user leaves the text box by any means - tabs, clicks out of it, presses enter, etc. (The processing in question is to save the text the user typed to the appropriate data object, nothing fancy.)
My google-fu has failed on this one: I'm confident that it's possible, I just can't see how.
Add a FocusListener.
It's worth noting that this is a relatively low-level listener. On JComboBox it wont work unless you find the text field (and perhaps button) that the particular PL&F inserts. Swing is a bit odd that way (amongst many other ways).
Although for my money, non-cosmetic changes that happen when focus leaves a field give poor user experience. Much better to do any relevant changes on every change with a listener on the text field's document.
If you want to edit the text as it is typed then you should use an DocumentFilter.
If you want to validate the text as a complete entity then you can use an InputVerifier.

Categories

Resources