java中的jTable问题(java,swing,jtable)
急!!!当我双击jTable1中的一行数据时,将我双击的这行数据传到jTable2中去,有没有哪个热心的人帮帮忙啊,我也没什么分送了,我会从心底里感激的!!!...
急!!!当我双击jTable1中的一行数据时,将我双击的这行数据传到jTable2中去,有没有哪个热心的人帮帮忙啊,我也没什么分送了,我会从心底里感激的!!!
展开
1个回答
展开全部
重点在于如何重写的TableModel
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class DataTransferDemo extends JFrame {
public DataTransferDemo() {
tableModel1 = new StudentTableModel();
tableModel2 = new StudentTableModel();
for (int i = 0; i < 10; i++) {
tableModel1.addStudent(new Student("" + i, i * 10));
}
final JTable table1 = new JTable(tableModel1);
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JTable table2 = new JTable(tableModel2);
table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
Student student = tableModel1.getStudent(table1
.getSelectedRow());
tableModel1.removeStudent(student);
tableModel2.addStudent(student);
}
}
});
table2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
Student student = tableModel2.getStudent(table2
.getSelectedRow());
tableModel2.removeStudent(student);
tableModel1.addStudent(student);
}
}
});
this.setLayout(new GridLayout(1, 2));
this.add(new JScrollPane(table1));
this.add(new JScrollPane(table2));
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new DataTransferDemo().setVisible(true);
}
/**
* 重写TableModel
*/
private class StudentTableModel extends AbstractTableModel {
public StudentTableModel() {
students = new ArrayList<DataTransferDemo.Student>();
}
public void addStudent(Student student) {
students.add(student);
fireTableStructureChanged();
}
public void removeStudent(Student student) {
students.remove(student);
fireTableStructureChanged();
}
public Student getStudent(int index) {
return students.get(index);
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return students.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return students.get(rowIndex).id;
case 1:
return students.get(rowIndex).score;
default:
return "";
}
}
private List<Student> students;
private String[] columnNames = { "学号", "成绩" };
}
/**
* 数据部分,自己修改
*/
private class Student {
public Student(String id, int score) {
this.id = id;
this.score = score;
}
String id;
int score;
}
private StudentTableModel tableModel1;
private StudentTableModel tableModel2;
}
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class DataTransferDemo extends JFrame {
public DataTransferDemo() {
tableModel1 = new StudentTableModel();
tableModel2 = new StudentTableModel();
for (int i = 0; i < 10; i++) {
tableModel1.addStudent(new Student("" + i, i * 10));
}
final JTable table1 = new JTable(tableModel1);
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JTable table2 = new JTable(tableModel2);
table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
Student student = tableModel1.getStudent(table1
.getSelectedRow());
tableModel1.removeStudent(student);
tableModel2.addStudent(student);
}
}
});
table2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
Student student = tableModel2.getStudent(table2
.getSelectedRow());
tableModel2.removeStudent(student);
tableModel1.addStudent(student);
}
}
});
this.setLayout(new GridLayout(1, 2));
this.add(new JScrollPane(table1));
this.add(new JScrollPane(table2));
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new DataTransferDemo().setVisible(true);
}
/**
* 重写TableModel
*/
private class StudentTableModel extends AbstractTableModel {
public StudentTableModel() {
students = new ArrayList<DataTransferDemo.Student>();
}
public void addStudent(Student student) {
students.add(student);
fireTableStructureChanged();
}
public void removeStudent(Student student) {
students.remove(student);
fireTableStructureChanged();
}
public Student getStudent(int index) {
return students.get(index);
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return students.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return students.get(rowIndex).id;
case 1:
return students.get(rowIndex).score;
default:
return "";
}
}
private List<Student> students;
private String[] columnNames = { "学号", "成绩" };
}
/**
* 数据部分,自己修改
*/
private class Student {
public Student(String id, int score) {
this.id = id;
this.score = score;
}
String id;
int score;
}
private StudentTableModel tableModel1;
private StudentTableModel tableModel2;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询