未执行Java ActionListener操作

时间:2014-07-07 10:05:10

标签: java swing jtable actionlistener defaulttablemodel

我有一个JButton,它应该从我的GUI中的JTable中删除选定的行。但是,出于某种原因,我用来执行此操作的ActionListener()中的代码似乎永远不会被调用...

private void addListeners(){
    ...
    //removeBtn = new JButton("Remove");
    removeBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("Remove button clicked. Printed from actionListener. ");
            removeRow(e);
        }
    });

removeRow()方法稍后在同一个类中定义:

public void removeRow(ActionEvent arg0){
    System.out.println("'Remove' button pressed, Printed from 'removeRow()' method. ");
    int selectedRow = jEntityFilterTable.getSelectedRow();
    DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();
    model.removeRow(selectedRow);
    System.out.println("Selected row should have been removed. Printed from 'removeRow()' method. ");

removeBtn使用以下行声明为类顶部的全局变量:

private JButton removeBtn = new JButton("Remove");

当我运行代码,并单击GUI上的“删除”按钮时,没有任何反应,我甚至看不到控制台中显示的调试...但我无法解决为什么 - 任何人都可以发现我做错了什么?为什么没有调用actionListener / removeRow()方法?

编辑07/07/2014 @ 13:20

以下是课程相关部分的完整代码,如下所示:

public class JConfigurationPane extends JPanel implements UndoableEditListener, ChangeListener, ActionListener
{
    ...
    private JButton addBtn = null;
    private JButton saveBtn = null;
    private JButton removeBtn = new JButton("Remove"); //null;
    private JButton editBtn = null;
    public boolean addBtnClicked = false;
    public boolean saveBtnClicked = false;
    public boolean removeBtnClicked = false;
    public boolean editBtnClicked = false;
    /**
     * This method initializes 
     * 
     */
    public JConfigurationPane(ConfigurationDataModel dataModel)
    {
    super();

    this.dataModel = dataModel;

    initialize();
    initialiseData();
    addListeners();
}

public JConfigurationPane()
{
    super();

    initialize();

    addListeners();
}

private void addListeners()
{
    System.out.println("--- 'addListeners()' method has been called. ---");
    jcbRxFilterExcludes.addChangeListener(this);

    docRxAddress = jfRxAddress.getDocument();
    docRxPort = jfRxPort.getDocument();
    docRxExerciseID = jfRxExerciseID.getDocument();
    docRxMaxPduSize = jfRxMaxPduSize.getDocument();

    docRxAddress.addUndoableEditListener(this);
    docRxPort.addUndoableEditListener(this);
    docRxExerciseID.addUndoableEditListener(this);
    docRxMaxPduSize.addUndoableEditListener(this);

    addBtn.addActionListener(this);
    /*Add action listeners for other buttons (07/07/2014 @ 08:35) 
    saveBtn.addActionListener(this);
    removeBtn.addActionListener(this);
    editBtn.addActionListener(this);

    Causes an "Exception in thread 'main', java.lang.nullPointerException on: 
        'saveBtn.addActionListener(this);
        'addListeners();' call in 'public JConfigurationPane()'
        'JConfigurationPane panel = new JConfigurationPane()' call in 'main(String[] args)'
    */
    //removeBtn = new JButton("Remove");
    removeBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("Remove button clicked. Printed from actionListener. ");
            removeRow(e);
        /*  System.out.println("'Remove' button pressed. ");
            int selectedRow = jEntityFilterTable.getSelectedRow();
            DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();
            model.removeRow(selectedRow);
            System.out.println("Selected row should have been removed. "); */
        }
    });
}

private void initialiseData()
{
    ...
}

/**
 * This method initializes this
 * 
 */
private void initialize() {
    ...

    /*Create filter buttons */
    System.out.println("------------------JButtons BEING CREATED---------------------");
    addBtn = new JButton("Add");
    addBtn.setBounds(950, 135, 125, 25);
   //     addBtn.addActionListener(new ActionListener(){
//      public void actionPerformed(ActionEvent e){

//      }
//  });
    JButton saveBtn = new JButton("Save");
    saveBtn.setBounds(1100, 135, 125, 25);
    JButton removeBtn = new JButton("Remove");
    removeBtn.setBounds(950, 200, 125, 25);
    JButton editBtn = new JButton("Edit");
    editBtn.setBounds(1100, 200, 125, 25);
    System.out.println("------------------JButtons CREATED-------------------------");

    ...

    /*Add filter buttons */
    System.out.println("-------------------JButtons BEING ADDED TO GUI--------------------");
    this.add(addBtn);
    this.add(saveBtn);
    this.add(removeBtn);
    this.add(editBtn);
    System.out.println("------------------JButtons ADDED TO GUI---------------------");
}

/**
 * This method initializes jfRxAddress  
 *  
 * @return javax.swing.JTextField   
 */
private JTextField getJfRxAddress()
{
    ...
}

/**
 * This method initializes jfRxPort 
 *  
 * @return javax.swing.JTextField   
 */
private JTextField getJfRxPort()
{
    ...
}

/**
 * This method initializes jfRxExerciseID   
 *  
 * @return javax.swing.JTextField   
 */
private JTextField getJfRxExerciseID()
{
    ...
}

