How can I serialize a JComboBox selection? - java

I would like it so that if I pick something on the JComboBox, when I close it it's saved, and so when I re-open the program, whatever was last selected is still selected.

I faced this problem before in one of my GUI applications. What I've done is that I saved the JComboBox selection in a variable using getSelectedItem() function. And when I reopen the program, I set it out again at the same index using setSelectedItem(Object a).
I hope it helps you solve your problem.

Related

Differentiate between typing and pasting in JTextField

i've been looking around and haven't been able to find any solution to this problem: i have a JTextField and i want to do some things when the user paste something in there, i've found this: What event to use when pasting something in a JTextField?
which works ok, except that i want only to do things when the user paste something, not when it writes on the text field, i've though of saving the previous value of it and compare it with the new, and if it was empty and now is not, do things, but this won't work since it will enter in that condition when the user types the first letter in the text field.
If anyone knows how to do it whit the documentListener or whit any other listener it would be of grate help.
Update: since various people has asked, the reason i want to do this is because the text will come from a bar code reader or some similar device.
except that i want only to do things when the user paste something
Why should pasted text be treated any different than typed text? Sounds like a design issue. If you specify a better reason/requirement for doiong this we might be able to come up with a better solution.
i want to do some things when the user paste something in there
You might be able to override the paste() method of the JTextField. Just override the method to invoke super.paste() and then add your custom code.
how to do it whit the documentListener
Maybe you would consider a "paste" to mean more than one character is added at a time. In which case you just test the length of the String that is added to the Document.
I was able to fix my problem by configuring my bar code scanner and making it send a "new line" after each reading, and executing my code every time this happens with the actionPerformed of the JTextField. Thanks to everybody who tried to help.

Transfer Items from One JComboBox to another JComboBox

Hi I'm trying to transfer one object from one combobox to another and vice versa.
To accomplish this,
I use actionListeners or ItemListeners, to no luck that they dont answer my problems, or maybe there's just something wrong in my implementation.
Assuming we have to comboboxes, combobox1 and combobox2.
Basically,
1. I first add the selected item to another combobox (combobox2)
2. I remove the selected item on the first combobox (combobox1)
When trying to debug this, i found out that everytime i am on the step of removing items, the listener of the other combobox fires, which does the same steps as above. This results into a loop, that just deletes the item, and places it back to the original combobox.
When using the ItemListener, with the proper if conditions of being selected or not, it throws a bigger error. Guys please help me..
*on edit mode/currently making an SSCE
Found this, as suggested by sir mKorbel.
It did the trick, setting the model via setModel(DefaultComboBoxModel model) method doesnt trigger the ActionListener when it tries to add the contents of the model passed, versus the addItem(Object obj) method that fires the ActionListener causing the havoc that i described on my question above.
Thanks guys, and i learned about a new thing called DefaultComboBoxModels!
jComboBox12.removeAllItems();
for (int t = 0; t < jComboBox11.getItemCount(); t++)
{
jComboBox12.addItem(jComboBox11.getItemAt(t));
}

Java Swing Buttons

So I have three buttons: add, edit and save. I am using ActionListener and getActionCommand to determine which button was pressed.
If I press add, then save, it has to add a new person in my table. If I press edit, then save, it has to edit that person in the table.
Is there any way to determine which button was pressed before save so that I know which way I go in Save button?
Yes. Store which button was pressed in an instance variable in your class (I don't mean in your listener class).
by using getSource() we can do it
and add some conditions like flag=1 in add button, flag=2 in edit button
if flag=1 then add new record to a table
if flag=2 then edit existing record.
A better way than using ActionListeners directly is to use Actions:
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
I always felt like getSource was a big hack, don't know why you would ever need to use it.

Saving value from JComboBox in custom TreeCellEditor

I've got a JTree with a custom TreeModel and a custom TreeCellEditor displaying (for now) a JComboBox through the getTreeCellEditorComponent() override. The tree is displayed properly, with the nodes going into edit mode and displaying the JComboBox when I click on them.
Whenever I edit a node, changing the value from the dropdown, and then proceed to select another node from the three, I can see the TreeCellEditor's cancelCellEditing() being triggered.
What's the "correct" way to stop editing in stead of cancelling it, thus (hopefully?) making sure the model's valueForPathChanged() get's triggered?
After further investigation in the source code I found the answer inside the JTree class:
Setting JTree#setInvokesStopCellEditing(true) means editing is stopped in stead of cancelled whenever I change focus from one node to another. This also means my TreeModel#valueForPathChanged() gets called.

JTable: double-click should keep prior selection

I have a JTable where I can select one or more cells. I also want to react on double-click for doing some extra action for the selected cells. But the problem is, when the user double-clicks, the selection changes to the clicked cell. But I want to keep the prior selection on double-click, so I can handle the double-click for all selected cells.
EDIT:
Related to this question:
Java : ignore single click on double click?
But I hope, there is a better/easier solution for my case.
The problem is, that on the first click the first event goes out. A bit later the second click might come or not. So the first click event does know nothing. As in the proposed solution a timer might do.
What also might do is on the first click to select nothing, but invoke a special selection event a bit later.
SwingUtilities.invokeLater(myRunnable);
and on handling the double click/myRunnable the true selection. Timing might be unavoidable though.
you can use setClickCountToStart() for XxxCellEditor, I don't know something about your JTable

Categories