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

📄 classtable.java

📁 JAVA CLONE 用于CLASS范式的CLONE
💻 JAVA
字号:
package client;

import java.awt.Color;
import java.awt.Dimension;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import baseClass.Class;
import baseClass.TableRender;

public class ClassTable extends JTable {

	String titles[];

	Object[][] classsdate;

	DefaultTableModel dm;

	ListSelectionModel selectionmode = null;

	public static ArrayList ALLCLASS;
	UIManager uimanager;
	public ClassTable(UIManager arguimanager) {
		super();
		uimanager=arguimanager;
		//ALLCLASS=ManagerSystem.ALLCLASS;
		dateBuild();
		//selectionmode=this.getSelectionModel();
		//selectionmode.addListSelectionListener(this);
		//this.setCellSelectionEnabled(true);
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setPreferredScrollableViewportSize(new Dimension(400, 150));
		this.setSelectionBackground(Color.black);
		this.setSelectionForeground(Color.white);
	}

	/**
	 * load classtable date
	 * 
	 * @return all date of class
	 */
	public Object[][] getclassdate() {
		ALLCLASS = getclassarraycontent();
		Object[][] results = new Object[ALLCLASS.size()][2];
		for (int i = 0; i < ALLCLASS.size(); i++) {
			Class tempclass = ((Class) ALLCLASS.get(i));
			results[i][0] = tempclass.getClassId();
			results[i][1] = tempclass.getClassName();
		}
		return results;
	}

	/**
	 * bound date on the table
	 */
	public void dateBuild() {
		titles = new String[] { "classid", "classname" };
		classsdate = getclassdate();
		dm = new DefaultTableModel();
		dm.setDataVector(classsdate, titles);
		this.setModel(dm);
		TableRender.makeFace(this);
	}

	/**
	 * return the select class id
	 * 
	 * @param row
	 *            select row number
	 * @return class id
	 */
	public String getclassid(int row) {
		Class temp = (Class) ALLCLASS.get(row);
		return temp.getClassId();
	}

	/**
	 * Changes a cell data need to update the  interface and ALLCLASS
	 */
	public void setValueAt(Object aValue, int row, int column) {
		Class temp = (Class) ALLCLASS.get(row);
		if (column == 0)//modyfy the first row
		{
			try {
				int id = Integer.parseInt(aValue.toString());
				String oldid = temp.getClassId();
				temp.setClassId(aValue.toString());
				if (new Class().isclassidexsitclassarray(ALLCLASS, id) > 1) {
					JOptionPane.showMessageDialog(this, "id exsit");
					temp.setClassId(oldid);//cancle the modify
					//dm.setValueAt(0, row, column);
				} else {
					temp.setClassId(aValue.toString());//Id must be amended to delete time will not go wrong
					dm.setValueAt(aValue, row, column);
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "id must be a number");
			}
		} else//change other row
		{
			temp.setClassName(aValue.toString());
			dm.setValueAt(aValue, row, column);
		}
	}

	/**
	 * Add a line of data, need to update data and interface ALLCLASS
	 * 
	 * @param rowData
	 */
	public void addRow(Object[] rowData) {
		ALLCLASS.add(new Class(rowData[0].toString(), rowData[1].toString()));
		dm.addRow(rowData);
	}

	public void savedate() {
		Class.saveAllClass(ALLCLASS);
	}

	public void removerow() {
		int selectrow = this.getSelectedRow();
		if (selectrow != -1) {
			ALLCLASS.remove(selectrow);
			dm.removeRow(selectrow);
		}
	}

	public void mutiremoverow() {
		int[] selectrows = this.getSelectedRows();
		String[] selectid = getselectids(selectrows);
		if (selectrows != null) {
			for (int i = 0; i < selectrows.length; i++) {
				removeclassfromarraybyid(ALLCLASS, selectid[i]);
				dm.removeRow(selectrows[i]);
			}
		}
	}

	public String[] getselectids(int[] rowids) {
		String[] temp = new String[rowids.length];
		for (int i = 0; i < rowids.length; i++) {
			temp[i] = getValueAt(rowids[i], 0).toString();//返回行的classid
		}
		return temp;
	}

	public void removeclassfromarraybyid(ArrayList array, String id) {
		for (int i = 0; i < array.size(); i++) {
			Class temp = (Class) array.get(i);
			if (temp.getClassId().equals(id)) {
				array.remove(i);
				array.trimToSize();
			}
		}
	}
	private ArrayList getclassarraycontent()
	{
		try {
			return uimanager.getclassarraycontent();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}

⌨️ 快捷键说明

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