我正在努力避免多个JTable的数据重复。基本上我有一个TableModel,它有一个数据的arraylist和一个string [] header。
到目前为止还没什么新鲜的。现在我有另一个TableModel,它具有相同的数据arylylist但是不同的string []标题。我无法让我的代码工作。关于如何跨多个表模型共享数据的arrayList,我将不胜感激。
因此,当我更改数据时,所有模型都会更新,并且没有数据重复。我想避免arraylist的副本 任何想法或我错误的设置?我确实尝试过监听器,但我认为这样我们仍然会在表模型中进行数据复制。
public class GymTableModel extends AbstractTableModel {
private final ResourceBundle name = ResourceBundle.getBundle("labels/TableLabel", Locale.getDefault());
private final String[] entete = {
name.getString("LNAME"),
name.getString("FNAME"),
name.getString("CONTACT"),
name.getString("TELFIX"),
name.getString("TELGSM"),
name.getString("EMAIL"),
name.getString("BDATE"),
name.getString("EDATE"),
name.getString("LICENSE"),
name.getString("AGE"),
name.getString("ANCIEN")
};
@Override
public Class<?> getColumnClass(int columnIndex){
switch(columnIndex){
case 6:
return Date.class;
case 7:
return Date.class;
case 9:
return Integer.class;
default:
return Object.class;
}
}
@Override
public int getRowCount() {
return gymList.size();
}
@Override
public int getColumnCount() {
return entete.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex){
case 0:
return gymList.get(rowIndex).getLastName();
case 1:
return gymList.get(rowIndex).getFirstName();
case 2:
return gymList.get(rowIndex).getContact();
case 3:
return gymList.get(rowIndex).getTelFix();
case 4:
return gymList.get(rowIndex).getTelGsm();
case 5:
return gymList.get(rowIndex).getEmail();
case 6:
return gymList.get(rowIndex).getDateBirth();
case 7:
return gymList.get(rowIndex).getDateClub();
case 8:
return gymList.get(rowIndex).getLicence();
case 9:
return gymList.get(rowIndex).getAge();
case 10:
return gymList.get(rowIndex).getAnciennete();
default:
return null; // should never happen;
}
}
public String getColumnName(int columnIndex){
return entete[columnIndex];
}
}
现在我有另一个表具有相同的数据但只有3列(LNAME,FNAME和AGE),因此getColumnClass不相同。问题是,我可以与另一个表模型共享我的gymList数据(这是一个arrayList)吗? 三江源
答案 0 :(得分:3)
使用Che的答案作为包含四列的主表的基础,那么您的代码应该是这样的:
AllTableModel model = new AllTableModel();
JTable mainTable = new JTable( model );
如果您想要另一个View
模型,则可以创建新视图而不是新模型。所以代码是:
JTable studentTable = new JTable( model );
studentTable.removeColumn( studentTable.getColumn("Subject") );
studentTable.removeColumn( studentTable.getColumn("Staff") );
现在,学生表只包含“名称”和“成绩”列。
这是MVC编程背后的基础。也就是说,您可以轻松更改View
的{{1}}。因此,您共享1个不共享3个独立模型数据的模型。
答案 1 :(得分:2)
我使用了一个存储所有行的表模型,另一个只存储学生详细信息的表模型和存储工作人员详细信息的第三个表模型。所有这些都从同一个来源获取数据,但每个表的列显示不同。
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;
}
}
答案 2 :(得分:2)
您需要将TableModelListener
注册到Tablemodel
的{{1}}。因此,只要其中一个表的单元格值发生更改,就可以将其反映在其他通过{{1} } JTables
的方法。例如,考虑下面给出的代码:
repaint()
答案 3 :(得分:1)
具有不同列名的相同data
的小示例:
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
public class TestTable {
public static class MyModel extends AbstractTableModel {
private List<Object[]> data;
private List<String> columnNames;
public MyModel(List<String> columnNames, List<Object[]> data) {
super();
this.columnNames = columnNames;
this.data = data;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columnNames.size();
}
@Override
public String getColumnName(int column) {
return columnNames.get(column);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
}
protected void initUI() {
JFrame frame = new JFrame(TestTable.class.getSimpleName());
List<String> columns = Arrays.asList("Name", "Age");
List<String> columns2 = Arrays.asList("Label", "Price");
List<Object[]> data = new ArrayList<Object[]>();
for (int i = 0; i < 50; i++) {
Object[] value = new Object[2];
value[0] = "Something-" + i;
value[1] = 12 + i;
data.add(value);
}
JTable table = new JTable(new MyModel(columns, data));
JTable table2 = new JTable(new MyModel(columns2, data));
frame.add(new JScrollPane(table), BorderLayout.WEST);
frame.add(new JScrollPane(table2), BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTable().initUI();
}
});
}
}