java - 从 AbstractTableModel 扩展时如何切换 JTable 中复选框的值
问题描述
我希望你能帮助我解决一个困扰我好几天的问题!
我已经阅读了很多来自网络的 SO 答案和各种示例,并且我已尽力阅读 DefaultTableModel 源代码以尝试了解幕后发生的事情,但不幸的是,我仍然完全缺乏更大的图景. 我似乎找不到任何示例、文章或视频来解释TableModel如何或为什么做它所做的事情,以及如何根据我的需要有效地改变它。
我正在做一个项目,我需要在 JTable 中显示与每个对象关联的数据。我有一个 ObjectCollection 类,它将所有对象保存在 ArrayList 内的文件夹中。正是这个集合被传递给我的自定义 TableModel 以填充 JTable。然而,问题是我需要 JTable 有一个包含用户可以与之交互的复选框的列(在本例中为第 0 列)。复选框的状态不是然而,反映在 Object 或 ObjectCollection 中。一旦用户做出任何选择,就会调用一个单独的方法,然后在 JTable 中搜索选中的行并对 ObjectCollection 中的相应对象进行操作 - 因此对象本身和集合都不需要了解复选框。本质上,就好像 TableModel 保存来自两个不同来源的数据——该行中对象的第 1 到 4 列以及该行第 0 列中单元格的选中状态。我希望这是有道理的,但如果不是希望我下面的示例代码能比我的话更好地证明它。
我遇到的问题是双重的。首先,虽然我可以显示复选框并且可以注册对它们的点击,但我不知道如何实际切换复选框的选中状态(当我单击复选框时没有任何反应)。其次,假设我可以弄清楚如何切换复选框状态,我不知道如何使用 getValue() 方法返回它,考虑到它不是 Object 的成员(再次,请参阅下面的示例代码如果我这里不是很清楚)。
由于各种原因,我无法从我的真实项目中发布代码(还),所以我为这个问题创建了一个假的示例项目。原理完全相同,我面临的问题也是如此,所以如果我能在这个例子中解决它,我可以在我的真实代码库中以同样的方式解决它。无论如何,这是我到目前为止的示例代码......
一个简单的示例对象
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;
}
}
对象集合
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() 方法,但我没有知道如何实际切换选中状态,不幸的是,文档没有任何帮助,我找到的答案/教程都没有涵盖这个特定问题。
希望我已经提供了足够的信息,但如果没有,请告诉我,我会相应地编辑问题。
为清楚起见进行编辑:在示例代码中,将布尔值添加到 ExampleMovie 类并将此值反映在表的第 0 列中是微不足道的,但不幸的是,这不是我正在使用的实际代码库中的选项所以我猜我的具体问题是:有没有办法在没有ExampleMovie 类中的相应数据成员的情况下获取和设置/切换第 0 列中的复选框值?如果这是不可能的,或者很难实现,那么任何人都可以向我指出另一种显示数据的方法来解决问题吗?
第二次编辑:忽略我。实际上,我可以相对轻松地将新数据成员添加到我正在使用的类中。我认为吉尔伯特勒布朗的答案是正确的。我想这一直是一个重复的问题。对于那个很抱歉!感谢您的帮助,我真的很感激。
谢谢。
解决方案
您提供的代码异常终止。我将主类的名称从 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 类添加了一个布尔值。这是 GUI 的样子。
我将所有类都设为内部类,这样我就可以将完整的可运行代码作为一个块发布。
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;
}
}
}
推荐阅读
- sql - 在 Laravel 中计算单个列的不同值
- pyspark - 在 pyspark 中删除所有重复的实例
- apache - .htaccess 用于 SPA,将传入请求重写为 index.html,静态资源除外
- c++ - 将对象的字节复制到数组并再次返回是否安全
- c# - Select语句在db C#中没有得到任何数据
- java - 交易标记为仅回滚,原因不明
- javascript - 使用 C# 发送 JS 对象的 HTTP 响应不起作用
- shell - 如何检查子目录的读写执行权限?
- neo4j - 如何在 NEO4J 的路径中获取不同的节点?
- python - 具有多个元素的数组的真值是不明确的。使用 ReLu 函数使用 a.any() 或 a.all() python numpy