⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studenttable.java

📁 一个可以存储和打印成绩单的系统
💻 JAVA
字号:
package org.signsmile.view.student;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import org.minjey.cjsjk.model.Major;

public class StudentTable extends JTable implements FocusListener {
	private static final long serialVersionUID = 1L;

	int focusRow;
	int focusColumn;
	boolean isfocus;
	Major major;

	public StudentTable(TableModel tableModel) {
		super(tableModel);
		
		this.addFocusListener(this);
		this.setAutoResizeMode(AUTO_RESIZE_OFF);
		this.getTableHeader().setReorderingAllowed(false);
	}

	public void setTableView() {
		TableColumnModel tcm = this.getColumnModel();

		// 设置第一列列宽
		TableColumn tc = tcm.getColumn(0);
		tc.setMaxWidth(5);
		tc.setPreferredWidth(tc.getMinWidth());
		
		// 设置第一列列颜色
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
			private static final long serialVersionUID = 1L;

			@Override
			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);
				if (column == 0) // 设置变色的单元格
					cell.setBackground(tableHeader.getBackground());
				else {
					if (row != table.getRowCount() - 1){
						//cell.setBackground(new Color(200, 255, 200));
					} else {
						cell.setBackground(Color.white);
					}

				}
				return cell;
			}
		};
		this.setDefaultRenderer(this.getColumnClass(0), tcr);

	}

	@Override
	public void focusGained(FocusEvent e) {
		if (isfocus) {
			System.out.println("setfocus");
			this.changeSelection(focusRow, focusColumn, false, false);
			this.editCellAt(focusRow, focusColumn);
			isfocus = false;
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
	}

	public void setfocus(int row, int col) {
		focusRow = row;
		focusColumn = col;
		isfocus = true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -