简体   繁体   中英

I don't understand why my ActionListeners are not working when pressing JButton?

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

    }

}

Yes you can call methods from your onclicklistener but the problem is in your called method, you are calling

model.addRow()

on DefaultTableModel which is not defined yet. Initialize your "model" variable before calling the pressed method.

Your main problem is you're shadowing your variables...

You define model as an instance variable

public DefaultTableModel model;

But in the constructor you define a local variable of the same name...

DefaultTableModel model = new DefaultTableModel(data, col);

This means that when NEWpressed is called, model is null

You also define p and jt as a instance variable

public JTable jt;
public JPanel p;

But in the constructor, you define a local variable of the same name...

JPanel p = new JPanel();
//...
JTable jt = new JTable(model);

Which means when updatepressed is pressed jt and p are null

Lose the local definitions and simply instantiate the instance variables within the constructor...

public GUI() {
    p = new JPanel();
    //...
    model = new DefaultTableModel(data, col);
    //...
    jt = new JTable(model);

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