从 AbstractTableModel 扩展时如何切换 JTable 中复选框的值

How to toggle the value of a checkbox in a JTable when extending from AbstractTableModel

我希望你能帮助我解决困扰我 的问题!

我已经阅读了 很多 的 SO 答案和来自网络的各种示例,并且我已尽力阅读 DefaultTableModel 源代码以尝试了解发生了什么在幕后,但不幸的是,我仍然完全缺乏大局观。我似乎找不到任何示例、文章或视频来解释 TableModel 如何或为何 做它做的事情以及如何根据我的需要有效地改变它。

我正在处理一个项目,我需要在 JTable 中显示与每个对象关联的数据。我有一个 ObjectCollection class,它将所有对象保存在 ArrayList 内的一个文件夹中。正是这个集合被传递到我的自定义 TableModel 以填充 JTable。然而,问题是我需要 JTable 有一个包含用户可以与之交互的复选框的列(在本例中为第 0 列)。然而,复选框的状态 not 反映在 Object 或 ObjectCollection 中。一旦用户做出任何选择,就会调用一个单独的方法,然后在 JTable 中搜索选中的行,并对 ObjectCollection 中的相应对象进行操作——因此对象本身和集合都不需要知道状态复选框。它本质上就好像 TableModel 正在保存来自两个不同来源的数据 - 该行中对象的第 1 到 4 列 该行第 0 列中单元格的选中状态。我希望这是有道理的,但如果没有的话,我希望下面的示例代码能够比我的话更好地演示它。

我遇到的问题是双重的。首先,虽然我可以显示复选框并且可以注册对它们的点击,但我不知道如何实际切换复选框的选中状态(当我点击复选框时没有任何反应)。其次,假设我能弄清楚如何切换复选框状态,我不知道如何使用 getValue() 方法来 return 它,考虑到它不是 Object 的成员(再次,请参阅我的下面的示例代码,如果我在这里不清楚)。

出于各种原因,我无法 post 来自真实项目的代码(目前),因此我为此问题创建了一个假的示例项目。原理完全相同,我面临的问题也是如此,所以如果我能在这个例子中解决它,我可以在我的真实代码库中以同样的方式解决它。无论如何,这是我到目前为止的示例代码...

一个简单的例子对象

public class ExampleMovie {

    private final String title;
    private final String year;
    private final String director;
    private final double score;

    public ExampleMovie(String title, String year, String director, double score) {
        this.title = title;
        this.year = year;
        this.director = director;
        this.score = score;
    }

    public String getTitle() {
        return title;
    }

    public String getYear() {
        return year;
    }

    public String getDirector() {
        return director;
    }

    public double getScore() {
        return score;
    }
}

ObjectCollection

public class ExampleMovieCollection {

    private List<ExampleMovie> allMovies;

    public ExampleMovieCollection() {
        allMovies = new ArrayList<>();
    }

    public void addMovie(ExampleMovie movie) {
        allMovies.add(movie);
    }

    public void removeMovie(ExampleMovie movie) {
        if (allMovies.contains(movie)) {
            allMovies.remove(movie);
        }
    }

    public List<ExampleMovie> getMovies() {
        return allMovies;
    }
}

自定义TableModel

public class ExampleMovieCollectionTableModel extends AbstractTableModel {

    private List<ExampleMovie> items;
    private String[] columnNames = {"Checked?", "Title", "Year", "IMDB Score", "Director"};

    public ExampleMovieCollectionTableModel(ExampleMovieCollection movieCollection) {
        this.items = new ArrayList<>(movieCollection.getMovies());
    }

    @Override
    public int getRowCount() {
        return  items.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0; // Only the first column in the table should be editable
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ExampleMovie item = items.get(rowIndex);
        switch (columnIndex) {
            case 0: // How do I return the checked state of the cell as it isn't a member of item?
            case 1: return item.getTitle();
            case 2: return item.getYear();
            case 3: return item.getScore();
            case 4: return item.getDirector();
            default: return null;
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        // How do I toggle the checked state of the cell when columnIndex == 0?
        System.out.println("(" + rowIndex + ", " + columnIndex + ") clicked. Value = " + aValue);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return Boolean.class;
        }
        return String.class;
    }
}

