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");
}
}