简体   繁体   中英

Why Won't My JAVA GUI Program Accept and Cancel Buttons Work?

I am trying to program a GUI that uploads a file with a song data base and allows a user to add, edit, or remove songs from this database. Song names appear in a combo box and when a song is selected, the pertinent information appears in un-editable text fields. The interface has buttons for add, edit, delete, accept, cancel, and exit. When either edit or add are selected, text fields become editable and the accept and cancel buttons are enabled. This functionality works okay, but the accept and cancel buttons do not work. When accept is chosen, a song is added, or current song is edited, and added to the combo box and accept and cancel are disabled while the other buttons become enabled and text fields become un-editable. Cancel should perform much the same way, but instead of adding or editing a song, the interface just reverts back to its original state. Below is the code for the actionPerformed class:

public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();
        int index = songBox.getSelectedIndex();
        Song selection = songList.get(index);
        Song newSong = new Song();

        if (source == songBox) {
            itemCodeField.setText(selection.getSongCode());
            descriptionField.setText(selection.getSongName());
            artistField.setText(selection.getSongArtist());
            albumField.setText(selection.getSongAlbum());
            priceField.setText(selection.getSongPrice());

        }

        if (source == addButton) {

            //Enable and disable appropriate buttons
            addButton.setEnabled(false);
            editButton.setEnabled(false);
            deleteButton.setEnabled(false);
            acceptButton.setEnabled(true);
            cancelButton.setEnabled(true);
            exitButton.setEnabled(false);

            //Clear text fields and make editable
            itemCodeField.setText("");
            itemCodeField.setEditable(true);
            descriptionField.setText("");
            descriptionField.setEditable(true);
            artistField.setText("");
            artistField.setEditable(true);
            albumField.setText("");
            albumField.setEditable(true);
            priceField.setText("");
            priceField.setEditable(true);

            //Set song values
            newSong.setSongCode(itemCodeField.getText());
            newSong.setSongName(descriptionField.getText());
            newSong.setSongArtist(artistField.getText());
            newSong.setSongAlbum(albumField.getText());
            newSong.setSongPrice(priceField.getText());


        }

        if (source == editButton) {

            //Enable and disable appropriate buttons
            addButton.setEnabled(false);
            editButton.setEnabled(false);
            deleteButton.setEnabled(false);
            acceptButton.setEnabled(true);
            cancelButton.setEnabled(true);
            exitButton.setEnabled(false);

            //Make text fields editable
            descriptionField.setEditable(true);
            artistField.setEditable(true);
            albumField.setEditable(true);
            priceField.setEditable(true);

        }

        if (source == deleteButton) {

                songBox.removeItemAt(index);

        }

    if (source == acceptButton)
    {
        if (source == addButton)
        {
            //Add new song to array
            songBox.addItem(newSong);

            //Enable and disable appropriate buttons
            addButton.setEnabled(true);
            editButton.setEnabled(true);
            deleteButton.setEnabled(true);
            acceptButton.setEnabled(false);
            cancelButton.setEnabled(false);
            exitButton.setEnabled(true);
        }

        if (source == editButton)
        {
            //Make text fields uneditable
            descriptionField.setEditable(false);
            artistField.setEditable(false);
            albumField.setEditable(false);
            priceField.setEditable(false);

            //Set new text
            selection.setSongName(descriptionField.getText());
            selection.setSongArtist(artistField.getText());
            selection.setSongAlbum(albumField.getText());
            selection.setSongPrice(priceField.getText());

            songBox.addItem(selection);

            //Enable and disable appropriate buttons
            addButton.setEnabled(true);
            editButton.setEnabled(true);
            deleteButton.setEnabled(true);
            acceptButton.setEnabled(false);
            cancelButton.setEnabled(false);
            exitButton.setEnabled(true);
        }
    }

        if (source == cancelButton)
        {
            //Enable and disable appropriate buttons
            addButton.setEnabled(true);
            editButton.setEnabled(true);
            deleteButton.setEnabled(true);
            acceptButton.setEnabled(false);
            cancelButton.setEnabled(false);
            exitButton.setEnabled(true);

            //Make text fields uneditable
            descriptionField.setEditable(false);
            artistField.setEditable(false);
            albumField.setEditable(false);
            priceField.setEditable(false);
        }

        if (source == exitButton) {


            System.exit(0);

        }
    }

The program currently compiles and runs. The add and edit buttons do what they intend, but the accept and cancel buttons do not. When chosen, they don't do anything at all. Text fields remain editable and the accept and cancel buttons remain enabled while all other buttons remain disabled.

Update: The cancel button works to make the correct items enabled or disabled, but any changes made are not reset immediately, you have to toggle the combo box. I understand now that 'source' can't equal two button inputs at the same time. However, the accept button has to do two different things depending on whether the user first selected add or edit, and I don't know how to handle that.

Your code looks like this:

if (source == acceptButton)
{
    // Irelevant code removed. 

    if (source == cancelButton)
    {
       // This code is newer run, because you require first that source 
       // is==acceptButton and here you require that source==cancelButton.
    }
}

But a better solution, is to have a method for each button, instead of looking at the event source. If you are using java 8(And you should be) have a look at http://www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM