java – 将对象列表连接到JTable
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 将对象列表连接到JTable,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4256字,纯文字阅读大概需要7分钟。
内容图文
![java – 将对象列表连接到JTable](/upload/InfoBanner/zyjiaocheng/802/411f4253718e49c7a5c470b7d1204daa.jpg)
我有一个Java类型的SI(SingleInstruction)对象列表.
这是带有构造函数的SI类.
public class SI {
private String recipe;
private Integer makingOrder;
private String instruction;
private double estimatedTimeInMinutesSeconds;
private Double timerInMinutesSeconds;
private String timerEndingText;
private boolean containsOtherInstructions = false;
private List<SI> embeddedInstructionsList;
public SI (String s1, Integer i1, String s2, double d1, Double d2, String s3){
recipe = s1;
makingOrder = i1;
instruction = s2;
estimatedTimeInMinutesSeconds = d1;
timerInMinutesSeconds = d2;
timerEndingText = s3;
}
setters and getters methods ommited....
我需要在GUI中有一个表格,显示该列表的数据.
当我开始这个程序时,我创建了一个
public List<SI> finalList = new ArrayList();
这是空的,所以我需要一个空表来显示.
该表应该有列
(String Recipe, Integer Order, String Instruction, double Est.time,
Double Timer, String TimerText, boolean containOtherInstructions)
之后,finalList填充了数据,我需要将这些数据显示在表格中.
为了给你一个例子,我创建了一个创建一些SI的按钮并将它们添加到finalList.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
SI si1 = new SI("Greek Salad", 1, "cut veggies", 4, null, null);
SI si2 = new SI("Greek Salad", 2, "put spices", 1, null, null);
SI si3 = new SI("Greek Salad", 3, "feta and finish", 1, null, null);
SI si4 = new SI("Break", null, "Your free 10 minutes", 10, null, null);
SI si5 = new SI("Meat", 1, "preheat oven", 0.5, 10.0, "oven is ready");
SI si6 = new SI("Meat", 2, "spices on meat", 2, null, null);
SI si7 = new SI("Meat", 3, "cook meat", 1, 10.0, "meat is ready");
SI si8 = new SI("Meat", 4, "serve", 1, null, null);
SI si9 = new SI("Break", null, "Your free 10 minutes", 10, null, null);
SI si10 = new SI("Jelly", 1, "Boil water", 0.5, 4.0, "water is ready");
SI si11 = new SI("Jelly", 2, "add mix and stir", 4, null, null);
SI si12 = new SI("Jelly", 3, "share in bowls, wait 3 hours", 2, null, null);
finalList.add(si1);
finalList.add(si2);
finalList.add(si3);
finalList.add(si4);
finalList.add(si5);
finalList.add(si6);
finalList.add(si7);
finalList.add(si8);
finalList.add(si9);
finalList.add(si10);
finalList.add(si11);
finalList.add(si12);
}
现在我需要按下按钮,表格会更新并显示数据.
我已经尝试了很多方法来了解如何做到这一点,但这太难了.我不确定是否应该扩展AbstractTableModel或只使用defaultTableModel.
为了帮助您提供建议,此表的数据是最终的,用户无法修改任何单元格.应该允许用户做什么是向上或向下移动行以更改finalList中的顺序.
PS:如果您对GUI构建器有任何快速建议,我正在使用netbeans.
解决方法:
在模型 – 视图 – 控制器架构中,模型始终负责提供数据.如果您为JTable使用模型,则模型会提供数据.
为了创建模型,创建一个扩展AbstractTableModel的单独类,并具有一个构造函数,用于创建所需的所有SI对象并将它们存储到ArrayList中.
public class FinalTableModel extends AbstractTableModel {
private List<SI> li = new ArrayList();
private String[] columnNames = { "Recipe", "Order", "Instruction",
"Est.time", "Timer", "Timer Label", "Has Subinstructions"};
public FinalTableModel(List<SI> list){
this.li = list;
}
@Override
public String getColumnName(int columnIndex){
return columnNames[columnIndex];
}
@Override
public int getRowCount() {
return li.size();
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
SI si = list.get(rowIndex);
switch (columnIndex) {
case 0:
return si.getRecipe();
case 1:
return si.getMakingOrder();
case 2:
return si.getInstruction();
case 3:
return si.getEstimatedTimeInMinutesSeconds();
case 4:
return si.getTimerInMinutesSeconds();
case 5:
return si.getTimerEndingText();
case 6:
return si.isContainsOtherInstructions();
}
return null;
}
@Override
public Class<?> getColumnClass(int columnIndex){
switch (columnIndex){
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return String.class;
case 3:
return Double.class;
case 4:
return Double.class;
case 5:
return String.class;
case 6:
return Boolean.class;
}
return null;
}
}
然后制作你的JFrame,并像这样创建你的JTable:
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTable1.setModel(new FinalTableModel(finalList));
jScrollPane1.setViewportView(jTable1);
想法是JTable将通过调用getValueAt(row,col)自动请求模型中的数据.
Netbeans可以很容易地将模型分配给JTable.该模型是JTable的属性.单击“…”按钮,然后选择“自定义代码”.在下面创建一个模型实例.
内容总结
以上是互联网集市为您收集整理的java – 将对象列表连接到JTable全部内容,希望文章能够帮你解决java – 将对象列表连接到JTable所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。