Question

I'm creating a program for my Java class and I'm having a hard time implementing a actionListener in conjunction to my main class.

This is an example of my main class, I am using other classes to create the components for the tabs. Where I'm running into trouble is when I try to implement action listeners in the other class. I keep running into errors regarding abstract classes. This is probably a simple solution, but I'm still fairly new to programming.

'
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import javax.swing.plaf.ColorUIResource;

public class TaikunStudyResource extends JFrame {


    private static final int FRAME_WIDTH = 700;
    private static final int FRAME_HIEGHT = 500;
    private JPanel searchTab,addTab, grammerTab,testTab,homeTab; 

    public static void main(String[] args)throws UnsupportedOperationException {
        TaikunStudyResource tsr = new TaikunStudyResource();
        tsr.setVisible(true);
    }
    public TaikunStudyResource(){
        //set basic features
        setTitle("Taikun-Japanese Study Resource!");
        setSize(FRAME_WIDTH,FRAME_HIEGHT);
        setResizable(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBackground(new Color(226,199,255));
        addComponents(getContentPane());

    }
    public void addComponents(Container contentPane){
        //add overlaying panel
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        contentPane.add(topPanel);

        //create tabs
        contentPane.add(createTabs());

    }
    public JTabbedPane createTabs(){
        //UIManager.put("TabbedPane.contentAreaColor",ColorUIResource.getHSBColor(153,153,255));
        //create tabs
        JTabbedPane tabs = new JTabbedPane();
        SearchPanel sp = new SearchPanel();
        tabs.addTab("Search", sp);
        addPanel ap = new addPanel();
        tabs.addTab("Add",ap);


        return tabs;
    }

}
'

Here is a example of my my addPanel class '

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.List;
/**
 *
 * @author tyler.stanley.4937
 */
public abstract class addPanel extends JPanel{
    //variables
private JPanel mainPanel, eastPanel,westPanel, checkboxPanel, radioPanel, typePanel, adjectivePanel, verbPanel, kanjiPanel;
private JLabel kanaKanjiLabel, romanjiLabel;
private JTextField kanaKanjiField, romanjiField,exampleInput, defInput, tagInput;
private JButton radicalButton, addDefButton, addExampleButton, addButton, addTagButton
        ,clearexButton,clearDefButton,clearTagButton;
private ButtonGroup commonGroup;
private JRadioButton[] commonButton;
private String[] commonTypeArray = {"very-common", "common", "Uncommon", "Out-dated"};
private JCheckBox[] typeCheckBox;
private String[] typeofWordArray = {"Noun", "Pronoun", "Verb","Adjective","Idiom"};
private JComboBox adjectiveTypeCombo;
private String[] adjectivetypeArray ={"Na","i"};
private JComboBox verbTypeCombo;
//private String[] verbTypetext = {"Suru","Kuru","da","desu","iku","-masu"};//these may not make it
private String[] verbTypeArray ={"-u","-ku","-gu","-su","-tsu","-nu","-bu","-mu","-ru"};
private JTextArea definitionArea, exampleArea, tagArea;
private List<String>definitionList, exampleList, tagList, radicalList,typeList;
private String kanjiKana,romanji;
private ActionListener a;

public static void main(String[] arg)throws UnsupportedOperationException{
}
public addPanel(){
    setLayout(new BorderLayout());
    //setBackground(Color.blue);
    addComponents();
}
public void addComponents(){
    fillSouthPanel();
    fillEastPanel();
}

}
public void fillEastPanel(){
eastPanel = new JPanel(new BorderLayout());
//add definition pane
JPanel definitionPanel = new JPanel(new FlowLayout());
definitionPanel.setBorder(BorderFactory.createTitledBorder("Defintion"));
definitionArea = new JTextArea();
definitionArea.setColumns(22);
definitionArea.setRows(8);
definitionArea.setBorder(BorderFactory.createLineBorder(Color.black));
definitionArea.setEditable(false);
//add scroll pane
JScrollPane scrollPane = new JScrollPane(definitionArea);
scrollPane.setSize(200,135);
definitionPanel.add(scrollPane);

//add input and button
defInput = new JTextField(50);
definitionPanel.add(defInput);
addDefButton = new JButton("Add");
definitionPanel.add(addDefButton);
//addDefButton.addActionListener(al);
clearDefButton = new JButton("Clear");
definitionPanel.add(clearDefButton);
//clearDefButton.addActionListener(al);

//add tags
JPanel tagPanel = new JPanel(new FlowLayout());
tagPanel.setBorder(BorderFactory.createTitledBorder("Tags"));
tagArea = new JTextArea();
tagArea.setColumns(22);
tagArea.setRows(1);
tagArea.setBorder(BorderFactory.createLineBorder(Color.black));
tagArea.setEditable(false);

JScrollPane tagScrollPane = new JScrollPane(tagArea);
tagScrollPane.setSize(200,133);
tagPanel.add(tagScrollPane);
tagInput = new JTextField(22);
tagPanel.add(tagInput);
addTagButton = new JButton("Add Tag");
clearTagButton = new JButton("Clear Tag");
tagPanel.add(addTagButton);
tagPanel.add(clearTagButton);
//clearTagButton.addActionListener(al);
//addTagButton.addActionListener(al);

//examples
JPanel examplePanel = new JPanel(new FlowLayout());
examplePanel.setBorder(BorderFactory.createTitledBorder("example"));
exampleArea = new JTextArea();
exampleArea.setColumns(22);
exampleArea.setRows(8);
exampleArea.setBorder(BorderFactory.createLineBorder(Color.black));
exampleArea.setEditable(false);

JScrollPane exampleScrollPane = new JScrollPane(exampleArea);
exampleScrollPane.setSize(200,135);
examplePanel.add(exampleScrollPane);

exampleInput = new JTextField(30);
examplePanel.add(exampleInput);
addExampleButton = new JButton("Add");
examplePanel.add(addExampleButton);
//addExampleButton.addActionListener(this);
JButton clearExampleButton = new JButton("Clear");
examplePanel.add(clearExampleButton);
//clearExampleButton.addActionListener(this);

add(eastPanel, BorderLayout.EAST);

}
public void fillSouthPanel(){
JPanel southPanel = new JPanel(new FlowLayout());
addButton = new JButton("Add");
southPanel.add(addButton);
addButton.addActionListener(new Action() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
add(southPanel, BorderLayout.SOUTH);

}
public void addEntry(int buttonNumber){
    switch(buttonNumber){

    case 1:
        tagList.add(tagInput.getText());
        tagArea.append("/n"+tagInput.getText());
        break;
    case 2:
        exampleList.add(exampleInput.getText());
        exampleArea.append("/n"+exampleInput.getText());
                break;
    case 3:
        definitionList.add(defInput.getText());
        definitionArea.append("/n"+defInput.getText());
                break;
    }
} 

public void clearEntry(int buttonNumber){
switch(buttonNumber){
    case 0: 
    case 1:
    case 2:
}
}
//public List radicalList(){
//RadicalFrame rf = new RadicalFrame();
//List<String>radicalList = rf.getRadicals();
//return 
//}
public boolean getFieldEntries(){
boolean romaji,kanji, passable;

if(kanaKanjiField.getText()!= null){
    kanjiKana = kanaKanjiField.getText();
    kanji = true;
}else{
    JOptionPane.showMessageDialog(null, "Please entry a vaild Japanese word");
    kanaKanjiField.setText(null);
    kanji = false;
}
//test if it's english
if(romanjiField.getText() !=null){
    romanji = romanjiField.getText();
    romaji = true;
}else{
    JOptionPane.showMessageDialog(null, "please enter a vaild romaji entry");
    romanjiField.setText(null);
    romaji = false;
}
if(romaji == true && kanji == true){
    passable =true;
}else{
    passable = false;
}
return passable;

}
public boolean getTextArea(){
boolean defText, exText, tagText,passable;

if(tagList.size()!=0){
    tagText = true;
}else{
    JOptionPane.showMessageDialog(null, "Please enter tags to indentify the word");
    tagText = false;
}

if(definitionList.size()!=0){
    defText = true;
}else{
    JOptionPane.showMessageDialog(null,"Please Enter defintion of the word");
    defText = false;
}
if(exampleList.size()!=0){
    exText = true;
}else{
    JOptionPane.showMessageDialog(null, "Please Enter an Example of the word usage");
    exText = false;
}

if(exText == true&&defText== true&& tagText == true){
    passable = true;
}else{
    passable = false;
}
return passable;
}
public void addWord() throws FileNotFoundException, IOException{

boolean vaild = getFieldEntries();
boolean vaild2 = getTextArea();
if(vaild == true&& vaild2 == true){

//Word word = new Word(KanjiKanaEntry, RomanjiEntry, CommonIndex, radicalList,typeentry, adjectiveIndex,VerbIndex);
File outFile = new File("dictionary.dat","UTF-8");
//Writer unicodeFileWriter = new OutputStreamWriter( new FileOutputStream("dictionary.dat)."UTF-8");//can't use writer
//fileOutputStream("dictionary.dat"),"UTF-8");
FileOutputStream outFileStream = new FileOutputStream(outFile,true);
ObjectOutputStream oos = new ObjectOutputStream(outFileStream);//append to a file
//oos.WriteObject(word);//store word
}
}
public void actionPermored(ActionEvent event) throws FileNotFoundException,  IOException{
            System.out.println("action");
//get even source
if(event.getSource() instanceof JButton){
    JButton clickedButton = (JButton)event.getSource();
    if(clickedButton == radicalButton){
        //radicalList = radicalFrame();
    }else if(clickedButton ==addButton){
        addWord();
    }else if(clickedButton == addTagButton){
        addEntry(1);
    }else if(clickedButton == addExampleButton){
        addEntry(2);
    }else if(clickedButton == addDefButton){
        addEntry(3);
    }else if(clickedButton == clearTagButton){
        clearEntry(1);
    }else if(clickedButton == clearDefButton){
        clearEntry(0);
    }else if(clickedButton == clearexButton){
        clearEntry(2);
    }

}
//get combo box entries
if(event.getSource() instanceof JComboBox){
JComboBox selectedComboBox = (JComboBox)event.getSource();
if(selectedComboBox == adjectiveTypeCombo){
    int adjectiveform = selectedComboBox.getSelectedIndex();
}else if(selectedComboBox == verbTypeCombo){
    int VerbForm = selectedComboBox.getSelectedIndex();
}

if(event.getSource() instanceof JCheckBox){
for(int i = 0;i<typeCheckBox.length;i++){
    if(typeCheckBox[i].isSelected()){
     typeList.add(typeCheckBox[i].getText()); //some how assign index numbers
    }
}

}


}

}




}'

This is just a piece of code, so sorry if it seems a little jumbled, I'm trying to condense it as much as possible.

I've tried to create a internal class to handle the action listener, but I can't seem to get it to work. Also I know I can create actionlisteners for every button, but I would like to condense all the actionevents to one class or method.

Was it helpful?

Solution

The errors are due to unimplemented methods of the Action class anonymous instance.

Any instance of the Action interface requires that all the its methods be implemented. However using Action as an anonymous instance if neither practical or good practice. Rather create a single concrete instance of a class that extends AbstractAction and set the Action for for each component.

button.setAction(mySingleAction);

where

class SingleAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
      // do stuff
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top