gpt4 book ai didi

Java ActionListener 操作未执行

转载 作者:行者123 更新时间:2023-12-01 12:46:55 25 4
gpt4 key购买 nike

我有一个 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. ");
}
}

最佳答案

在您的 initialize() 方法中,您要添加到面板的 removeBtn 是该方法的局部变量。同样的事情也发生在 addButtons() 方法中。然后,稍后,当您调用 addListeners() 时,您将 ActionListener 添加到未添加到面板的私有(private)成员 JButton 。从 initialize() 方法中删除本地 JButton removeBtn 并初始化成员 1。

关于Java ActionListener 操作未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24608008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com