質問

I am trying to add a listener to JTable that delete a selected rows.

I have a class ToolBar in which contains all the buttons, but all my JTable setting is on another class: TablePanel.

I want to be able to delete row(s) when the user click a buttons in ToolBar Class.

ToolBar.java

public class ToolBar extends JPanel
{

TablePanel tablePane;

JButton openButton, saveButton, addButton, delButton;
ImageIcon openIcon, saveIcon, addIcon, delIcon;
Image openImage, saveImage, addImage, delImage;
Image openImage2, saveImage2, addImage2, delImage2;

ButtonListener listener;

public ToolBar(TablePanel tablePane)
{

    this.tablePane = tablePane;

    //Bunch of codes here to initialize all the buttons and icons
}

//Button Listener
private class ButtonListener implements ActionListener
{

    public void actionPerformed(ActionEvent event)
    {

        if (event.getSource() == openButton)
            JOptionPane.showMessageDialog(null, "hello");
        if (event.getSource() == saveButton)
            tablePane.saveData();
        if (event.getSource() == addButton)
            tablePane.addRow();
        if (event.getSource() == delButton)
            tablePane.delRow();


    }

}

}

TablePanel

public class TablePanel extends JPanel
{

//Table's column name
private ArrayList<String> columnNames = new ArrayList<String>();


//List of data, later needs to be change 
//so that it will be editable on the go
//It starts with 5 columns 
//User can add more columns 
private ArrayList<Object[]> data = new ArrayList<Object[]>();


//DefaultTableModel is needed for adding new rows
private JTable table;
private TModel tModel;

private JScrollPane scrollPane;

//For importing & exporting data
JFileChooser fileChooser;


public TablePanel()
{

    //Column Names:
    columnNames.add("Date");
    columnNames.add("Category");
    columnNames.add("Details");
    columnNames.add("Add/Subtract");
    columnNames.add("Total");

    //Example data:
    data.add(new Object[]{20140925, "Grocery", "Supermarket", -5.23,600.00});
    data.add(new Object[]{20141013,"Car Maintenance", "Changing Tires", -200.00, 400.00});



        //Some codes here
}
private class TModel extends AbstractTableModel implements TableModelListener
{
    //Bunch of codes(methods) in here such as:
    //public voide setValueAt, public Boolean isCellEditable, getColumnCount etc.
    //Method for deleting row
    public void delRow()
    {

    public void tableChanged(TableModelEvent event)
    {

        int row = event.getFirstRow();
        fireTableRowsDeleted(row, row);
    }

    }


} //End of TModel class



} //End of TablePanel Class 

How should I implement listener and fireTableDeleted(int row)? What should I add in the public void delRow()?

enter image description here

役に立ちましたか?

解決

Someone is going to need a reference to someone else, but you also want to maintain a level of separation so you don't end up tightly coupling your components together

You could use a controller of some kind, which has a reference to the table and is either listening directly to the buttons on the toolbar or which the tool bar can call and instruct it on what it wants to do.

For example, when clicked, the delete button would simply instruct the controller to "delete the selected rows". The controller would ask the TablePane to "Delete the selected rows" and the TablePane would comply.

This could be achieved through the Actions API where the Action would act as the controller.

Basically you would create a new DeleteRowAction (this is class you'd have to create), which had a reference to the TablePane and which you would pass to the ToolBar pane. You would then apply the Action to the delete button (JButton#setAction) and let it take care of everything else for you

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top