📄 studenttable.java
字号:
package client;
import java.awt.Color;
import java.awt.Dimension;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import baseClass.Class;
import baseClass.Student;
import baseClass.StudentTableRender;
public class StudentTable extends JTable{
String titles[];
Object[][] classsdate;
DefaultTableModel dm;
public static ArrayList ALLSTUDENT;
public static ArrayList ALLCLASS;
UIManager uimanager;
public StudentTable(UIManager arguimanager){
super();
uimanager=arguimanager;
dateBuild();
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.setPreferredScrollableViewportSize(new Dimension(400,150));
this.setSelectionBackground(Color.black);
this.setSelectionForeground(Color.white);
//set font size
//this.setFont(new Font("Courier", Font.PLAIN, 13));
//no column moving
//this.getTableHeader().setReorderingAllowed(false);
//System.out.println("Hltable column count =" + this.getColumnCount());
}
public Object[][] getstudentdate()
{
ALLSTUDENT=getstudentarraycontent();
ALLCLASS=getclassarraycontent();
Object[][] results = new Object[ALLSTUDENT.size()][4];
for (int i=0; i < ALLSTUDENT.size(); i++) {
Student tempclass=((Student)ALLSTUDENT.get(i));
results[i][0] = tempclass.getStudentId();
results[i][1] =tempclass.getStudentNmae();
if(tempclass.getClassId().equals("0"))
{
results[i][2]="not select";
results[i][3]="not select";
}
else
{
String classname =getclassnamebyid(tempclass.getClassId());
results[i][2]=tempclass.getClassId();
results[i][3]=classname;
}
}
return results;
}
public void dateBuild()
{
titles = new String[] {"student id", "name","class id"," class name"};
classsdate=getstudentdate();
dm = new DefaultTableModel();
dm.setDataVector(classsdate, titles);
this.setModel(dm);
StudentTableRender.makeFace(this);
}
private String getclassnamebyid(String Id)
{
for (int i=0; i < ALLCLASS.size(); i++)
{
Class tempclass=((Class)ALLCLASS.get(i));
if(tempclass.getClassId().equals(Id))
{
return tempclass.getClassName();
}
}
return "class null";
}
/**
* return select row's class id
* @param row selected row
* @return class id
*/
public String getclassid(int row)
{
Student temp=(Student)ALLSTUDENT.get(row);
return temp.getClassId();
}
/**
* modify one Cell need to update interface and ALLCLASS's date
*/
public void setValueAt(Object aValue,int row,int column)
{
Student temp=(Student)ALLSTUDENT.get(row);
if(column==0)//modify first col
{
try
{
int id= Integer.parseInt(aValue.toString());
temp.setStudentId(aValue.toString());
dm.setValueAt(aValue, row, column);
} catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Please enter only integer values.");
}
}
if(column==2)//modify third col
{
try
{
int id= Integer.parseInt(aValue.toString());
temp.setClassId(aValue.toString());
dm.setValueAt(aValue, row, column);
} catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this,"Please enter only integer values.");
}
}
else//modify other
{
temp.setStudentNmae(aValue.toString());
dm.setValueAt(aValue, row, column);
}
}
/**
* Add a new row, need to update data and interface ALLCLASS
* @param rowData
*/
public void addRow(Object[] rowData)
{
ALLSTUDENT.add(new Student(rowData[0].toString(),rowData[1].toString(),rowData[2].toString()));
dm.addRow(rowData);
}
public void savedate()
{
new Student().saveAllStudent(ALLSTUDENT);
}
public boolean isCellEditable(int row,int column)
{
if(column==3)
{
return false;
}
else
{
return true;
}
}
public void removerow()
{
int selectrows= this.getSelectedRow();
if(selectrows!=-1)
{
ALLSTUDENT.remove(selectrows);
dm.removeRow(selectrows);
}
}
private ArrayList getclassarraycontent()
{
try {
return uimanager.getclassarraycontent();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private ArrayList getstudentarraycontent()
{
try {
return uimanager.getstudentarraycontent();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -