jtable 双表头的实现

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

双表头写在model里实现

这是父类的实现:

public abstract class AbstractMatriculateResultTableModel extends AbstractTableModel {

protected Object[][] dataMesh = new Object[0][0];

public abstract int getGroupColumnCounts();

@Override
public boolean isCellEditable(int row, int column) {
return false;
}

public int getRowCount() {
return dataMesh.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
Object obj = dataMesh[rowIndex][columnIndex];
return obj;
}

/**
* 获取组名
* @param idx 组索引,不是列索引
* @return
*/
public abstract String getGroupColumnName(int idx);

/**
* 获取某一个合成表头的子列个数
* @param column 合成表头列索引(非整个表中的列索引)
* @return
*/
public abstract int getGroupColumnSubCounts(int column);

/**
* 获取表格前面的简单表头列数
* @return
*/
public abstract int getSingleColumnCount();

/**
* 获取表格结尾出简单表头列数
*
*/
public abstract int getSuffixSingleColumnCount();

/**
* 根据组编号,组内索引,转换层对应的表格基础列索引
* @param groupIndex
* @param index
* @return
*/
public abstract int changeToTotalColumnIndex(int groupIndex, int index);
}

这是子类的实现:

public class MatriculateResultTableModel extends AbstractMatriculateResultTableModel{

private String[] columns = new String[]{"高考","入学","免试","志愿",""};

private String[] singleColumns = {"考号", "姓名","性别"};
private String[] subColumnsCee = {"语文","数学","英语","综合"};
private String[] subColumnsEntrance = {"面试","口试","笔试"};
private String[] subColumnsFree = {"托福","雅思"};
private String[] subColumnsChoice = {"第一志愿","第二志愿","第三志愿","是否服从项目调配"};
private String[] subColumnsOthers = {"意见"};

private List allColumns = new ArrayList();
private List groupColumns = new ArrayList();
private List groupChildSize = new ArrayList();

public static final int ENROLLSIGNUPCODE = 0;
public static final int NAME = 1;
public static final int SEX = 2;

private List mcrcList = new ArrayList();


public MatriculateResultTableModel(){
allColumns.clear();
groupColumns.clear();
groupChildSize.clear();

Map temp = new HashMap();

for (String objects : singleColumns) {
allColumns.add(objects);
}

for (int i = 0; i < columns.length; i ++) {
String s = columns[i];

if(i == 0){
for (String obj : subColumnsCee) {
allColumns.add(obj);
}
groupColumns.add(s);
groupChildSize.add(4);
}else if(i == 1){
for (String obj : subColumnsEntrance) {
allColumns.add(obj);
}
groupColumns.add(s);
groupChildSize.add(3);
}else if(i == 2){
for (String obj : subColumnsFree) {
allColumns.add(obj);
}
groupColumns.add(s);
groupChildSize.add(2);
}else if(i == 3){
for (String obj : subColumnsChoice) {
allColumns.add(obj);
}
groupColumns.add(s);
groupChildSize.add(4);
}else if(i == 4){
for (String obj : subColumnsOthers) {
allColumns.add(obj);
}
groupColumns.add(s);
groupChildSize.add(1);
}
}
// }
}

public int getSingleColumnWidth(int column) {
switch (column) {
case ENROLLSIGNUPCODE:
return 73;
case NAME:
return 100;
case SEX:
return 48;
default:
return 74;
}
}

public void setMcrcList(List list){
mcrcList.clear();
mcrcList.addAll(list);
setList(mcrcList);
fireTableDataChanged();
}

public void setList(List list){
//填充数据
dataMesh = new Object[mcrcList.size()][allColumns.size()];
for (int i = 0; i < mcrcList.size(); i++) {
MatriculateResultComposite matriculateResultComp = mcrcList.get(i);
if(null != matriculateResultComp.getMatriculateResult()){
dataMesh[i][ENROLLSIGNUPCODE] = matriculateResultComp.getMatriculateResult().getEnrollSingUpCode();
}
if(null != matriculateResultComp.getEnrollSignUpForm()){
dataMesh[i][NAME] = matriculateResultComp.getEnrollSignUpForm().getName();
if(EnrollSignUpForm.SEX_FEMALE.equals(matriculateResultComp.getEnrollSignUpForm().getSex())){
dataMesh[i][SEX] = "女";
}else if(EnrollSignUpForm.SEX_MALE.equals(matriculateResultComp.getEnrollSignUpForm().getSex())){
dataMesh[i][SEX] = "男";
}
}

for(int k = 0; k < columns.length; k ++){
String s = columns[k];
int j = groupColumns.indexOf(s);
if(k == 0){

if(null != matriculateResultComp.getEnrollSignUpForm()){
Object chineseScore = matriculateResultComp.getEnrollSignUpForm().getCeeChineseScore();
if (null != chineseScore && !"".equals(chineseScore) && Double.parseDouble(chineseScore.toString()) >= 0) {
dataMesh[i][changeToTotalColumnIndex(j, 0)] = ScoreTool.double2string(Double.parseDouble(chineseScore.toString()));
}else{
dataMesh[i][changeToTotalColumnIndex(j, 0)] = "";
}
Object mathScore = matriculateResultComp.getEnrollSignUpForm().getCeeMathScore();
if(null != mathScore && !"".equals(mathScore) && Double.parseDouble(mathScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 1)] = matriculateResultComp.getEnrollSignUpForm().getCeeMathScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 1)] = "";
}
Object englishScore = matriculateResultComp.getEnrollSignUpForm().getCeeEnglishScore();
if(null != englishScore && !"".equals(englishScore) && Double.parseDouble(englishScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 2)] = matriculateResultComp.getEnrollSignUpForm().getCeeEnglishScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 2)] = "";
}
Object multipleScore = matriculateResultComp.getEnrollSignUpForm().getCeeMultipleScore();
if(null != multipleScore && !"".equals(multipleScore) && Double.parseDouble(multipleScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 3)] = matriculateResultComp.getEnrollSignUpForm().getCeeMultipleScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 3)] = "";
}
}
}else if(k == 1){
if(null != matriculateResultComp.getScore()){
Object auditionExamScore = matriculateResultComp.getScore().getAuditionExamScore();
if(null != auditionExamScore && !"".equals(auditionExamScore) && Double.parseDouble(auditionExamScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 0)] = matriculateResultComp.getScore().getAuditionExamScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 0)] = "";
}
Object oralExamScore = matriculateResultComp.getScore().getOralExamScore();
if(null != oralExamScore && !"".equals(oralExamScore) && Double.parseDouble

(oralExamScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 1)] = matriculateResultComp.getScore().getOralExamScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 1)] = "";
}
Object writtenExamScore = matriculateResultComp.getScore().getWrittenExamScore();
if(null != writtenExamScore && !"".equals(writtenExamScore) && Double.parseDouble(writtenExamScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 2)] = matriculateResultComp.getScore().getWrittenExamScore();
}else{
dataMesh[i][changeToTotalColumnIndex(j, 2)] = "";
}
}
}else if(k == 2){
if(null != matriculateResultComp.getEnrollSignUpForm()){
Object toeflTotalScore = matriculateResultComp.getEnrollSignUpForm().getToeflTotalScore();
if(null != toeflTotalScore && !"".equals(toeflTotalScore) && Double.parseDouble(toeflTotalScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 0)] = ScoreTool.double2string(matriculateResultComp.getEnrollSignUpForm().getToeflTotalScore());
}else{
dataMesh[i][changeToTotalColumnIndex(j, 0)] = "";
}
Object ieltsTotalScore = matriculateResultComp.getEnrollSignUpForm().getIeltsTotalScore();
if(null != ieltsTotalScore && !"".equals(ieltsTotalScore) && Double.parseDouble(ieltsTotalScore.toString()) >= 0){
dataMesh[i][changeToTotalColumnIndex(j, 1)] = ScoreTool.double2string(matriculateResultComp.getEnrollSignUpForm().getIeltsTotalScore());
}else{
dataMesh[i][changeToTotalColumnIndex(j, 1)] = "";
}
}
}else if(k == 3){
if(null != matriculateResultComp.getEnrollSignUpForm()){
dataMesh[i][changeToTotalColumnIndex(j, 0)] = matriculateResultComp.getEnrollSignUpForm().getFirstChoiceCode();
dataMesh[i][changeToTotalColumnIndex(j, 1)] = matriculateResultComp.getEnrollSignUpForm().getSecondChoiceCode();
dataMesh[i][changeToTotalColumnIndex(j, 2)] = matriculateResultComp.getEnrollSignUpForm().getThirdChoiceCode();
if(false == matriculateResultComp.getEnrollSignUpForm().isChoiceAdjustable()){
dataMesh[i][changeToTotalColumnIndex(j, 3)] = "×";
}else if(true == matriculateResultComp.getEnrollSignUpForm().isChoiceAdjustable()){
dataMesh[i][changeToTotalColumnIndex(j, 3)] = "√";

}
}
}else if(k == 4){
if(null != matriculateResultComp.getMatriculateResult()){
dataMesh[i][changeToTotalColumnIndex(j, 0)] = matriculateResultComp.getMatriculateResult().getFinalOpinionCode();
}
}
}
}
}

