I have a Java program with a model and a GUI. On my Frame (that implements Observer) I have put a jcombobox with a list of registration from my model (that extends Observable).
When I click on a button add registration the list in my model changed. And than I do
setChanged();
notifyObservers();
In my update method I want to make change the values of the jcombobox. I tried with a repaint() or something like that, but my combobox doesn't change. I am sure I go to my update method, but I don't know how I have to change the jcombobox.
Can someone help
Your update() implementation should obtain a reference to the combo's model and either set the selected item or add a new item, as warranted. A PropertyChangeEvent, illustrated here, may be an alternative, as it can include both old and new values.
Related
I would like to make a custom component that is much like a JList, except there is a little "x" button on the right side of each cell that removes that cell from the list (and triggers an event). I know that you would have to extend JList, but looking at the code for JList I have no idea where to go from there. For reference, I would like the list to be like on the macOS Messages app (except the "x" button can always be visible, not just when the mouse is over the cell).
I would like to make a custom component
I suggest you do that by extending JPanel and adding real components to your panel. Then you can actually add JButton with the "x" that can respond to the mouse event.
A JList does not display real components, only rendered images of the component and therefore is does not respond to events if you try to click on the "x".
The other option is to use a JTable. A JTable does allow you to display values in a column format. In this case it does support the concept of editors, which would allow you to add a button to a column. For example check out Table Button Column.
I would like to know if there is a way to detect if changes in the selection of a item in a swing JCombobox is done by a user (actively) or is causes by repopulating the Jcombobox.
I have to dynamically repopulate the items of the combobox based on other selection, this also invokes the actionPerformed event
so actionPerformed is invoked by:
selection changed by user
repopulating the jcombobox items.
how to tell the difference?
Thanks of helping !
No, not really.
A possible solution is to disable event notification while the combo box is updated. This can be done in (at least) one of two ways...
Firstly, you could physically remove the listener from the combo box, if you have a reference to it.
Secondly, you set a boolean flag, which when true, the listener would ignore the event.
For example...
I'm fairly new to Java, and have exhausted my Google'ing for this problem with selection on a JList component. I have a list of strings that dynamically change when the user clicks various JButtons. My JList selection mode is set to SINGLE_SELECTION, and the ListSelectionModel is registered to a custom ListSelectionChangeHandler that implements ListSelectionListener.
My problem is that every time the JList model's contents get modified (by clicking a JButton), the ListSelectionChangeHandler gets called and a NullPointerException occurs - e.g. The user has an item selected in the list, clicks a button, the list contents change, and the listener gets called. I want the ListSelectionListener to only perform some action when a MouseClick fires the event. How can I prevent my listener from firing when the model data gets modified?
Relevant Code:
this.suggestionsList = new JList();
this.suggestionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel model = this.suggestionsList.getSelectionModel();
model.addListSelectionListener(new ListSelectionChangeHandler());
class ListSelectionChangeHandler implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent arg0) {
Object selectedValue = suggestionsList.getSelectedValue();
// Perform action on selectedValue
// Enable/Disable components as needed
}
}
Thanks for the help!
The easiest way, of which I can think, is to make a switcher - boolean variable, that will be checked every time in your listener before it starts to really do something. Then you can start all of you modifications with switching this variable off. In case of multithreading, you may need some synchronization here.
I have a JComboBox named "jComboBox18" and a JTextArea "jTextArea11". Now I want that whenever a item is selected from the "jComboBox18" combo box its corresponding description is shown in the "jTextArea11" textarea.
I have added the appropriate listener to the JComboBox But the JTextArea is not showing any text. The code that I have written is as follows:
private void jComboBox18ItemStateChanged(java.awt.event.ItemEvent evt) {
Object item = jComboBox18.getSelectedItem();
if(item != null) {
ems.logic.Process selectedProcess = (ems.logic.Process)item;
jTextArea11.setText(selectedProcess.getProcessDescription());
jTextArea11.updateUI();
jTextArea11.revalidate();
jTextArea11.validate();
}
}
=====================EDITED===========================================
The method is being called for sure. I am changing the state of one more combobox
which is also being written in this method and its state changes successfully whenever item is selected from the "jComboBox18"
I think That should work. In fact, you should only need the setText() call. My guess is that you're function isn't getting called for some reason. Put a break point in your code and make sure it's getting called.
In the code shown your method is named as jComboBox18ItemStateChanged. Are you sure this method is being called. The ItemListener for a JComboBox should implement the interface ItemListener which declares that the subclasses should implement the below method.
void itemStateChanged(ItemEvent e);
How are you adding an instance of ItemListener to your JComboBox ?
EDIT:
After reading your edit and comments one another possiblity that I can think of is that:
you have a listener that is triggered when the textarea is updated and probably itis undoing the changes done in JComboBox listener.
I have a Java program where I have a JMenu with an arbitrary number of items (in this case, there is one menu item for each dynamic library currently loaded in a different program). I'm running a loop to add JCheckBoxMenuItem s to the menu, since I don't know how many there will be.
How can I set up an action listener for these menu items that is aware of which option called it? Specifically, I want to run the same function but with a different set or parameters for each of the menu items (and a different function again depending on whether the check is being toggled or detoggled).
Could someone point me in the right direction?
While event.getSource() will definitely let you know which particular button the event came from it has the side effect of needing to track the generated buttons or snooping into the button. Also you may want to present a different name of the library to the user (possibly including the version information) than is used to identify the library. Using the "ActionCommand" property of the button may provide a way to separate those issues. So you will need to alter code in the generation of the checkbox menu items and in the listener.
ActionListener actionListener = ... // whatever object holds the method, possibly this
String[] libraries = ... // however you get your library names
JMenu parentMenu = ... // the menu you are adding them to
for (String s : libraries) {
// prettyName is a method to make a pretty name, perhaps trimming off
// the leading path
JCheckBoxMenuItem child = new JCheckBoxMenuItem(prettyName(s), true);
child.setActionCommand(s);
parentMenu.acc(child);
}
The action handler code would be...
public void actionPerformed(ActionEvent evt) {
// the 'current' selection state, i.e. what it is going to be after the event
boolean selected = ((JCheckBoxMenuItem)evt.getSource()).isSelected();
String library = evt.getActionCommand();
... process based on library and state here...
}
Definitely read over this: http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html
In short, add ActionListener to the menuItems. In the actionPerformed method, use event.getSource(). You can add the SAME ActionListener to all your menu items if you want.
event.getSource() should do it.
When you build the menu you can pass the Action object to the JCheckBoxMenuItem configured with whatever options you need for given action (you can also push there the reference to the check box to check the state). This way you will not have to do any kind of processing when the action is actually performed, because the correct action will be invoked.
The clean way to do it is to create a different ActionListener for each. EventObject.getSource is fugly.