Reputation: 7
I'm trying to compile this and it keeps spitting out 6 errors stating error:call to super must be first statement in constructor
- I'm really confused and have not been able to correct it. Help would be appreciated.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
public class MenuFrame extends JFrame
{
private class ItemHandler
implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
{
if(fieldItems[0].isSelected())
application.insert(textArea);
else
if(fieldItems[1].isSelected())
application.delete(textArea);
if(fieldItems[2].isSelected())
application.update(textArea);
repaint();
}
final MenuFrame this$0;
private ItemHandler()
{
this$0 = MenuFrame.this;
super();
}
}
public MenuFrame()
{
super("Using JMenus");
JMenu jmenu = new JMenu("File");
jmenu.setMnemonic('F');
JMenuItem jmenuitem = new JMenuItem("New");
jmenuitem.setMnemonic('N');
jmenu.add(jmenuitem);
jmenuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
File file = getFile(2);
JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1);
application = new HardwareStore(file.toString(), 0);
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
}
);
JMenuItem jmenuitem1 = new JMenuItem("Open");
jmenuitem1.setMnemonic('O');
jmenu.add(jmenuitem1);
jmenuitem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
File file = getFile(1);
if(file.exists())
{
JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1);
application = new HardwareStore(file.toString(), 1);
}
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
}
);
jmenu.addSeparator();
JMenuItem jmenuitem2 = new JMenuItem("Exit");
jmenuitem2.setMnemonic('x');
jmenu.add(jmenuitem2);
jmenuitem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
System.exit(0);
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
}
);
JMenuBar jmenubar = new JMenuBar();
setJMenuBar(jmenubar);
jmenubar.add(jmenu);
JMenu jmenu1 = new JMenu("Process");
jmenu1.setMnemonic('P');
String as[] = {
"Insert", "Delete", "Update"
};
JMenu jmenu2 = new JMenu("Edit");
jmenu2.setMnemonic('E');
fieldItems = new JRadioButtonMenuItem[as.length];
fieldButtonGroup = new ButtonGroup();
ItemHandler itemhandler = new ItemHandler();
for(int i = 0; i < as.length; i++)
{
fieldItems[i] = new JRadioButtonMenuItem(as[i]);
jmenu2.add(fieldItems[i]);
fieldButtonGroup.add(fieldItems[i]);
fieldItems[i].addActionListener(itemhandler);
}
fieldItems[0].setSelected(true);
jmenu1.add(jmenu2);
jmenu1.addSeparator();
JMenuItem jmenuitem3 = new JMenuItem("List");
jmenuitem3.setMnemonic('L');
jmenu1.add(jmenuitem3);
jmenuitem3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
application.display(textArea);
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
}
);
jmenubar.add(jmenu1);
JMenuItem jmenuitem4 = new JMenuItem("About...");
jmenuitem4.setMnemonic('A');
jmenubar.add(jmenuitem4);
jmenuitem4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
JOptionPane.showMessageDialog(MenuFrame.this, "This is an example\nof using menus", "About", -1);
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
}
);
textArea = new JTextArea();
add(textArea, "Center");
}
private File getFile(int i)
{
JFileChooser jfilechooser = new JFileChooser();
jfilechooser.setFileSelectionMode(0);
int j;
if(i == 1)
j = jfilechooser.showOpenDialog(this);
else
j = jfilechooser.showSaveDialog(this);
if(j == 1)
System.exit(1);
File file = jfilechooser.getSelectedFile();
if(file == null || file.getName().equals(""))
{
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", 0);
System.exit(1);
}
return file;
}
private JRadioButtonMenuItem fieldItems[];
private JRadioButtonMenuItem fonts[];
private JCheckBoxMenuItem styleItems[];
private JTextArea textArea;
private JTextField recNumText;
private JTextField newValueText;
private JTextField fieldText;
private ButtonGroup fontButtonGroup;
private ButtonGroup fieldButtonGroup;
private int style;
private HardwareStore application;
}
Upvotes: 0
Views: 569
Reputation: 1503479
All of the anonymous inner classes appear to include the generated code, as if you'd decompiled an existing class file (which I suspect you have). Each time you have something like this:
jmenuitem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
File file = getFile(1);
if(file.exists())
{
JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1);
application = new HardwareStore(file.toString(), 1);
}
}
final MenuFrame this$0;
{
this$0 = MenuFrame.this;
super();
}
});
... you should get rid of the final part - it will be autogenerated for you anyway. You only need:
jmenuitem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
File file = getFile(1);
if(file.exists())
{
JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1);
application = new HardwareStore(file.toString(), 1);
}
}
});
(Although I'd add an @Override
annotation too.)
Upvotes: 0
Reputation: 394126
The error is clear enough.
Change it to this :
private ItemHandler()
{
super(); // call to super must be the first line of the constructor
this$0 = MenuFrame.this;
}
As to the calls to super
in instance initializer blocks, such initializer blocks are copied to the start of each constructor, so you have to make the same change :
{
super();
this$0 = MenuFrame.this;
}
However, if the class that has such block already has a constructor that calls super(), this won't pass compilation.
Upvotes: 1