@Override
public String getColumnName(int column) {
return allColumns.get(column);
}

@Override
public int getGroupColumnCounts() {
return groupColumns.size();
}

@Override
public String getGroupColumnName(int idx) {
return groupColumns.get(idx);
}

@Override
public int getGroupColumnSubCounts(int column) {
return groupChildSize.get(column);
}

@Override
public int getSingleColumnCount() {
return singleColumns.length;
}

@Override
public int getSuffixSingleColumnCount() {
return 0;
}

@Override
public int changeToTotalColumnIndex(int groupIndex, int index) {
int k = 0;
for (int i = 0; i < groupIndex; i++) {
k += groupChildSize.get(i);
}
return singleColumns.length + k + index;
}

public int getColumnCount() {
return allColumns.size();
}

public MatriculateResultComposite getMatriculateResultCom(int idx){
return mcrcList.get(idx);
}

public void addMatriculateComp(int idx,MatriculateResultComposite mcrc){
mcrcList.add(idx,mcrc);
setList(mcrcList);
fireTableRowsInserted(idx, idx);
}

public void updateMatriculateComp(MatriculateResultComposite mcrc){
int idx = mcrcList.indexOf(mcrc);
mcrcList.set(idx, mcrc);
setList(mcrcList);
fireTableRowsUpdated(idx, idx);
}

