无法选中复选框-它们是可编辑的! Java秋千 [英] Check Boxes not able to be Selected -- They are Editable! Java Swing
问题描述
我已经在这个网站上阅读了很多有关此问题的问题,但找不到为什么它仍然无法工作.
I have read so many questions on this website concerning this problem and I cannot find why it is still not working.
问题:
我制作了一个JTable
以显示在JScrollPane
内部. JTable
的构造如下:
I made a JTable
to be displayed inside a JScrollPane
. The JTable
is constructed as follows:
table = new JTable(new DataTableModel());
如您所见,我使用了名为DataTableModel
的自定义AbstractDataModel
.现在,当我显示此复选框时,但无法选中它们.您可以在下面看到它们的可编辑性.这是DataTableModel
类中的相关代码:(请注意,我用于复选框的列是第一列,索引为0,并且此列中数组中的数据为"null").对于某些
As you can see, I use a custom AbstractDataModel
called DataTableModel
. Now, when I display this the checkboxes appear, but they are not able to be selected. They are editable as you can see below. Here is the pertinent code in the DataTableModel
class: (note that my column for check boxes is the first column, at index 0, and that my data in my array at this column is "null"). For some
public class DataTableModel extends AbstractTableModel
{
private String[][] data;
private String[] header =
{ "", "KB Name", "fpGUID" };
public DataTableModel() throws SQLException
{
// ========= CONNECTS TO DB AND PULLS RESULTS ==========
// GETS RESULTS SET CALLED "rs"
// populate data array
int counter = 0;
while (rs.next())
{
//data[counter][0] = "sfsdfsdfs ";
data[counter][1] = (String) rs.getObject(2);
data[counter][2] = (String) rs.getObject(4);
counter++;
}
// =====================================================
}
@Override
public String getValueAt(int rowIndex, int columnIndex)
{
return data[rowIndex][columnIndex];
}
public boolean isCellEditable(int row, int col)
{
return col == 0;
}
public Class getColumnClass(int column)
{
if (column == 0)
{
return Boolean.class;
} else
{
return String.class;
}
}
所以,看来我的getColumnClass()
方法很好,那是什么问题呢?可能与我存储表数据的数据"有关.这是数据数组:
So, it seems my getColumnClass()
method is fine, so what is the problem? Could it be something with my "data" which I store the data for the table. Here is the data array:
推荐答案
有很多事情可以跳出来...
There are a number of things that jump out...
- (正如已经开始指出的那样)
getValueAt
似乎被声明为错误.它应该返回Object
.想一想.您的期望是第一列将是boolean
,因此如何从该方法返回String
...这将导致... - 您的
data
数组是String
的数组...在这里,再次期望第一个值应为boolean
.现在您实际上可以保留它,但是您需要将值从getValueAt
方法转换为boolean
,然后再次返回setValueAt
方法...这将导致... - 您尚未实现
setValueAt
,因此无法存储单元格值的更改 - 您还没有实现必需的
getRowCount
,不是必需的getColumnCount
,并且应该实现getColumnName
,因为您实际上有一些.
- (As has already begin pointed out)
getValueAt
appears to be declared wrong. It should be returningObject
. Think about it for a second. Your expectation is that the first column will beboolean
, so how can you returnString
from this method...which leads to... - Your
data
array is an array ofString
...where, again, the expectation is that the first value should beboolean
. Now you can actually keep this, but you will need to translate the value toboolean
from thegetValueAt
method and back again for thesetValueAt
method...which leads to... - You've not implemented
setValueAt
, so there is no way that the changes to the cell value can be stored - You're also not implementing
getRowCount
, which is required,getColumnCount
, which is required and you should implementgetColumnName
, since you actually have some.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class TestTableModel02 {
public static void main(String[] args) {
new TestTableModel02();
}
public TestTableModel02() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new JTable(new DataTableModel())));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DataTableModel extends AbstractTableModel {
private Object[][] data;
private String[] header = {"", "KB Name", "fpGUID"};
public DataTableModel() {//throws SQLException {
// ========= CONNECTS TO DB AND PULLS RESULTS ==========
// GETS RESULTS SET CALLED "rs"
// populate data array
// int counter = 0;
// while (rs.next()) {
// //data[counter][0] = "sfsdfsdfs ";
// data[counter][1] = (String) rs.getObject(2);
// data[counter][2] = (String) rs.getObject(4);
//
// counter++;
// }
// =====================================================
data = new Object[4][3];
data[0] = new Object[]{false, "Help", "1234"};
data[1] = new Object[]{false, "On fire", "5648"};
data[2] = new Object[]{false, "Drowning", "9012"};
data[3] = new Object[]{false, "Micky Mouse", "3456"};
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public boolean isCellEditable(int row, int col) {
return col == 0;
}
@Override
public Class getColumnClass(int column) {
if (column == 0) {
return Boolean.class;
} else {
return String.class;
}
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
if (aValue instanceof Boolean) {
data[rowIndex][columnIndex] = (Boolean)aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return header.length;
}
@Override
public String getColumnName(int column) {
return header[column];
}
}
}
现在,有很多方法可以解决整个第一列必须是布尔值"问题,但是坦率地说,这样做的代码要少得多.
Now, there are ways you can get around the whole "first column must be boolean" issue, but to be frank, it's much less code to do it this way.
我也强烈建议您查看SwingWorker
并使用它进行所有数据加载,否则您可能会发现在开始加载数据时UI会停止运行,这通常是相当不愉快.
I would also, strongly, recommend that you take a look at SwingWorker
and do all your data loading using it, otherwise you may find that your UI comes to a grinding halt while the data is begin loaded, which is generally rather unpleasant.
看看...
- Concurrency in Swing
- How to use Tables
了解更多详情
这篇关于无法选中复选框-它们是可编辑的! Java秋千的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!