Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm working through making a simple word processor. Actually found a really helpful tutorial that is walking me through it. I'll still have to do quite a bit of work but it's giving me a solid foundation to start on. As I'm writing the display class, I'm getting an error that the guy in the video didn't get. When I add the Action listeners for the buttons and sliders, it gives me this error: The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the
arguments (Display)

Here is the code I've done:

Java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextPane;

public class Display extends JPanel {
	private JTextPane textArea;
	private JButton saveButton;
	private JComboBox colorCombo;
	private JComboBox fontCombo;
	private JLabel processorLabel;
	private JSlider fontSize;

	// Create some arrays
	String[] colorItems = { "Red", "Blue", "Green", "Purple", "Orange", "Black" };
	String[] fontItems = { "Monospaced", "Serif", "Sans Serif" };

	public Display() {
		init();
	}

	public void init() {
		// Construct Components
		textArea = new JTextPane();
		saveButton = new JButton("Save");
		colorCombo = new JComboBox(colorItems);
		fontCombo = new JComboBox(fontItems);
		processorLabel = new JLabel("Charles' Word Processor");
		fontSize = new JSlider(10, 30);

		// Work with slider
		fontSize.setOrientation(JSlider.HORIZONTAL);
		fontSize.setMinorTickSpacing(1);
		fontSize.setMajorTickSpacing(5);
		fontSize.setPaintTicks(true);
		fontSize.setPaintLabels(true);

		// Make the text area look presentable
		textArea.setBackground(Color.LIGHT_GRAY);
		// textArea.setForeground(color);

		// Adjust size and layout
		setPreferredSize(new Dimension(817, 473));
		setLayout(null);

		// Add components
		add(textArea);
		add(saveButton);
		add(colorCombo);
		add(fontCombo);
		add(processorLabel);
		add(fontSize);

		// set boundaries
		textArea.setBounds(10, 10, 650, 450);
		saveButton.setBounds(670, 315, 140, 35);
		colorCombo.setBounds(670, 205, 140, 35);
		fontCombo.setBounds(670, 150, 140, 35);
		processorLabel.setBounds(670, 20, 140, 35);
		fontSize.setBounds(670, 95, 140, 49);

		// add action listeners
		saveButton.addActionListener(this);
		colorCombo.addActionListener(this);
		fontCombo.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {

	}
}


Any help would be greatly appreciated. Thank you guys!

What I have tried:

I've tried changing casting "this" to the action listener. Didn't work
Posted
Updated 18-Mar-16 16:14pm
v2

1 solution

Okay so the problem was I should have typed at the top of the class:
public class Display extends JPanel implements ActionListener{

That's why it wasn't working when I made the action listeners later in the class.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900