/**
 * This method initializes jEntityFilterPane    
 *  
 * @return javax.swing.JScrollPane  
 */
private JScrollPane getJEntityFilterPane()
{
    ...
}

/**
 * This method initializes jEntityFilterTable   
 *  
 * @return javax.swing.JTable   
 */
private JTable getJEntityFilterTable()
{
    ...
}

/**
 * This method initializes jEntitySymbolsPane   
 *  
 * @return javax.swing.JScrollPane  
 */
private JScrollPane getJEntitySymbolsPane()
{
    ...
}

/**
 * This method initializes jEntitySymbolsTable  
 *  
 * @return javax.swing.JTable   
 */
private JTable getJEntitySymbolsTable()
{
    ...
}

/*Method to add buttons to 'Plugin Configuration' window */
private void addButtons(){
    /*Buttons moved up to initialize() method on 25/06/2014 @ 17:00 */
    JButton addBtn = new JButton("Add");
    JButton saveBtn = new JButton("Save");
    JButton removeBtn = new JButton("Remove");
    JButton editBtn = new JButton("Edit");

    addBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            /*(25/06/2014 @ 15:20) Need to add code here to add a new editable row to 'Entity Filter' table */
            addBtnClicked = true;
        }
    });
    //addBtn.setBounds(1150, 135, 30, 15);
    saveBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            /*(25/06/2014 @ 15:20) Need to add code here to save the data in the 'Entity Filter' table to a set of variables */
            saveBtnClicked = true;
        }
    });
    //saveBtn.setBounds(1190, 135, 30, 15);
    removeBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        /*(25/05/2014 @ 15:25) Need to add code here to remove the data in selected row from variables, and remove row from table */
            removeBtnClicked = true;
        }
    });
    //removeBtn.setBounds(1150, 160, 30, 15);
    editBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            /*(25/06/2014 @ 15:25) Need to add code here to enable editing of data in selected row */
            editBtnClicked = true;
        }
    });
    //editBtn.setBounds(1190, 160, 30, 15);
    System.out.println("'addButtons()' method is being called by 'initialize()' in JConfigurationPanel.java");
}

/**
 * This method initializes jcbRxFilterExcludes  
 *  
 * @return javax.swing.JCheckBox    
 */
private JCheckBox getJcbRxFilterExcludes()
{
    ...
}

/**
 * This method initializes jfRxMaxPduSize   
 *  
 * @return javax.swing.JTextField   
 */
private JTextField getJfRxMaxPduSize()
{
    ...
}

@Override
public void undoableEditHappened(UndoableEditEvent editEvent)
{
    ...
}

@Override
public void stateChanged(ChangeEvent changeEvent)
{
    ...
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setMinimumSize(new Dimension(500,500));
    frame.setSize(new Dimension(500,500));

    JConfigurationPane panel = new JConfigurationPane();

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    /*Check which button has been pressed, perform a different action depending on which button it was. Reset the check variables
     * to false after performing action */

    // TODO Auto-generated method stub
    /*Code to add row to table when button is pressed */
    System.out.println("'Add' button pressed.");

    DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();

    Object[] obj = new Object[]{}; 

    System.out.println("Row Count: " + model.getRowCount());
    System.out.println("1st Column: " + model.getColumnName(0));
    System.out.println("Column Count: " + model.getColumnCount());

    model.addRow(obj);

    System.out.println("--- ActionListener added to 'addBtn' ---");

    /*Code to remove selected row from the table when button is clicked 
    int selectedRow = jEntityFilterTable.getSelectedRow();
    model.removeRow(selectedRow); */

}

public void removeRow(ActionEvent arg0){
    System.out.println("'Remove' button pressed. Printed from 'removeRow()' method. ");
    int selectedRow = jEntityFilterTable.getSelectedRow();
    DefaultTableModel model = (DefaultTableModel)jEntityFilterTable.getModel();
    model.removeRow(selectedRow);
    System.out.println("Selected row should have been removed. Printed from 'removeRow()' method. ");
}
}

3 个答案:

答案 0 :(得分:1)

initialize()方法中,您要添加到面板的removeBtn是该方法的局部变量。 addButtons()方法中也出现同样的情况。然后,稍后,当您致电addListeners()时,您将ActionListener添加到未添加到面板的私人会员JButton。从JButton removeBtn方法中删除本地initialize(),然后初始化成员。

答案 1 :(得分:0)

尝试将按钮声明为最终字段。如果代码的某些部分更改了按钮对象,则会收到警告。

答案 2 :(得分:0)

  

再次出现空指针异常。

在您的代码中使用:

JConfigurationPane panel = new JConfigurationPane();

而不是:

public JConfigurationPane(ConfigurationDataModel dataModel)

为什么你有两个构造者?为什么要将数据模型传递给类?

另外,请勿使用setBounds()来设置按钮的大小/位置。 Swing旨在与布局管理器一起使用,因此使用布局管理器。也许你会从使用FlowLayout的小组开始。

最后,以下代码(可能)不起作用:

model.removeRow(selectedRow);

所选行指的是表视图中的行,而不是表模型中的行。如果您的代码进行排序或过滤,那么索引可能会有所不同。首先需要使用以下方法转换所选索引:

JTable.convertRowIndexToModel(...);