我使用了一个存储所有行的表模型,另一个只存储学生详细信息的表模型和存储员工详细信息的第三个表模型。他们都从同一来源获取数据,但每个表的列显示不同。
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class AllTableExample {
private JTabbedPane tabbedPane;
private JTable allTable;
private JTable studentTable;
private JTable staffTable;
public AllTableExample() {
setData();
allTable = new JTable();
studentTable = new JTable();
staffTable = new JTable();
}
public void createUI() {
JFrame frame = new JFrame();
tabbedPane = new JTabbedPane();
tabbedPane.add("All", getAllTablePanel());
tabbedPane.add("Student", getStudentTablePanel());
tabbedPane.add("Staff", getStaffTablePanel());
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Table Model Example.");
frame.pack();
frame.setVisible(true);
}
private void setData() {
List<TableData> data = new ArrayList<TableData>();
data.add(new TableData("John1", "A", "Maths", "Hellen1"));
data.add(new TableData("John2", "A", "Maths", "Hellen2"));
data.add(new TableData("John3", "A", "Maths", "Hellen3"));
data.add(new TableData("John4", "A", "Maths", "Hellen4"));
data.add(new TableData("John5", "A", "Maths", "Hellen5"));
MainDataClass.getInstance().setData(data);
}
private JPanel getAllTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(new AllTableModel());
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
private JPanel getStudentTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(new StudentTableModel());
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
private JPanel getStaffTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(new StaffTableModel());
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new AllTableExample().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class AllTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Name", "Grade", "Subject", "Staff"};
public AllTableModel() {
tableData = MainDataClass.getInstance().getData();
}
@Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return tableData.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
switch (columnIndex) {
case 0:
return data.getName();
case 1:
return data.getGrade();
case 2:
return data.getSubject();
case 3:
return data.getStaff();
default:
return null;
}
}
}
class StudentTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Name", "Grade"};
public StudentTableModel() {
tableData = MainDataClass.getInstance().getData();
}
@Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return tableData.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
switch (columnIndex) {
case 0:
return data.getName();
case 1:
return data.getGrade();
default:
return null;
}
}
}
class StaffTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Subject", "Staff"};
public StaffTableModel() {
tableData = MainDataClass.getInstance().getData();
}
@Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return tableData.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
switch (columnIndex) {
case 0:
return data.getSubject();
case 1:
return data.getStaff();
default:
return null;
}
}
}
class TableData {
private String name;
private String grade;
private String subject;
private String staff;
public TableData(String name, String grade, String subject, String staff) {
super();
this.name = name;
this.grade = grade;
this.subject = subject;
this.staff = staff;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getStaff() {
return staff;
}
public void setStaff(String staff) {
this.staff = staff;
}
}
class MainDataClass {
private static MainDataClass mainDataClass;
private List<TableData> data;
private MainDataClass() {
}
public static MainDataClass getInstance() {
if(null == mainDataClass) {
mainDataClass = new MainDataClass();
}
return mainDataClass;
}
public List<TableData> getData() {
return data;
}
public void setData(List<TableData> data) {
this.data = data;
}
}
编辑-2:
参考@camickr 对仅使用一种模型的解释,我尝试了这个模型,它奏效了。使用removeColumn
它的方法就像一个魅力。
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class AllTableExample {
private JTabbedPane tabbedPane;
private JTable allTable;
private JTable studentTable;
private JTable staffTable;
private AllTableModel allTableModel;
public AllTableExample() {
allTable = new JTable();
studentTable = new JTable();
staffTable = new JTable();
allTableModel = new AllTableModel();
}
public void createUI() {
JFrame frame = new JFrame();
tabbedPane = new JTabbedPane();
tabbedPane.add("All", getAllTablePanel());
tabbedPane.add("Student", getStudentTablePanel());
tabbedPane.add("Staff", getStaffTablePanel());
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Table Model Example.");
frame.pack();
frame.setVisible(true);
}
private JPanel getAllTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(allTableModel);
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
private JPanel getStudentTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(allTableModel);
table.removeColumn(table.getColumn("Subject"));
table.removeColumn(table.getColumn("Staff"));
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
private JPanel getStaffTablePanel() {
JPanel panel = new JPanel();
JTable table = new JTable(allTableModel);
table.removeColumn(table.getColumn("Name"));
table.removeColumn(table.getColumn("Grade"));
JScrollPane scroll = new JScrollPane(table);
panel.add(scroll);
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new AllTableExample().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class AllTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Name", "Grade", "Subject", "Staff"};
public AllTableModel() {
List<TableData> data = new ArrayList<TableData>();
data.add(new TableData("John1", "A", "Maths", "Hellen1"));
data.add(new TableData("John2", "A", "Maths", "Hellen2"));
data.add(new TableData("John3", "A", "Maths", "Hellen3"));
data.add(new TableData("John4", "A", "Maths", "Hellen4"));
data.add(new TableData("John5", "A", "Maths", "Hellen5"));
tableData = data;
}
@Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return tableData.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
switch (columnIndex) {
case 0:
return data.getName();
case 1:
return data.getGrade();
case 2:
return data.getSubject();
case 3:
return data.getStaff();
default:
return null;
}
}
}
class TableData {
private String name;
private String grade;
private String subject;
private String staff;
public TableData(String name, String grade, String subject, String staff) {
super();
this.name = name;
this.grade = grade;
this.subject = subject;
this.staff = staff;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getStaff() {
return staff;
}
public void setStaff(String staff) {
this.staff = staff;
}
}
class MainDataClass {
private static MainDataClass mainDataClass;
private List<TableData> data;
private MainDataClass() {
}
public static MainDataClass getInstance() {
if(null == mainDataClass) {
mainDataClass = new MainDataClass();
}
return mainDataClass;
}
public List<TableData> getData() {
return data;
}
public void setData(List<TableData> data) {
this.data = data;
}
}