swing jtable 添加多选框

n年没有碰swing开发了 Jtable中加一个下checkbox 多选框 这个还真的记录一下 ~
import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class ChecBoxTest {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Editable Color Table");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ColorTableModel model = new ColorTableModel();
		Vector<Object> rowData = new Vector<Object>();
		rowData.add(new Object[] { "1", Boolean.TRUE });
		rowData.add(new Object[] { "2", Boolean.TRUE });
		rowData.add(new Object[] { "3", Boolean.FALSE });
		
		model.setRowData(rowData);
		JTable table = new JTable(model);
		JScrollPane scrollPane = new JScrollPane(table);
		frame.add(scrollPane, BorderLayout.CENTER);
		frame.setSize(400, 150);
		frame.setVisible(true);
	}
}

class ColorTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Vector<Object> rowData;

	public Vector<Object> getRowData() {
		return rowData;
	}

	public void setRowData(Vector<Object> rowData) {
		this.rowData = rowData;
	}

	String columnNames[] = { "English", "fdsa" };

	public int getColumnCount() {
		return columnNames.length;
	}

	public String getColumnName(int column) {
		return columnNames[column];
	}

	public int getRowCount() {
		return rowData.size();
	}

	public Object getValueAt(int row, int column) {
		Object[] obj = (Object[]) rowData.get(row);
		return obj[column];
	}

	@SuppressWarnings("rawtypes")
	public Class getColumnClass(int column) {
		return (getValueAt(0, column).getClass());
	}

	public void setValueAt(Object value, int row, int column) {
		Object[] obj = (Object[]) rowData.get(row);
		obj[column] = value;
	}

	public boolean isCellEditable(int row, int column) {
		return (column != 0);
	}
}




如何刷新 JTABLE
this.xxTable.updateUI();


 

你可能感兴趣的:(java,swing)