Use separate ActionListeners for the ComboBox and your buttons. Right now your main problem is you are casting here expecting a JComboBox: JComboBox cb = (JComboBox)e.getSource() But this will fail when the button is clicked because a JButton is not a JComboBox Doing this cast would be fine if it was in an ActionListener only dealing with JComboBoxes.
Use separate ActionListeners for the ComboBox and your buttons. Right now your main problem is you are casting here expecting a JComboBox: JComboBox cb = (JComboBox)e.getSource(); But this will fail when the button is clicked because a JButton is not a JComboBox. Doing this cast would be fine if it was in an ActionListener only dealing with JComboBoxes.
– kachilous Aug 22 at 19:02 1 better would be ItemListener +1 – mKorbel Aug 22 at 19:07 @kachilous, yes sort of. Creating separate ActionListeners will force you to have separate actionPerformed methods. – jzd Aug 22 at 20:11 @jzd: Ok thanks.
I've updated the action listener for the combo box (see original post), but for some reason the selection of the color is not being saved into g, do I have to pass g back into my drawPoint() method – kachilous Aug 22 at 20:27.
1) Don't use the getGraphics() method to do painting. It may appear to work, but try minimizing and then restoring the frame and the painting will disappear. Check out Custom Painting Approaches for ideas on how to do this.2) Don't use "==" to compare String values.
Use: colorName. Equals("Red"); Actually a better solution is to store a custom `ColorItem' object in the combo box. This item would store both the String display text and the Color object.
Then there is no need for multiple if statements in the ActionListener. Here is a simple example that uses this approach: import java.awt. *; import java.awt.event.
*; import java.util. *; import javax.swing. *; import javax.swing.plaf.basic.
*; public class ComboBoxItem extends JFrame implements ActionListener { public ComboBoxItem() { Vector model = new Vector(); model. AddElement( new Item(1, "car" ) ); model. AddElement( new Item(2, "plane" ) ); model.
AddElement( new Item(4, "boat" ) ); model. AddElement( new Item(3, "train" ) ); model. AddElement( new Item(5, "boat" ) ); JComboBox comboBox; // Easiest approach is to just override toString() method // of the Item class comboBox = new JComboBox( model ); comboBox.
SetSelectedIndex(-1); comboBox. AddActionListener( this ); // comboBox. PutClientProperty("JComboBox.
IsTableCellEditor", Boolean. TRUE); getContentPane(). Add(comboBox, BorderLayout.
NORTH ); // Most flexible approach is to create a custom render // to diplay the Item data // Note this approach will break keyboard navigation if you don't // implement a default toString() method. ComboBox = new JComboBox( model ); comboBox. SetSelectedIndex(-1); comboBox.
SetRenderer( new ItemRenderer() ); comboBox. AddActionListener( this ); getContentPane(). Add(comboBox, BorderLayout.
SOUTH ); } public void actionPerformed(ActionEvent e) { JComboBox comboBox = (JComboBox)e.getSource(); Item item = (Item)comboBox.getSelectedItem(); System.out. Println( item.getId() + " : " + item.getDescription() ); } class ItemRenderer extends BasicComboBoxRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super. GetListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value!
= null) { Item item = (Item)value; setText( item.getDescription().toUpperCase() ); } /* if (index == -1) { Item item = (Item)value; setText( "" + item.getId() ); } */ return this; } } class Item { private int id; private String description; public Item(int id, String description) { this. Id = id; this. Description = description; } public int getId() { return id; } public String getDescription() { return description; } public String toString() { return description; } } public static void main(String args) { JFrame frame = new ComboBoxItem(); frame.
SetDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame. SetVisible( true ); } }.
Thanks for the suggesstions – kachilous Aug 22 at 20:28.
I want to implement a JComboBox so that the user can specify what color they want the plot to be colored when drawn. I have created different color objects to be used for this. In my actionPerformed method, I also have code that handles the JButton events that start, stop and erase the simulation.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.