如何将arraylist数据导入到jtable中
1个回答
展开全部
package test;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
@SuppressWarnings("serial")
public class TestJtable extends JPanel {
public TestJtable(){
init();
}
private void init() {
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(createTable());
add(scrollPane);
}
private JTable createTable() {
JTable table = new JTable();
String[] tableHeads = new String[]{"姓名","年龄","是否通过"};
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setColumnIdentifiers(tableHeads);
/**ArrayList 数据**/
List arraylist = new ArrayList();
arraylist.add("张三");
arraylist.add("13");
arraylist.add(new Boolean(false));
/******转化成Vector***********/
Vector v = new Vector();
v.add(arraylist.get(0));
v.add(arraylist.get(1));
v.add(arraylist.get(2));
/********添加进JTable中********/
dtm.addRow(v);
/*****设置table的列模型****/
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(2).setCellRenderer(new TestTableCellRenderer());
tcm.getColumn(2).setPreferredWidth(80);
tcm.getColumn(2).setWidth(80);
tcm.getColumn(2).setMaxWidth(80);
return table;
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(300,100,300,200);
jframe.setTitle("测试");
jframe.add(new TestJtable());
jframe.setVisible(true);
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
return this;
}
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
@SuppressWarnings("serial")
public class TestJtable extends JPanel {
public TestJtable(){
init();
}
private void init() {
setLayout(new BorderLayout());
JScrollPane scrollPane = new JScrollPane(createTable());
add(scrollPane);
}
private JTable createTable() {
JTable table = new JTable();
String[] tableHeads = new String[]{"姓名","年龄","是否通过"};
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setColumnIdentifiers(tableHeads);
/**ArrayList 数据**/
List arraylist = new ArrayList();
arraylist.add("张三");
arraylist.add("13");
arraylist.add(new Boolean(false));
/******转化成Vector***********/
Vector v = new Vector();
v.add(arraylist.get(0));
v.add(arraylist.get(1));
v.add(arraylist.get(2));
/********添加进JTable中********/
dtm.addRow(v);
/*****设置table的列模型****/
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
tcm.getColumn(2).setCellRenderer(new TestTableCellRenderer());
tcm.getColumn(2).setPreferredWidth(80);
tcm.getColumn(2).setWidth(80);
tcm.getColumn(2).setMaxWidth(80);
return table;
}
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(300,100,300,200);
jframe.setTitle("测试");
jframe.add(new TestJtable());
jframe.setVisible(true);
}
class TestTableCellRenderer extends JCheckBox implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Boolean b = (Boolean) value;
this.setSelected(b.booleanValue());
return this;
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |