Hello, I have been having a problem in some code I'm writing.
I'm doing a project in 8th grade that involves some GUI and I have a problem with my listener and this.dispose().
I'll post the code with the error here:
Basically I'm getting an error on addFrame, which is the name of my JFrame variable that contains all the stuff in the window.class doneListener implements ActionListener{ public void actionPerformed (ActionEvent e){ if("doneAdd".equals(e.getActionCommand())){ addFrame.dispose();
I'll post the whole thing here:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class primary extends JPanel implements ListSelectionListener { private JList list; private DefaultListModel listModel; private static final String addString = "ADD!"; private static final String removeString = " - "; private static final String editString = "edit"; private JButton removeButton,editButton; private static final String doneAdd = "Done!"; public primary() { super(new BorderLayout()); listModel = new DefaultListModel(); //Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0); list.addListSelectionListener(this); list.setVisibleRowCount(20); JScrollPane listScrollPane = new JScrollPane(list); JButton addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddListener()); addButton.setEnabled(true); editButton = new JButton(editString); editButton.setActionCommand(editString); editButton.addActionListener(new EditListener()); removeButton = new JButton(removeString); removeButton.setActionCommand(removeString); removeButton.addActionListener(new RemoveListener()); //Create a panel that uses BoxLayout. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BorderLayout()); JPanel addremovePane = new JPanel(); addremovePane.setLayout(new BorderLayout()); addremovePane.add(addButton, BorderLayout.LINE_START); addremovePane.add(removeButton, BorderLayout.LINE_END); addremovePane.add(Box.createHorizontalStrut(5), BorderLayout.CENTER); buttonPane.add(addremovePane, BorderLayout.LINE_START); buttonPane.add(editButton, BorderLayout.LINE_END); buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); add(listScrollPane, BorderLayout.CENTER); add(buttonPane, BorderLayout.PAGE_END); if (listModel.getSize()==0){ removeButton.setEnabled(false); editButton.setEnabled(false); } } class RemoveListener implements ActionListener { public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); listModel.remove(index); int size = listModel.getSize(); if (size == 0) { //Nobody's left, disable remove removeButton.setEnabled(false); } else { //Select an index. if (index == listModel.getSize()) { //removed item in last position index--; } list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } } } class EditListener implements ActionListener { public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } } class addWindow extends JPanel{ public addWindow(){ super(new BorderLayout()); String [] eqSel = {"Test1","Test2","Test3"}; JPanel topPanel = new JPanel(new BorderLayout()); JPanel buttonPanel = new JPanel(new BorderLayout()); JPanel comboPanel = new JPanel(new BorderLayout()); JCheckBox solType = new JCheckBox("Equation as solution?",false); solType.setEnabled(true); topPanel.add(solType,BorderLayout.PAGE_END); JButton doneButton = new JButton(doneAdd); doneButton.setEnabled(true); doneButton.setActionCommand(doneAdd); doneButton.addActionListener(new doneListener()); JComboBox eqType = new JComboBox(eqSel); eqType.setEnabled(true); topPanel.add(eqType,BorderLayout.LINE_START); add(topPanel,BorderLayout.PAGE_START); buttonPanel.setLayout(new BorderLayout()); buttonPanel.add(doneButton, BorderLayout.PAGE_END); add(buttonPanel,BorderLayout.LINE_END); } class doneListener implements ActionListener{ public void actionPerformed (ActionEvent e){ if("doneAdd".equals(e.getActionCommand())){ addFrame.dispose(); } } } } public class AddListener implements ActionListener{ //Required by ActionListener. public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); //get selected index if (index == -1) { //no selection, so insert at beginning index = 0; } else { //add after the selected item index++; } JFrame addFrame = new JFrame("Equation Editor"); //Select the new item and make it visible. list.setSelectedIndex(index); list.ensureIndexIsVisible(index); //This creates the window //These lines create the frame addFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new addWindow(); newContentPane.setOpaque(true); //content panes must be opaque addFrame.setContentPane(newContentPane); //Display the window. addFrame.pack(); addFrame.setSize(300,500); addFrame.setVisible(true); addFrame.setResizable(false); } } //This method is required by ListSelectionListener. public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable remove and edit button removeButton.setEnabled(false); editButton.setEnabled(false); } else { //Selection, enable the remove and edit button removeButton.setEnabled(true); editButton.setEnabled(true); } } } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Puzzles!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JComponent newContentPane = new primary(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
I've tried moving around the class that it's in, nesting it within the JFrame class, making the JFrame class public, and changing around the syntax of dispose();. My teacher is out of ideas and I am stuck until I can get this solved.
Thanks!