Slabach
Slabach

Reputation: 491

Multiple Buttons?

I'm having trouble getting mutliple buttons to work in Java. Right now I have 2 buttons that should both be doing something. The "Fill" button should fill the top 3 fields "Title" "Director" and "Year" with the what I have set in the "Fill" method in the "Io" class. The "Add" button should then take what is in the top 3 fields and copy them into the bottom 3 text areas. The "Save" button does nothing as of right now. However, for some reason I can only get the program to recognize the first button. If I switch the buttons around in the 'If' statement and move the "Add" to the 'else if' and vice versa it will still only recognize the "Add" button. No matter what I try I cannot seem to get it to execute both buttons. If someone could point me in the right direction, any help would be great! Thank you in advance! :)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class database extends Io implements ActionListener{
    //frame
    private JFrame window = new JFrame("Movie Database");

    //Buttons
    public JButton btnAdd = new JButton("Add");
    public JButton btnFill = new JButton("Fill");
    public JButton btnSave = new JButton("Save");

    //Label
    private JLabel lblTitle = new JLabel("Movie Title:");
    private JLabel lblDir = new JLabel("Director:");
    private JLabel lblYear = new JLabel("Year:");

    //Panel
    private Panel pnlNorth = new Panel();
    private Panel pnlSouth = new Panel();
    private Panel pnlCenter = new Panel();

    public void init(){
        //set main window
        window.setLayout(new BorderLayout());

        //add JLabel
        window.add(pnlNorth,BorderLayout.NORTH);
        window.add(pnlCenter,BorderLayout.CENTER);
        window.add(pnlSouth,BorderLayout.SOUTH);

        //set panels to gridframe
        pnlNorth.setLayout(new GridLayout(1,3));
        pnlCenter.setLayout(new GridLayout(2,3));
        pnlSouth.setLayout(new GridLayout(1,3));

        //layout center panel
        pnlNorth.add(lblTitle);
        pnlNorth.add(lblDir);
        pnlNorth.add(lblYear);

        //layout center panel
        pnlCenter.add(inTitle);
        pnlCenter.add(inDir);
        pnlCenter.add(inYear);
        pnlCenter.add(btnAdd);
        pnlCenter.add(btnFill);
        pnlCenter.add(btnSave);

        //layout south panel
        pnlSouth.add(outTitle);
        pnlSouth.add(outDir);
        pnlSouth.add(outYear);

        //actionlistener
        btnAdd.addActionListener(this);

        //generic frame operation
        window.pack();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    } //end init

    public database(){
        init();
    }

    public void actionPerformed(ActionEvent a){
        Object source = a.getSource();
        if(source==btnAdd){
            set();
        } else if(source==btnFill){
            fill();
        }           
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new database();
    } // end main
} //end class

class Io{
    String title;
    String dir;
    String year;

    //JText
    public JTextField inTitle = new JTextField("",20);
    public JTextField inDir = new JTextField("",20);
    public JTextField inYear = new JTextField("",20);
    public JTextArea outTitle = new JTextArea("",20,20);
    public JTextArea outDir = new JTextArea("",20,20);
    public JTextArea outYear = new JTextArea("",20,20);

    public void fill(){
        inTitle.setText("Interstellar");
        inDir.setText("Christopher Nolan");
        inYear.setText("2014");
    }


    public void set(){
        outTitle.append(inTitle.getText() + "\n");
        outDir.append(inDir.getText() + "\n");
        outYear.append(inYear.getText() + "\n");
    } 
}

Upvotes: 1

Views: 119

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You've only added an ActionListener to one button:

btnAdd.addActionListener(this);

And so, only one button, the btnAdd, will work, since the buttons won't work by magic and all require that an ActionListener be added to them for them to have any function whatsoever. i.e.,

btnFill.addActionListener(....something here....);

Myself, I prefer to use anonymous ActionListeners if possible, something like

btnFill.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        // do some fill stuff here
    }

});

This suggests that you're trying to use JButtons without first reading the tutorials, something I advise against. Please have a look at them as they're very helpful: How to use Buttons.

Upvotes: 3

Related Questions