本文介绍了我怎么会去JTable中改变布尔字符串到JCheckBox的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧。我实现了自定义的JTable
模型,其中包括全
Alright. I have implemented a custom JTable
model that includes the whole
@Override
public Class<?> getColumnClass(final int column) {
和里面我有
if (column == 0)
return Boolean.class;
当我跑了,我去了的JTable
,而不是检查 JCheckBox的
,它说真的。当我点击它,它变成了一个 JCheckBox的
,直到我取消勾选,然后或者说真的还是假的。
When I run, and I go to the JTable
, instead of a checked JCheckbox
, it says true. When I click on it, it turns into a JCheckBox
until I unclick and then either says true or false.
我究竟做错了!! ???
WHAT AM I DOING WRONG!!???
推荐答案
真的不知道,你可以比较你的code以
really don't know, you can compare your code with
注意,Column.Class两个定义是有效的,你可以尝试与取消注释受阻code
note both definitions for Column.Class are valid, you can try it that with uncomment blocked code
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TablePrepareRenderer extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TablePrepareRenderer() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
/*@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}*/
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Integer.class;
case 3:
return Double.class;
default:
return Boolean.class;
}
}
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int firstRow = 0;
int lastRow = table.getRowCount() - 1;
if (row == lastRow) {
((JComponent) c).setBackground(Color.red);
} else if (row == firstRow) {
((JComponent) c).setBackground(Color.blue);
} else {
((JComponent) c).setBackground(table.getBackground());
}
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TablePrepareRenderer frame = new TablePrepareRenderer();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这篇关于我怎么会去JTable中改变布尔字符串到JCheckBox的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!