java Swing界面左边表格选中后添加到右边表格
先放个效果图,看看是不是你们想要的 话不多说,直接上代码
package com.ldj.test;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Dialog extends JDialog {
private static final long serialVersionUID = 1L;
private JTable leftProjectTable;
private JTable rightProjectTable;
private Vector<String> columnNames1;
private Vector<String> columnNames2;
private DefaultTableModel leftDataModel;
public DefaultTableModel rightDataModel;
private Vector<Vector<Object>> rowData;
public Dialog() {
inintUi();
}
public Vector<Vector<String>> getChooseDate() {
DefaultTableModel model = (DefaultTableModel) rightProjectTable.getModel();
return model.getDataVector();
}
@SuppressWarnings("serial")
private void inintUi() {
System.out.println("填报项目Dialog");
JPanel mainJPanel = new JPanel();
mainJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
mainJPanel.setPreferredSize(new Dimension(500, 400));
JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
row1.setPreferredSize(new Dimension(750, 40));
JLabel xmbhJL = new JLabel("项目编号");
xmbhJL.setFont(new java.awt.Font("宋体", 1, 15));
JTextField xmbhJTextField = new JTextField();
xmbhJTextField.setPreferredSize(new Dimension(223, 25));
JLabel xmmcJL = new JLabel("项目名称");
xmmcJL.setFont(new java.awt.Font("宋体", 1, 15));
JTextField xmmcJTField = new JTextField();
xmmcJTField.setPreferredSize(new Dimension(223, 25));
JButton cxJB = new JButton("查询");
cxJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
String code = xmbhJTextField.getText().trim();
String name = xmmcJTField.getText().trim();
if ("".equals(code) && "".equals(name)) {
javax.swing.JOptionPane.showMessageDialog(null, "请输入查询条件");
return;
}
DefaultTableModel allmodel = (DefaultTableModel) leftProjectTable.getModel();
DefaultTableModel rightmodel = (DefaultTableModel) rightProjectTable.getModel();
Vector beforeDatas = rightmodel.getDataVector();
for (int i = 0; i < allmodel.getRowCount(); i++) {
Vector<String> row = new Vector<String>();
String tempNum = (String) leftProjectTable.getValueAt(i, 0);
String tempName = (String) leftProjectTable.getValueAt(i, 1);
if (tempNum.contains(code) && tempName.contains(name)) {
row.add(tempNum);
row.add(tempName);
} else if (!"".equals(code)) {
if (tempNum.contains(code)) {
row.add(tempNum);
row.add(tempName);
}
} else if (!"".equals(name)) {
if (tempName.contains(name)) {
row.add(tempNum);
row.add(tempName);
}
}
System.out.println("row:" + row);
if (null != row && row.size() > 0) {
System.out.println("row.size():" + row.size() + "\t" + row);
if (!beforeDatas.contains(row)) {
beforeDatas.add(row);
}
}
}
System.out.println("beforeDatas:" + beforeDatas);
if (null != beforeDatas && beforeDatas.size() > 0) {
rightmodel.setDataVector(beforeDatas, columnNames2);
rightProjectTable.setModel(rightmodel);
System.out.println("-----142");
}
}
});
row1.add(getBlankPanel(30, 1));
row1.add(xmbhJL);
row1.add(xmbhJTextField);
row1.add(xmmcJL);
row1.add(xmmcJTField);
row1.add(getBlankPanel(10, 1));
row1.add(cxJB);
JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
row2.setPreferredSize(new Dimension(1300, 30));
JLabel allProjectJL = new JLabel("所参加的项目");
allProjectJL.setFont(new java.awt.Font("宋体", 1, 15));
JLabel chooseProjectJL = new JLabel("添加的项目");
chooseProjectJL.setFont(new java.awt.Font("宋体", 1, 15));
row2.add(getBlankPanel(130, 1));
row2.add(allProjectJL);
row2.add(getBlankPanel(490, 1));
row2.add(chooseProjectJL);
JPanel row3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
row3.setPreferredSize(new Dimension(1350, 450));
leftProjectTable = new JTable(leftDataModel) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
return false;
}
};
;
leftProjectTable.getTableHeader().setReorderingAllowed(false);
leftProjectTable.setRowHeight(30);
leftProjectTable.setFillsViewportHeight(true);
leftDataModel = new DefaultTableModel(columnNames1, 0);
Vector<Vector<String>> rows = new Vector<Vector<String>>();
for (int i = 0; i < 4; i++) {
Vector<String> row = new Vector<String>();
row.add("001");
row.add("语文");
rows.add(row);
}
columnNames1 = new Vector<String>();
columnNames1.add("项目编码");
columnNames1.add("项目名称");
leftDataModel.setDataVector(rows, columnNames1);
leftProjectTable.setModel(leftDataModel);
JScrollPane jsp1 = new JScrollPane(leftProjectTable);
jsp1.setPreferredSize(new Dimension(520, 400));
rightProjectTable = new JTable(rightDataModel) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
rightProjectTable.getTableHeader().setReorderingAllowed(false);
rightProjectTable.setRowHeight(30);
rightProjectTable.setFillsViewportHeight(true);
rightDataModel = new DefaultTableModel(columnNames2, 0);
rowData = new Vector<Vector<Object>>();
columnNames2 = new Vector<String>();
columnNames2.add("项目编码");
columnNames2.add("项目名称");
rightDataModel.setDataVector(rowData, columnNames2);
rightProjectTable.setModel(rightDataModel);
JScrollPane jsp2 = new JScrollPane(rightProjectTable);
jsp2.setPreferredSize(new Dimension(520, 400));
JPanel buttonJPanel = new JPanel();
buttonJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonJPanel.setPreferredSize(new Dimension(70, 70));
JButton tjJB = new JButton("添加");
tjJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
System.out.println("添加按钮被点击");
int[] index = leftProjectTable.getSelectedRows();
System.out.print("选择的行索引: ");
for (int i = 0; i < index.length; i++) {
System.out.print(i + ":" + index[i]);
}
DefaultTableModel model = (DefaultTableModel) leftProjectTable.getModel();
DefaultTableModel rightmodel = (DefaultTableModel) rightProjectTable.getModel();
Vector beforeDatas = rightmodel.getDataVector();
for (int i = 0; i < index.length; i++) {
System.out.println("i=====" + i);
int columnCount = model.getColumnCount();
Vector<String> row = new Vector<String>();
for (int j = 0; j < columnCount; j++) {
System.out.println("j=====" + j);
System.out.println("index[i] = " + index[i]);
row.add(model.getValueAt(index[i], j).toString());
}
if (!beforeDatas.contains(row)) {
beforeDatas.add(row);
}
}
rightmodel.setDataVector(beforeDatas, columnNames2);
rightProjectTable.setModel(rightmodel);
System.out.println("设置添加行完成!");
}
});
JButton scJB = new JButton("删除");
scJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
DefaultTableModel model = (DefaultTableModel) rightProjectTable.getModel();
int[] rowIndex = rightProjectTable.getSelectedRows();
for (int i = rowIndex.length - 1; i >= 0; i--) {
model.removeRow(rowIndex[i]);
rightProjectTable.setModel(model);
}
}
});
buttonJPanel.add(tjJB);
buttonJPanel.add(scJB);
JPanel qdButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 20));
qdButtonPanel.setPreferredSize(new Dimension(1300, 70));
JButton quedingJB = new JButton("确定");
quedingJB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
dispose();
}
});
qdButtonPanel.add(getBlankPanel(900, 1));
qdButtonPanel.add(quedingJB);
row3.add(getBlankPanel(30, 1));
row3.add(jsp1);
row3.add(buttonJPanel);
row3.add(jsp2);
mainJPanel.add(getBlankPanel(800, 20));
mainJPanel.add(row1);
mainJPanel.add(row2);
mainJPanel.add(row3);
mainJPanel.add(qdButtonPanel);
add(mainJPanel);
setSize(1300, 700);
setTitle("项目选择");
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Dialog();
}
private JPanel getBlankPanel(int width, int height) {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(width, height));
return panel;
}
}
拿来就用好吧,优秀的程序员也是个优秀的复制粘贴大师! 觉得有用的给我点个赞好吧,白嫖的真香!
|