Java Jtable-特殊列
我需要创建列类型和值的Jtable。该值可以是true、false或number。这就是为什么我想在一列中显示复选框或文本。谢谢大家! 以下给出了一种允许在同一列中使用不同数据类型的方法:Java Jtable-特殊列,java,swing,jtable,Java,Swing,Jtable,我需要创建列类型和值的Jtable。该值可以是true、false或number。这就是为什么我想在一列中显示复选框或文本。谢谢大家! 以下给出了一种允许在同一列中使用不同数据类型的方法: import java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class TablePropertyEditor exte
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);
}
}
基本上,您需要使用基于数据的呈现器/编辑器,而不是列类型。您尝试过什么吗?是的,我尝试过编辑列管理器。它不起作用。这一点信息并不能告诉我们多少我们可以使用的东西,这将使这个问题更容易回答或质量更高。请多讲,多解释,包括展示你的照片。另外,请阅读的如何提问部分以及。