這是我的一個JTable代碼,我打算讓有像組合框,JSpinner的,一個JRadioButton,JTextfeiled等JTable中
所有的組件,但我結束了這一點,表頭是沒有出現,我不能編輯列。請指出我的錯誤,因爲我嘗試了很多並放棄了
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.table.*;
public class TableComponent extends JFrame
{
JTable dataTable = null;
public int changeRow = -1, changeColumn = -1;
public JRadioButton radioButton = new JRadioButton();
public void init() {
JPanel upperPanel = setMainPanel();
super.getContentPane().removeAll();
Container content = super.getContentPane();
content.setLayout(new BorderLayout());
content.add(upperPanel, BorderLayout.CENTER);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(800, 400);
setVisible(true);
}
public static void main(String[] args) {
TableComponent tableComponent = new TableComponent();
tableComponent.init();
}
// This function set the main Panel
private JPanel setMainPanel() {
dataTable = createTable();
dataTable = setTableProp(dataTable);
JPanel panel = new JPanel(new BorderLayout(5, 10));
//Get the screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
panel.add(dataTable, BorderLayout.CENTER);
return panel;
}
private JTable createTable()
{
String[] columnName = {"", "Name", "Radio", "Text", "Combo", "Spiner"};
String []s={"01:1mS","02:2mS","04:4mS","06:6mS"};
Object[][] data = new Object[5][6];
for (int i = 0; i < 5; i++) {
data[i][0] = new Boolean(false);
data[i][1] = "Column 1";
data[i][2] = new CustomRadio(false);
data[i][3] = new CustomTextField("Test");
data[i][4] = new CustomCombo(s);
data[i][5] = new CustomSpiner();
}
AbstractTableModel model = new MyTableModel(data, columnName);
JTable table = new JTable(model);
return table;
}
// This function set the child table properties
private JTable setTableProp(JTable table)
{
JTableHeader tableHeader = table.getTableHeader();
tableHeader.setBackground(Color.WHITE);
tableHeader.setForeground(Color.gray);
table.setTableHeader(tableHeader);
TableColumn tc = null;
for (int j = 1; j < table.getColumnCount(); j++) {
tc = table.getColumnModel().getColumn(j);
if (j == 1) {
tc.setCellRenderer(new ColumnRenderer());
} else if (j == 2) {
tc.setCellRenderer(new RadioButtonRenderer());
} else if (j == 3) {
tc.setCellRenderer(new TextFieldRenderer());
} else if (j == 4) {
tc.setCellRenderer(new ComboRenderer());
} else if (j == 5) {
tc.setCellRenderer(new SpinerRenderer());
}
}
return table;
}
//This is the Abstract Table Model class
class MyTableModel extends AbstractTableModel {
private String[] columnName = null;
private Object[][] data = null;
MyTableModel(Object[][] data, String[] columnName) {
this.data = data;
this.columnName = columnName;
}
public int getColumnCount() {
return columnName.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnName[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the first column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
// if (col == 1)
return true;
// else
// return false;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
class ColumnRenderer extends DefaultTableCellRenderer {
public ColumnRenderer() {
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return cell;
}
}
class RadioButtonRenderer implements TableCellRenderer, ItemListener {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
return null;
}
CustomRadio radio = (CustomRadio) value;
return radio;
}
public void itemStateChanged(ItemEvent e) {}
}
class TextFieldRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
return null;
}
CustomTextField customTextField = (CustomTextField) value;
return customTextField;
}
}
class ComboRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
return null;
}
CustomCombo customCombo = (CustomCombo) value;
return customCombo;
}
}
class SpinerRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
return null;
}
CustomSpiner customSpiner = (CustomSpiner) value;
return customSpiner;
}
}
class CustomRadio extends JRadioButton {
public CustomRadio(boolean isSel) {
super();
this.setSelected(isSel);
}
public CustomRadio(ButtonGroup btnGroup, boolean isSel) {
this(isSel);
btnGroup.add(this);
}
}
class CustomTextField extends JTextField {
public CustomTextField(String dataVal) {
super();
}
}
class CustomCombo extends JComboBox {
public CustomCombo(String[] s) {
super(s);
}
}
class CustomSpiner extends JSpinner {
public CustomSpiner() {
super();
}
}
}
表使用的渲染器「橡皮圖章」組件到表 - 他們不是真正的組件。因此,要使表格可編輯,您還必須提供自定義編輯器。試試看[這裏](http://stackoverflow.com/a/577021/1212960)。 –