public void deleteMatriculateComp(MatriculateResultComposite mcrc){
int idx = mcrcList.indexOf(mcrc);
mcrcList.remove(idx);
setList(mcrcList);
fireTableRowsDeleted(idx, idx);
}

public List getList(){
return mcrcList;
}

}



在面板里用JNTable实现:



//这是两个JNTable,columnTable 用于显示双表头的table,headerTable 用于显示单表头的table,这两个table共同显示在一个table中

private JNTable columnTable = new JNTable();
private JNTable headerTable = new JNTable();



//这个是监听选中行改变的事件

columnTable.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
selectingMatriculateResult();
}
}
});



//初始化表头的方法

private void initTableHeader() {
matriculateResultTableModel = new MatriculateR

esultTableModel();
columnTable.getTable().setDefaultRenderer(Object.class, new MatriculateTableRenderer(matProjectMap, majorMap));
columnTable.setModel(matriculateResultTableModel);
TableSettingTool.setTable(columnTable.getTable());
columnTable.getTable().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
columnTable.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
selectingMatriculateResult();
int i = columnTable.getTable().getSelectedRow();
if (i == -1) {
headerTable.getTable().clearSelection();
} else {
headerTable.getTable().getSelectionModel().
setSelectionInterval(
i, i);
}
}
});
headerTable.setModel(matriculateResultTableModel);
TableSettingTool.setTable(headerTable.getTable());
headerTable.getTable().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
headerTable.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
selectingMatriculateResult();
int i = headerTable.getTable().getSelectedRow();
if (i == -1) {
columnTable.getTable().clearSelection();
} else {
columnTable.getTable().getSelectionModel().setSelectionInterval(i, i);
}
}
});
}



//合成表头的方法

