简体   繁体   中英

How to get information from a JTable row when clicking on it with mouse?

I have an application which stores students, courses, what they are studying etc. The application is connected to a SQL database. I have no problem inserting or searching for stuff in the database through my application. But I still haven't figured out a way to delete rows in the tables through my application, in an easy way. I can, just like searching and inserting, use userinput in order to delete a row (example JTextFields) but I'd like an easier way. So I've figured out that if I add a popupmenu to my table, then I can rightclick a row and make it show "Delete row", as shown here

在此处输入图片说明

But now, I'd like to get the information in that row so I can send it to my frame, controller and later on database but I have no idea how. In this example I'd like to get "Lars" in a String and "12" in a int variable as you might imagine. So far I have created the code which does the thing in the picture. I've removed some code so it's easier to see what I'm getting at here. Any help is appreciated!

    table = new JTable(studentTableModel);
    popup = new JPopupMenu();

    JMenuItem removeItem = new JMenuItem("Delete student");
    popup.add(removeItem);

    table.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {

            int row = table.rowAtPoint(e.getPoint());

            table.getSelectionModel().setSelectionInterval(row, row);

            if(e.getButton() == MouseEvent.BUTTON3) {
                popup.show(table, e.getX(), e.getY());
            }
        }
    });

    removeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int row = table.getSelectedRow();

            if(studentTableListener != null) {
                studentTableListener.rowDeleted(row);
                studentTableModel.fireTableRowsDeleted(row, row);
            }
        }
    });`

There's a few ways to handle this.

ArrayList

You can declare a final ArrayList to keep track of the students. This allows you to keep track of individual students as they get deleted. The downside is that you can't undo a delete, so you'd have to clear the array and reset the model or it would not represent the database properly.

    final ArrayList<Student> list = new ArrayList<Student>();

    removeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int row = table.getSelectedRow();

            if(studentTableListener != null) {

                Student student = new Student();
                student.setName(studentTableModel.getValueAt(...));
                //get the values from the row and fill a Student object
                list.add(student);

                studentTableListener.rowDeleted(row);
                studentTableModel.fireTableRowsDeleted(row, row);
            }
        }
    });

Then, you could iterate through the list whenever you propagate to the database.

for(Student s : list) {
    //do call to delete s from database
}

And afterwards, clear the list.

list.clear();

Undo Events

A tutorial can be found here for how to implement an undoable JTable, which is more complex. This would allow you to roll back deletes and changes. I don't have the specific code related to this (it's significantly larger, but supports the JTable much better).

However, you'd basically create your own UndoAction for row deletes, and have it store the row being deleted.

Whenever you do a delete, you'd store a RowDeleteUndo event that stores an array of all the values you deleted. You can then override the toString() method or create a new method to get all the values out in a Student object.

Then, when you commit, you take the stack from the undo tree and iterate through it backwards (it's last-in-first-out by default) and commit in that order if you want to mirror the user's operations.

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