main()方法和GUI创建代码等

public class ExampleForm extends JFrame {

    private JPanel rootPanel;
    private JTable exampleTable;
    private JScrollPane tableScrollPane;
    private ExampleMovieCollection movieCollection;
    private ExampleMovieCollectionTableModel tableModel;

    public ExampleForm() {
        movieCollection = new ExampleMovieCollection();
        movieCollection.addMovie(new ExampleMovie("The Shawshank Redemption", "1994", "Frank Darabont", 9.3));
        movieCollection.addMovie(new ExampleMovie("The Godfather", "1972", "Francis Ford Coppola", 9.2));
        movieCollection.addMovie(new ExampleMovie("The Godfather: Part II", "1974","Francis Ford Coppola", 9.1));
        movieCollection.addMovie(new ExampleMovie("The Dark Knight", "2008", "Christopher Nolan", 9.0));
        movieCollection.addMovie(new ExampleMovie("Schindler's List", "1993", "Steven Spielberg", 8.9));
        createExampleTable();
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("TableModel Example");
        frame.setContentPane(new ExampleForm().rootPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null); // Center on primary monitor
        frame.setVisible(true);
    }

    private void createExampleTable() {
        tableModel = new ExampleMovieCollectionTableModel(movieCollection);
        exampleTable = new JTable(tableModel);
        exampleTable.setPreferredScrollableViewportSize(exampleTable.getPreferredSize());
        exampleTable.changeSelection(0, 0, false, false);
        exampleTable.setAutoCreateRowSorter(true);
        tableScrollPane.setViewportView(exampleTable);
    }

    public static void main(String[] args) {
        createAndShowGui();
    }
}

除了无法设置或检索第 0 列中的复选框状态外,以上操作完美无缺。当我单击带有 System.out.println 的复选框时,我可以确认正在调用 setValueAt() 方法声明,但我不知道如何实际切换选中状态,不幸的是文档没有任何帮助,我发现 answers/tutorials 的 none 涵盖了这个特定问题。

希望我提供了足够的信息,但如果没有,请告诉我,我会相应地编辑问题。

为清楚起见编辑: 在示例代码中,向 ExampleMovie class 添加一个布尔值并将此值反映在 [= 的第 0 列中是微不足道的78=],但不幸的是,这不是我正在使用的实际代码库中的一个选项,所以我想我的具体问题是:有没有办法获取和 set/toggle 第 0 列 [=42] 中的复选框值=]withoutExampleMovie中有相应的数据成员class?如果这不可能,或者实现起来非常困难,谁能告诉我另一种显示数据的方法,这可能会奏效?

第二次编辑:忽略我。实际上,我可以相对轻松地向我正在使用的 class 添加一个新数据成员。我将 Gilbert Le Blanc 的答案推销为正确的。我想这一直是一个重复的问题。对于那个很抱歉!感谢您的帮助,我真的很感激。

谢谢。

您提供的代码异常终止。我将主要 class 的名称从 ExampleForm 更改为 ExampleMovieGUI。

Exception in thread "main" java.lang.NullPointerException
    at com.ggl.testing.ExampleMovieGUI.createExampleTable(ExampleMovieGUI.java:45)
    at com.ggl.testing.ExampleMovieGUI.<init>(ExampleMovieGUI.java:27)
    at com.ggl.testing.ExampleMovieGUI.createAndShowGui(ExampleMovieGUI.java:32)
    at com.ggl.testing.ExampleMovieGUI.main(ExampleMovieGUI.java:49)

修复所有异常结束后,我向 ExampleMovie 添加了一个布尔值 class。这是 GUI 的样子。

