I use action listener and then call it to a class that relays a function. I didn't want to use implements ActionListener
in the class because it is more complicated to deal with multiple buttons. Can you access a method from an ActionListener
?
I am also getting a Serializable caution. I'm not sure what that means. I suppressed the warnings and looked it up online, however, I still do not understand it in context. First GUI project, any help is greatly appreciated.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame{
public JFrame j;
public JPanel p;
public JButton NEW;
public JButton update;
public DefaultTableModel model;
public JTable jt;
public GUI(){
setVisible(true);
setSize(600,400);
setTitle("Student Record Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
JButton NEW = new JButton("Additional Student");
NEW.setBounds(10,10,20,20);
NEW.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
NEWpressed();
}
});
p.add(NEW);
JButton update = new JButton("Update Exam Scores");
update.setBounds(50,40,20,20);
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
updatepressed();
}
});
p.add(update);
String [] col = {"Last Name", "First Name", "HKID", "Student ID", "Final Exam Score"};
String [][] data = {{"Rollins", "Zachary", "1234", "4321", "88"},{"Preston","John", "4321", "1234", "94"}};
DefaultTableModel model = new DefaultTableModel(data, col);
JTable jt = new JTable(model);
jt.setEnabled(false);
jt.setPreferredScrollableViewportSize(new Dimension(400,300));
jt.setFillsViewportHeight(true);
jt.setBackground(Color.BLUE);
jt.setAlignmentY(BOTTOM_ALIGNMENT);
JScrollPane jps = new JScrollPane(jt);
p.add(jps);
add(p);
}
public void NEWpressed(){
model.addRow(new Object[]{"col 1", "col 2", "col 3", "col 4", "col 5"});
}
public void updatepressed(){
jt.setEnabled(true);
p.add(jt);
add(p);
}
public static void main(String args []){
GUI a = new GUI();
a.setVisible(true);
}
}