private void setTableHeader() {
//生成合成表头
TableColumnModel columnModel = columnTable.getTable().getColumnModel();
ColumnGroupTableHeader header = new ColumnGroupTableHeader(columnModel);
for (int i = 0; i < matriculateResultTableModel.getGroupColumnCounts(); i++) {
ColumnGroup group = new ColumnGroup(matriculateResultTableModel.getGroupColumnName(i));
int subCount = matriculateResultTableModel.getGroupColumnSubCounts(i);
for (int k = 0; k < subCount; k++) {
group.add(columnModel.getColumn(matriculateResultTableModel.changeToTotalColumnIndex(i, k)));
}
header.addColumnGroup(group);
}
columnTable.getTable().setTableHeader(header);

//锁定左侧列,只滚动右侧列
TableColumnModel columnModel2 = headerTable.getTable().getColumnModel();
for (int i = 0; i < matriculateResultTableModel.getSingleColumnCount(); i++) {
TableColumn tc = columnModel2.getColumn(i);
tc.setPreferredWidth(matriculateResultTableModel.getSingleColumnWidth(i));
tc.setMinWidth(matriculateResultTableModel.getSingleColumnW

idth(i));
tc.setMaxWidth(matriculateResultTableModel.getSingleColumnWidth(i));
}
for (int i = columnModel2.getColumnCount() - 1; i >=
matriculateResultTableModel.getSingleColumnCount(); i--) {
headerTable.getTable().getColumnModel().removeColumn(columnModel2.getColumn(i));
}
JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, columnTable.getTable());
for (int i = matriculateResultTableModel.getSingleColumnCount() - 1; i >=
0; i--) {
columnTable.getTable().getColumnModel().removeColumn(columnTable.getTable().getColumnModel().getColumn(i));
}
scrollPane.setRowHeaderView(headerTable.getTable());
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,
headerTable.getTable().getTableHeader());

//
columnTable.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
columnTable.getTable().getTableHeader().setReorderingAllowed(false);
columnTable.getTable().setSortable(false);
headerTable.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.getTable().getTableHeader().setReorderingAllowed(false);
headerTable.getTable().setSortable(false);
}



这是renderer,只是用于在table里把成绩根据不同分数段绘成不同的颜色

public class MatriculateTableRenderer extends JLabel implements TableCellRenderer, Serializable {
private Map matProjectMap = new HashMap();
private Map matMajorMap = new HashMap();

public MatriculateTableRenderer(Map projectMap, Map majorMap) {
this.matProjectMap = projectMap;
this.matMajorMap = majorMap;
setOpaque(true);
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Color fg = null;
Color bg = null;
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null && !dropLocation.isInsertRow() &&
!dropLocation.isInsertColumn() && dropLocation.getRow() == row &&
dropLocation.getColumn() == column) {

fg = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
isSelected = true;
}

if (isSelected) {
super.setForeground(fg == null ? table.getSelectionForeground()
: fg);
super.setBackground(bg == null ? table.getSelectionBackground()
: bg);
} else {
super.setForeground(table.getForeground());
super.setBackground(table.getBackground());
}

setFont(table.getFont());
setText((value == null) ? "" : value.toString());

String firstChoic

e = (String) table.getModel().getValueAt(row, 12);
MatriculateProject matProject = (MatriculateProject) matProjectMap.get(firstChoice);
Set set = matMajorMap.keySet();
for (Object key : set) {
if (null != key) {
Major major = (Major) matMajorMap.get(key);
if (null != major && major.getCode().equals(firstChoice)) {
matProject = (MatriculateProject) matProjectMap.get(major.getProjectCode());
break;
}
}
}
switch (column) {
case 0:
case 1:
case 2:
case 3:
if (null != value && !"".equals(value) &&
Double.parseDouble(value.toString()) > 0) {
setText(ScoreTool.double2string(Double.parseDouble(value.toString())));
if (matProject != null &&
matProject.getCeeScoreConditionalLine() <
ScoreTool.string2double(value.toString()) &&
matProject.getCeeScoreNormalLine() >=
ScoreTool.string2double(value.toString())) {
setForeground(Color.blue);
} else if (matProject != null &&
matProject.getCeeScoreConditionalLine() >
ScoreTool.string2double(value.toString())) {
setForeground(Color.red);
}
} else {
setText("");
}
break;
case 4:
case 5:
case 6:
if (null != value && !"".equals(value) &&
Double.parseDouble(value.toString()) > 0) {
setText(ScoreTool.double2string(Double.parseDouble(value.toString())));
if (matProject != null &&
matProject.getEntranceScoreConditionalLine() <
ScoreTool.string2double(value.toString()) &&
matProject.getEntranceScoreNormalLine() >=
ScoreTool.string2double(value.toString())) {
setForeground(Color.blue);
} else if ((matProject != null &&
matProject.getEntranceScoreConditionalLine() >
ScoreTool.string2double(value.toString()))) {
setForeground(Color.red);
}
} else {
setText("");
}
break;
}
return this;
}
}

看看效果吧:



滚动条的意思是双表头是可以左右滚动的


相关文档
最新文档