我将所有 classes 都放在 classes 中,这样我就可以 post 将完整的可运行代码作为一个块。

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class ExampleMovieGUI {
    
    public static void main(String[] args) {
        ExampleMovieGUI gui = new ExampleMovieGUI();
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("TableModel Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                frame.add(gui.createExampleTable(), BorderLayout.CENTER);
                
                frame.pack();
                frame.setLocationRelativeTo(null); // Center on primary monitor
                frame.setVisible(true);
            }
        });
    }

    private JTable exampleTable;
    private JScrollPane tableScrollPane;
    private ExampleMovieCollection movieCollection;
    private ExampleMovieCollectionTableModel tableModel;

    public ExampleMovieGUI() {
        movieCollection = new ExampleMovieCollection();
        movieCollection.addMovie(new ExampleMovie(
                "The Shawshank Redemption", "1994", "Frank Darabont", 9.3));
        movieCollection.addMovie(new ExampleMovie(
                "The Godfather", "1972", "Francis Ford Coppola", 9.2));
        movieCollection.addMovie(new ExampleMovie(
                "The Godfather: Part II", "1974","Francis Ford Coppola", 9.1));
        movieCollection.addMovie(new ExampleMovie(
                "The Dark Knight", "2008", "Christopher Nolan", 9.0));
        movieCollection.addMovie(new ExampleMovie(
                "Schindler's List", "1993", "Steven Spielberg", 8.9));
    }

    private JPanel createExampleTable() {
        JPanel panel = new JPanel(new BorderLayout());
        
        tableModel = new ExampleMovieCollectionTableModel(movieCollection);
        exampleTable = new JTable(tableModel);
        exampleTable.setPreferredScrollableViewportSize(
                exampleTable.getPreferredSize());
        exampleTable.changeSelection(0, 0, false, false);
        exampleTable.setAutoCreateRowSorter(true);
        tableScrollPane = new JScrollPane(exampleTable);
       
        panel.add(tableScrollPane, BorderLayout.CENTER);
        
        return panel;
    }
    
    public class ExampleMovieCollectionTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        
        private List<ExampleMovie> items;
        private String[] columnNames = {"Checked?", "Title", "Year", 
                "IMDB Score", "Director"};

        public ExampleMovieCollectionTableModel(
                ExampleMovieCollection movieCollection) {
            this.items = new ArrayList<>(movieCollection.getMovies());
        }

        @Override
        public int getRowCount() {
            return  items.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            // Only the first column in the table should be editable
            return columnIndex == 0;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            ExampleMovie item = items.get(rowIndex);
            switch (columnIndex) {
                case 0: return item.isSelected();
                case 1: return item.getTitle();
                case 2: return item.getYear();
                case 3: return item.getScore();
                case 4: return item.getDirector();
                default: return null;
            }
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            // How do I toggle the checked state of the cell when columnIndex == 0?
            System.out.println("(" + rowIndex + ", " + columnIndex + 
                    ") clicked. Value = " + aValue);
            if (columnIndex == 0) {
                ExampleMovie item = items.get(rowIndex);
                item.setSelected(Boolean.valueOf(aValue.toString()));
            }
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Boolean.class;
            } else if (columnIndex == 3) {
                return Double.class;
            }
            return String.class;
        }
    }
    
    public class ExampleMovieCollection {

        private List<ExampleMovie> allMovies;

        public ExampleMovieCollection() {
            allMovies = new ArrayList<>();
        }

        public void addMovie(ExampleMovie movie) {
            allMovies.add(movie);
        }

        public void removeMovie(ExampleMovie movie) {
            if (allMovies.contains(movie)) {
                allMovies.remove(movie);
            }
        }

        public List<ExampleMovie> getMovies() {
            return allMovies;
        }
    }
    
    public class ExampleMovie {
        
        private boolean selected;

        private final String title;
        private final String year;
        private final String director;
        private final double score;

        public ExampleMovie(String title, String year, 
                String director, double score) {
            this.title = title;
            this.year = year;
            this.director = director;
            this.score = score;
            this.selected = false;
        }

        public String getTitle() {
            return title;
        }

        public String getYear() {
            return year;
        }

        public String getDirector() {
            return director;
        }

        public double getScore() {
            return score;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }
        
    }
    
}