我的GUI中有一个(或多或少)空JTable,开头没有任何JCheckBox。按下按钮后,我想根据数组的内容用名称(字符串)和JCheckBoxes填充表格。具有名称的部分已经正常工作,但是我无法将单个单元格的类更改为布尔值,而是在JutckBox实际应该在的JTable中出现true或false。我知道如果在开头将列类型设置为boolean,则可以用JCheckBoxes填充完整的列。
我现在的问题是,是否可以更改单个单元格的列类型。
这是创建JTable的代码:
table = new JTable();
table.setRowMargin(3);
table.setRowHeight(25);
table.setFillsViewportHeight(true);
table.setModel(new DefaultTableModel(
new Object[][] {
{null, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"},
{"", "2016-02-29", "2016-03-01", "2016-03-02", "2016-03-03", "2016-03-04"},
{" 1. Lesson", null, null, null, null, null},
{" 2. Lesson", null, null, null, null, null},
{" 3. Lesson", null, null, null, null, null},
{" 4. Lesson", null, null, null, null, null},
{" 5. Lesson", null, null, null, null, null},
{" 6. Lesson", null, null, null, null, null},
{" 7. Lesson", null, null, null, null, null},
{" 8. Lesson", null, null, null, null, null},
{" 9. Lesson", null, null, null, null, null},
{" 10. Lesson", null, null, null, null, null},
},
new String[] { "Empty", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }
)
{
Class[] columnTypes = new Class[]
{
String.class, Object.class, Object.class, Object.class, Object.class, Object.class
};
public Class getColumnClass(int columnIndex)
{
return columnTypes[columnIndex];
}
});
table.setBorder(new LineBorder(new Color(0, 0, 0)));
table.setBounds(381, 11, 518, 300);
panel.add(table);
这是JTable内容发生变化的部分:
public void tableUpdate(Object[][] a)
{
for(int row = 0; row < 11; row++)
{
for(int column = 0 ; column < 5; column++)
{
table.setValueAt(a[column][row], row+1, column+1);
}
}
}
如果您需要进一步的信息,请提前问我并多多谢意。
答案 0 :(得分:1)
你可以这样做:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TablePropertyEditor extends JFrame
{
public TablePropertyEditor()
{
String[] columnNames = {"Type", "Value"};
Object[][] data =
{
{"String", "I'm a string"},
{"Date", new Date()},
{"Integer", new Integer(123)},
{"Double", new Double(123.45)},
{"Boolean", Boolean.TRUE}
};
JTable table = new JTable(data, columnNames)
{
private Class editingClass;
public TableCellRenderer getCellRenderer(int row, int column)
{
editingClass = null;
int modelColumn = convertColumnIndexToModel(column);
if (modelColumn == 1)
{
Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
return getDefaultRenderer( rowClass );
}
else
return super.getCellRenderer(row, column);
}
public TableCellEditor getCellEditor(int row, int column)
{
editingClass = null;
int modelColumn = convertColumnIndexToModel(column);
if (modelColumn == 1)
{
editingClass = getModel().getValueAt(row, modelColumn).getClass();
return getDefaultEditor( editingClass );
}
else
return super.getCellEditor(row, column);
}
// This method is also invoked by the editor when the value in the editor
// component is saved in the TableModel. The class was saved when the
// editor was invoked so the proper class can be created.
public Class getColumnClass(int column)
{
return editingClass != null ? editingClass : super.getColumnClass(column);
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
public static void main(String[] args)
{
TablePropertyEditor frame = new TablePropertyEditor();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
它根据单元格中的数据而不是列类确定要使用的渲染器/编辑器。