Do something via a JButton with an Element of a JComboBox
I need to find a resolution for connecting a JComboBox
with a JButton
. Means do "Button" with selected Item in JComboBox
.
I created a Controller for that
public class DeleteButtonController implements ActionListener{ private OceanGui view; private OceanInterface model; private JComboBox list;
public DeleteButtonController(OceanGui view, Ocean model, JComboBox list) {
this.view = view;
this.model = model;
this.list = list;
}
@Override
public void actionPerformed(ActionEvent arg0) {
OceanObject obj = (OceanObject) list.getSelectedItem();
int index = model.getIndexOfClosestOceanObject(obj.getPosition()[0], obj.getPosition()[1]);
model.delOceanObject(index);
}
}
In my GUI I did that:
this.buttonArray[1] = new JButton(this.buttonCaptions[1]);
this.buttonArray[1].addActionListener(new DeleteButtonController(this, model, objects));
panel.add(this.buttonArray[1]);
And I earn a exception for that:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to infpp.oceanlife.model.OceanObject at infpp.oceanlife.controller.DeleteButtonController.actionPerformed(DeleteButtonController.java:25) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at 开发者_JAVA技巧 java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
In line 25 it says
OceanObject obj = (OceanObject) list.getSelectedItem();
Give the model a public method deleteSelectedItem()
and have the button's ActionListener call that method. The button/action listener don't need to know how the item is deleted; all it needs to know and do is to send a message to the model to do this.
Line 25 and the exception tells you everything you need to know. You are pulling items out of the JComboBox
and casting to OceanObject
, but the exception tells you they are String
s.
You must be populating your JComboBox
's model with Strings. Either populate them with OceanObject
s and then your cast will work, or pull out the String object from the JComboBox and somehow get back your OceanObject
.
Vector<OceanItem> oceanItems = new Vector<OceanItem>();
oceanItems.add(new OceanItem(...));
oceanItems.add(new OceanItem(...));
oceanItems.add(new OceanItem(...));
JComboBox box = new JComboBox(oceanItems);
What do you mean by "connect a JComboBox with a JButton. do button with selected item"? Do you mean that you want the user to be able to select an item in the combo box, then press the button, and you are asking how to perform some action upon the selected item in the combo box?
If yes, just add an ActionListener to the button via addActionListener(), and in that action listener get the item that is selected.
comboBox = new JComboBox();
container.add(comboBox);
button = new JButton("Button");
container.add(button);
button.addActionListener(myButtonListener);
...
myButtonListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Selected item is: " + comboBox.getSelectedItem());
OceanObject myObject = (OceanObject)comboBox.getSelectedItem();
// do something else with myObject here
}
};
If I understand what you are asking correctly, I think that is exactly what you want. According to what you've said, it doesn't sound like you need to mess with any of the other stuff you're getting into there. It's as simple as making and adding your combo box and button, and adding an action listener to the button that does something to the combo box in its actionPerformed().
Ok, based on your comment I think I see now where your problem lies. Still, I stand by my comment that you don't need that extra overhead. JComboBox has a getSelectedItem(). I have modified my code above so that, in the spot where your logic for this goes, I have replaced my comment about "put your logic here" and put an output there instead so that you can see this used.
As you can see, I don't even have to keep track of what was selected as the changes were made.
(edit)
In response to your edited question: The exception is being thrown because you are not adding OceanObject
s to the ComboBox, you are adding String
s. So this problem goes back to the place in your code where you are adding things to your ComboBox. What does that look like?
Thanks for your help everyone. I just buildet a solution with my new know how. I hope someone else will find some help with it.
public class DeleteButtonController implements ActionListener {
private OceanGui view;
public DeleteButtonController(OceanGui view) {
this.view = view;
}
@Override
public void actionPerformed(ActionEvent arg0) {
try {
OceanObject obj = (OceanObject) view.getObjects().getSelectedItem();
int index = view.getModel().getIndexOfClosestOceanObject(
obj.getPosition()[0], obj.getPosition()[1]);
view.getModel().delOceanObject(index);
view.getObjects().removeAllItems();
Iterator<OceanObject> iterator = view.getModel().getOceanObjects()
.iterator();
while (iterator.hasNext()) {
view.getObjects().addItem(iterator.next());
}
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, "No more OceanObjects in Ocean.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
精彩评论