Reputation: 5
it seems as though i have found a new problem in my code. my code has no (visible) errors, but i still get no frame. Please help
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class hello{
//Int's and things
static JButton Play = new JButton("<PLAY>");
static JFrame pane = new JFrame("CIrCUT 0.0.2");
static JLabel Title = new JLabel("CIrCUT");
static JLabel none = new JLabel(" ");
static JPanel panel = new JPanel(new GridLayout(10,10,10,10));
static JButton Options = new JButton("<OPTIONS>");
static JPanel panel2 = new JPanel(new GridLayout(10,10,10,10));
static String b[] = new String[3];
static int panelLoct =1;
JComboBox optionlist = new JComboBox();
void initialize(){
b[0] = "High";
b[1] = "Medium";
b[2] = "Low";
//title
pane.setTitle("CIrCUT 0.0.2");
//drop down
optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "High", "Medium", "Low"}));
optionlist.setSelectedIndex(3);
optionlist.addActionListener((ActionListener) this);
//other pane-related things
if(panelLoct==1){
pane.setLayout(new GridLayout(10,10));
panel.setMaximumSize(new Dimension(500,500));
pane.setSize(500,500);
pane.setMaximumSize(new Dimension(500,500));
panel.add(Title);
panel.add(none);
panel.add(Play);
panel.add(Options);
panel2.add(optionlist);
Play.setSize(new Dimension(500,450));
pane.setLocation(500,50);
pane.setBackground(Color.lightGray);
pane.setContentPane(panel);
pane.pack();
pane.setMinimumSize(new Dimension(500,500));
pane.setContentPane(panel);
OptionButtonHandler cbHandler = new OptionButtonHandler();
Options.addActionListener(cbHandler);
pane.setVisible(true);
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
private static class OptionButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e){
pane.remove(panel);
pane.add(panel2);
}
}
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
cb.getSelectedItem();
}
public static void main(String args[])
{
hello a = new hello();
a.initialize();
}
}
any help at all would be much appreciated. here is the error i get when i try to run it
Exception in thread "main" java.lang.ClassCastException: hello cannot be cast to java.awt.event.ActionListener
at hello.initialize(hello.java:39)
at hello.main(hello.java:83)
It is probably a novice mistake, but could find nothing on how to solve it.
Upvotes: 0
Views: 8946
Reputation: 10139
You have to implement ActionListener.
public class Hello implements ActionListener{
}
Upvotes: 0
Reputation: 33187
optionlist.addActionListener((ActionListener) this);
Just because you will something doesn't mean it is possible. In this case, hello
is not an ActionListener
, so this will fail.
If you find yourself casting, you likely have a problem.
Upvotes: 2
Reputation: 1500873
Your program does have a visible error, precisely where it's failing:
optionlist.addActionListener((ActionListener) this);
this
is a reference to an instance of hello
. The hello
class doesn't implement ActionListener
- so how would you expect that cast to succeed?
The only implementation of ActionListener
you've given is OptionButtonHandler
. Perhaps you meant:
optionlist.addActionListener(new OptionButtonHandler());
?
Upvotes: 3